|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Spryker Commerce OS. |
|
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types = 1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Pyz\Zed\DataImport\Business\Model\MerchantUser; |
|
11
|
|
|
|
|
12
|
|
|
use Generated\Shared\Transfer\MerchantUserCriteriaTransfer; |
|
13
|
|
|
use Generated\Shared\Transfer\MerchantUserTransfer; |
|
14
|
|
|
use Generated\Shared\Transfer\UserCriteriaTransfer; |
|
15
|
|
|
use Orm\Zed\Merchant\Persistence\SpyMerchantQuery; |
|
16
|
|
|
use Orm\Zed\User\Persistence\SpyUserQuery; |
|
17
|
|
|
use Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException; |
|
18
|
|
|
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface; |
|
19
|
|
|
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface; |
|
20
|
|
|
use Spryker\Zed\MerchantUser\Business\MerchantUserFacadeInterface; |
|
21
|
|
|
|
|
22
|
|
|
class MerchantUserWriterStep implements DataImportStepInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected const MERCHANT_REFERENCE = 'merchant_reference'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected const USERNAME = 'username'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \Spryker\Zed\MerchantUser\Business\MerchantUserFacadeInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $merchantUserFacade; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \Spryker\Zed\MerchantUser\Business\MerchantUserFacadeInterface $merchantUserFacade |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(MerchantUserFacadeInterface $merchantUserFacade) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->merchantUserFacade = $merchantUserFacade; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
*/ |
|
50
|
|
|
public function execute(DataSetInterface $dataSet): void |
|
51
|
|
|
{ |
|
52
|
|
|
$idMerchant = $this->getIdMerchantByReference($dataSet[static::MERCHANT_REFERENCE]); |
|
53
|
|
|
$idUser = $this->getIdUserByUsername($dataSet[static::USERNAME]); |
|
54
|
|
|
|
|
55
|
|
|
$merchantUserTransfer = $this->merchantUserFacade->findMerchantUser( |
|
56
|
|
|
(new MerchantUserCriteriaTransfer()) |
|
57
|
|
|
->setIdUser($idUser) |
|
58
|
|
|
->setIdMerchant($idMerchant), |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
if ($merchantUserTransfer) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$userTransfer = $this->merchantUserFacade->findUser( |
|
|
|
|
|
|
66
|
|
|
(new UserCriteriaTransfer())->setIdUser($idUser), |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->merchantUserFacade->createMerchantUser( |
|
70
|
|
|
(new MerchantUserTransfer()) |
|
71
|
|
|
->setIdMerchant($idMerchant) |
|
72
|
|
|
->setUser($userTransfer), |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $merchantReference |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException |
|
80
|
|
|
* |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getIdMerchantByReference(string $merchantReference): int |
|
84
|
|
|
{ |
|
85
|
|
|
$merchantEntity = SpyMerchantQuery::create() |
|
86
|
|
|
->findOneByMerchantReference($merchantReference); |
|
87
|
|
|
|
|
88
|
|
|
if (!$merchantEntity) { |
|
89
|
|
|
throw new EntityNotFoundException(sprintf('Merchant with reference "%s" is not found.', $merchantReference)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $merchantEntity->getIdMerchant(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $username |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException |
|
99
|
|
|
* |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getIdUserByUsername(string $username): int |
|
103
|
|
|
{ |
|
104
|
|
|
$userEntity = SpyUserQuery::create() |
|
105
|
|
|
->findOneByUsername($username); |
|
106
|
|
|
|
|
107
|
|
|
if (!$userEntity) { |
|
108
|
|
|
throw new EntityNotFoundException(sprintf('User with username "%s" is not found.', $username)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $userEntity->getIdUser(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.