Skip to content

Commit e120d8a

Browse files
author
Oleksii Korshenko
authored
MAGETWO-69879: Fix PaymentTokenFactory interface to have the "Interface" at the end of the name. #9306
2 parents 04b3b52 + c86bb85 commit e120d8a

9 files changed

+147
-14
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Vault\Api\Data;
8+
9+
/**
10+
* Interface PaymentTokenFactoryInterface
11+
* @api
12+
*/
13+
interface PaymentTokenFactoryInterface
14+
{
15+
/**
16+
* Payment Token types
17+
* @var string
18+
*/
19+
const TOKEN_TYPE_ACCOUNT = 'account';
20+
const TOKEN_TYPE_CREDIT_CARD = 'card';
21+
22+
/**
23+
* Create payment token entity
24+
* @param $type string|null
25+
* @return PaymentTokenInterface
26+
*/
27+
public function create($type = null);
28+
}

app/code/Magento/Vault/Api/Data/PaymentTokenInterfaceFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
/**
1010
* Interface PaymentTokenInterfaceFactory
11-
* @api
11+
* @deprecated
12+
* @see PaymentTokenFactoryInterface
1213
*/
1314
interface PaymentTokenInterfaceFactory
1415
{

app/code/Magento/Vault/Model/AbstractPaymentTokenFactory.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Magento\Framework\ObjectManagerInterface;
99
use Magento\Vault\Api\Data\PaymentTokenInterface;
1010
use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory;
11+
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
1112

1213
/**
1314
* Class AbstractPaymentTokenFactory
14-
* @api
15+
* @deprecated
16+
* @see PaymentTokenFactoryInterface
1517
*/
1618
abstract class AbstractPaymentTokenFactory implements PaymentTokenInterfaceFactory
1719
{
@@ -20,13 +22,26 @@ abstract class AbstractPaymentTokenFactory implements PaymentTokenInterfaceFacto
2022
*/
2123
private $objectManager;
2224

25+
/**
26+
* @var PaymentTokenFactoryInterface
27+
*/
28+
private $paymentTokenFactory;
29+
2330
/**
2431
* AccountPaymentTokenFactory constructor.
2532
* @param ObjectManagerInterface $objectManager
33+
* @param PaymentTokenFactoryInterface $paymentTokenFactory
2634
*/
27-
public function __construct(ObjectManagerInterface $objectManager)
28-
{
35+
public function __construct(
36+
ObjectManagerInterface $objectManager,
37+
PaymentTokenFactoryInterface $paymentTokenFactory = null
38+
) {
39+
if ($paymentTokenFactory === null) {
40+
$paymentTokenFactory = $objectManager->get(PaymentTokenFactoryInterface::class);
41+
}
42+
2943
$this->objectManager = $objectManager;
44+
$this->paymentTokenFactory = $paymentTokenFactory;
3045
}
3146

3247
/**
@@ -35,9 +50,6 @@ public function __construct(ObjectManagerInterface $objectManager)
3550
*/
3651
public function create()
3752
{
38-
/** @var PaymentTokenInterface $paymentToken */
39-
$paymentToken = $this->objectManager->create(PaymentTokenInterface::class);
40-
$paymentToken->setType($this->getType());
41-
return $paymentToken;
53+
return $this->paymentTokenFactory->create($this->getType());
4254
}
4355
}

app/code/Magento/Vault/Model/AccountPaymentTokenFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
/**
99
* Class AccountPaymentTokenFactory
10-
* @api
10+
* @deprecated
11+
* @see PaymentTokenFactoryInterface
1112
*/
1213
class AccountPaymentTokenFactory extends AbstractPaymentTokenFactory
1314
{

app/code/Magento/Vault/Model/CreditCardTokenFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
/**
99
* Class CreditCardTokenFactory
10-
* @api
10+
* @deprecated
11+
* @see PaymentTokenFactoryInterface
1112
*/
1213
class CreditCardTokenFactory extends AbstractPaymentTokenFactory
1314
{
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Vault\Model;
8+
9+
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
11+
use Magento\Vault\Api\Data\PaymentTokenInterface;
12+
13+
/**
14+
* PaymentTokenFactory class
15+
* @api
16+
*/
17+
class PaymentTokenFactory implements PaymentTokenFactoryInterface
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $tokenTypes = [];
23+
24+
/**
25+
* PaymentTokenFactory constructor.
26+
* @param ObjectManagerInterface $objectManager
27+
* @param array $tokenTypes
28+
*/
29+
public function __construct(ObjectManagerInterface $objectManager, array $tokenTypes = [])
30+
{
31+
$this->objectManager = $objectManager;
32+
$this->tokenTypes = $tokenTypes;
33+
}
34+
35+
/**
36+
* Create payment token entity
37+
* @param $type string
38+
* @return PaymentTokenInterface
39+
*/
40+
public function create($type = null)
41+
{
42+
/**
43+
* This code added for Backward Compatibility reasons only, as previous implementation of Code Generated factory
44+
* accepted an array as any other code generated factory
45+
*/
46+
if (is_array($type)) {
47+
return $this->objectManager->create(
48+
PaymentTokenInterface::class,
49+
$type
50+
);
51+
}
52+
53+
if ($type !== null && !in_array($type, $this->tokenTypes, true)) {
54+
throw new \LogicException('There is no such payment token type: ' . $type);
55+
}
56+
57+
return $this->objectManager->create(
58+
PaymentTokenInterface::class,
59+
['data' => [PaymentTokenInterface::TYPE => $type]]
60+
);
61+
}
62+
}

app/code/Magento/Vault/Test/Unit/Model/AccountPaymentTokenFactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\ObjectManagerInterface;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1010
use Magento\Vault\Api\Data\PaymentTokenInterface;
11+
use Magento\Vault\Model\PaymentTokenFactory;
1112
use Magento\Vault\Model\AccountPaymentTokenFactory;
1213
use Magento\Vault\Model\PaymentToken;
1314
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -36,10 +37,16 @@ protected function setUp()
3637
{
3738
$objectManager = new ObjectManager($this);
3839

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

45+
$this->paymentToken = $objectManager->getObject(PaymentToken::class);
4146
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
42-
$this->factory = new AccountPaymentTokenFactory($this->objectManager);
47+
48+
$this->paymentTokenFactory = new PaymentTokenFactory($this->objectManager, $tokenTypes);
49+
$this->factory = new AccountPaymentTokenFactory($this->objectManager, $this->paymentTokenFactory);
4350
}
4451

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

61+
$this->paymentToken->setType(\Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT);
62+
5463
/** @var PaymentTokenInterface $paymentToken */
5564
$paymentToken = $this->factory->create();
5665
static::assertInstanceOf(PaymentTokenInterface::class, $paymentToken);

app/code/Magento/Vault/Test/Unit/Model/CreditCardTokenFactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\ObjectManagerInterface;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1010
use Magento\Vault\Api\Data\PaymentTokenInterface;
11+
use Magento\Vault\Model\PaymentTokenFactory;
1112
use Magento\Vault\Model\CreditCardTokenFactory;
1213
use Magento\Vault\Model\PaymentToken;
1314
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -36,10 +37,16 @@ protected function setUp()
3637
{
3738
$objectManager = new ObjectManager($this);
3839

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

45+
$this->paymentToken = $objectManager->getObject(PaymentToken::class);
4146
$this->objectManager = $this->getMock(ObjectManagerInterface::class);
42-
$this->factory = new CreditCardTokenFactory($this->objectManager);
47+
48+
$this->paymentTokenFactory = new PaymentTokenFactory($this->objectManager, $tokenTypes);
49+
$this->factory = new CreditCardTokenFactory($this->objectManager, $this->paymentTokenFactory);
4350
}
4451

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

61+
$this->paymentToken->setType(\Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);
62+
5463
/** @var PaymentTokenInterface $paymentToken */
5564
$paymentToken = $this->factory->create();
5665
static::assertInstanceOf(PaymentTokenInterface::class, $paymentToken);

app/code/Magento/Vault/etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
1010
<preference for="Magento\Vault\Model\VaultPaymentInterface" type="Magento\Vault\Model\Method\Vault"/>
1111
<preference for="Magento\Vault\Api\Data\PaymentTokenInterface" type="Magento\Vault\Model\PaymentToken"/>
12+
<preference for="Magento\Vault\Api\Data\PaymentTokenFactoryInterface" type="Magento\Vault\Model\PaymentTokenFactory"/>
1213
<preference for="Magento\Vault\Api\PaymentTokenRepositoryInterface" type="Magento\Vault\Model\PaymentTokenRepository" />
1314
<preference for="Magento\Vault\Api\PaymentTokenManagementInterface" type="Magento\Vault\Model\PaymentTokenManagement" />
1415
<preference for="Magento\Vault\Api\PaymentMethodListInterface" type="Magento\Vault\Model\PaymentMethodList" />
@@ -43,4 +44,13 @@
4344
</argument>
4445
</arguments>
4546
</virtualType>
47+
48+
<type name="Magento\Vault\Model\PaymentTokenFactory">
49+
<arguments>
50+
<argument name="tokenTypes" xsi:type="array">
51+
<item name="account" xsi:type="const">Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT</item>
52+
<item name="credit_card" xsi:type="const">Magento\Vault\Api\Data\PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD</item>
53+
</argument>
54+
</arguments>
55+
</type>
4656
</config>

0 commit comments

Comments
 (0)