Skip to content

Commit

Permalink
MAGETWO-69879: Fix PaymentTokenFactory interface to have the "Interfa…
Browse files Browse the repository at this point in the history
…ce" at the end of the name. #9306
  • Loading branch information
Oleksii Korshenko authored Jun 15, 2017
2 parents 04b3b52 + c86bb85 commit e120d8a
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 14 deletions.
28 changes: 28 additions & 0 deletions app/code/Magento/Vault/Api/Data/PaymentTokenFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Vault\Api\Data;

/**
* Interface PaymentTokenFactoryInterface
* @api
*/
interface PaymentTokenFactoryInterface
{
/**
* Payment Token types
* @var string
*/
const TOKEN_TYPE_ACCOUNT = 'account';
const TOKEN_TYPE_CREDIT_CARD = 'card';

/**
* Create payment token entity
* @param $type string|null
* @return PaymentTokenInterface
*/
public function create($type = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

/**
* Interface PaymentTokenInterfaceFactory
* @api
* @deprecated
* @see PaymentTokenFactoryInterface
*/
interface PaymentTokenInterfaceFactory
{
Expand Down
26 changes: 19 additions & 7 deletions app/code/Magento/Vault/Model/AbstractPaymentTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Magento\Framework\ObjectManagerInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory;
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;

/**
* Class AbstractPaymentTokenFactory
* @api
* @deprecated
* @see PaymentTokenFactoryInterface
*/
abstract class AbstractPaymentTokenFactory implements PaymentTokenInterfaceFactory
{
Expand All @@ -20,13 +22,26 @@ abstract class AbstractPaymentTokenFactory implements PaymentTokenInterfaceFacto
*/
private $objectManager;

/**
* @var PaymentTokenFactoryInterface
*/
private $paymentTokenFactory;

/**
* AccountPaymentTokenFactory constructor.
* @param ObjectManagerInterface $objectManager
* @param PaymentTokenFactoryInterface $paymentTokenFactory
*/
public function __construct(ObjectManagerInterface $objectManager)
{
public function __construct(
ObjectManagerInterface $objectManager,
PaymentTokenFactoryInterface $paymentTokenFactory = null
) {
if ($paymentTokenFactory === null) {
$paymentTokenFactory = $objectManager->get(PaymentTokenFactoryInterface::class);
}

$this->objectManager = $objectManager;
$this->paymentTokenFactory = $paymentTokenFactory;
}

/**
Expand All @@ -35,9 +50,6 @@ public function __construct(ObjectManagerInterface $objectManager)
*/
public function create()
{
/** @var PaymentTokenInterface $paymentToken */
$paymentToken = $this->objectManager->create(PaymentTokenInterface::class);
$paymentToken->setType($this->getType());
return $paymentToken;
return $this->paymentTokenFactory->create($this->getType());
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/Vault/Model/AccountPaymentTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

/**
* Class AccountPaymentTokenFactory
* @api
* @deprecated
* @see PaymentTokenFactoryInterface
*/
class AccountPaymentTokenFactory extends AbstractPaymentTokenFactory
{
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Vault/Model/CreditCardTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

/**
* Class CreditCardTokenFactory
* @api
* @deprecated
* @see PaymentTokenFactoryInterface
*/
class CreditCardTokenFactory extends AbstractPaymentTokenFactory
{
Expand Down
62 changes: 62 additions & 0 deletions app/code/Magento/Vault/Model/PaymentTokenFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Vault\Model;

use Magento\Framework\ObjectManagerInterface;
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;

/**
* PaymentTokenFactory class
* @api
*/
class PaymentTokenFactory implements PaymentTokenFactoryInterface
{
/**
* @var array
*/
private $tokenTypes = [];

/**
* PaymentTokenFactory constructor.
* @param ObjectManagerInterface $objectManager
* @param array $tokenTypes
*/
public function __construct(ObjectManagerInterface $objectManager, array $tokenTypes = [])
{
$this->objectManager = $objectManager;
$this->tokenTypes = $tokenTypes;
}

/**
* Create payment token entity
* @param $type string
* @return PaymentTokenInterface
*/
public function create($type = null)
{
/**
* This code added for Backward Compatibility reasons only, as previous implementation of Code Generated factory
* accepted an array as any other code generated factory
*/
if (is_array($type)) {
return $this->objectManager->create(
PaymentTokenInterface::class,
$type
);
}

if ($type !== null && !in_array($type, $this->tokenTypes, true)) {
throw new \LogicException('There is no such payment token type: ' . $type);
}

return $this->objectManager->create(
PaymentTokenInterface::class,
['data' => [PaymentTokenInterface::TYPE => $type]]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\PaymentTokenFactory;
use Magento\Vault\Model\AccountPaymentTokenFactory;
use Magento\Vault\Model\PaymentToken;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
Expand Down Expand Up @@ -36,10 +37,16 @@ protected function setUp()
{
$objectManager = new ObjectManager($this);

$this->paymentToken = $objectManager->getObject(PaymentToken::class);
$tokenTypes = [
'account' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT,
'credit_card' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD
];

$this->paymentToken = $objectManager->getObject(PaymentToken::class);
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
$this->factory = new AccountPaymentTokenFactory($this->objectManager);

$this->paymentTokenFactory = new PaymentTokenFactory($this->objectManager, $tokenTypes);
$this->factory = new AccountPaymentTokenFactory($this->objectManager, $this->paymentTokenFactory);
}

/**
Expand All @@ -51,6 +58,8 @@ public function testCreate()
->method('create')
->willReturn($this->paymentToken);

$this->paymentToken->setType(\Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT);

/** @var PaymentTokenInterface $paymentToken */
$paymentToken = $this->factory->create();
static::assertInstanceOf(PaymentTokenInterface::class, $paymentToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\PaymentTokenFactory;
use Magento\Vault\Model\CreditCardTokenFactory;
use Magento\Vault\Model\PaymentToken;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
Expand Down Expand Up @@ -36,10 +37,16 @@ protected function setUp()
{
$objectManager = new ObjectManager($this);

$this->paymentToken = $objectManager->getObject(PaymentToken::class);
$tokenTypes = [
'account' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT,
'credit_card' => \Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD
];

$this->paymentToken = $objectManager->getObject(PaymentToken::class);
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
$this->factory = new CreditCardTokenFactory($this->objectManager);

$this->paymentTokenFactory = new PaymentTokenFactory($this->objectManager, $tokenTypes);
$this->factory = new CreditCardTokenFactory($this->objectManager, $this->paymentTokenFactory);
}

/**
Expand All @@ -51,6 +58,8 @@ public function testCreate()
->method('create')
->willReturn($this->paymentToken);

$this->paymentToken->setType(\Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);

/** @var PaymentTokenInterface $paymentToken */
$paymentToken = $this->factory->create();
static::assertInstanceOf(PaymentTokenInterface::class, $paymentToken);
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Vault/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Vault\Model\VaultPaymentInterface" type="Magento\Vault\Model\Method\Vault"/>
<preference for="Magento\Vault\Api\Data\PaymentTokenInterface" type="Magento\Vault\Model\PaymentToken"/>
<preference for="Magento\Vault\Api\Data\PaymentTokenFactoryInterface" type="Magento\Vault\Model\PaymentTokenFactory"/>
<preference for="Magento\Vault\Api\PaymentTokenRepositoryInterface" type="Magento\Vault\Model\PaymentTokenRepository" />
<preference for="Magento\Vault\Api\PaymentTokenManagementInterface" type="Magento\Vault\Model\PaymentTokenManagement" />
<preference for="Magento\Vault\Api\PaymentMethodListInterface" type="Magento\Vault\Model\PaymentMethodList" />
Expand Down Expand Up @@ -43,4 +44,13 @@
</argument>
</arguments>
</virtualType>

<type name="Magento\Vault\Model\PaymentTokenFactory">
<arguments>
<argument name="tokenTypes" xsi:type="array">
<item name="account" xsi:type="const">Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT</item>
<item name="credit_card" xsi:type="const">Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit e120d8a

Please sign in to comment.