MerchantUserWriterStep::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
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(
0 ignored issues
show
Deprecated Code introduced by
The function Spryker\Zed\MerchantUser...deInterface::findUser() has been deprecated: Use {@link \Spryker\Zed\MerchantUser\Business\MerchantUserFacadeInterface::getUserCollection()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
        $userTransfer = /** @scrutinizer ignore-deprecated */ $this->merchantUserFacade->findUser(

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.

Loading history...
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