Passed
Push — master ( cacbf1...74b6c9 )
by
unknown
34:14
created

triggerAssetPublishEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerFeature\Zed\SelfServicePortal\Business\SspModel\DataImport\Step;
9
10
use Generated\Shared\Transfer\EventEntityTransfer;
11
use Orm\Zed\SelfServicePortal\Persistence\SpySspAssetQuery;
12
use Orm\Zed\SelfServicePortal\Persistence\SpySspAssetToSspModelQuery;
13
use Orm\Zed\SelfServicePortal\Persistence\SpySspModelQuery;
14
use Spryker\Zed\DataImport\Business\Exception\EntityNotFoundException;
15
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
16
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
17
use Spryker\Zed\Event\Business\EventFacadeInterface;
18
use SprykerFeature\Shared\SelfServicePortal\SelfServicePortalConfig as SharedSelfServicePortalConfig;
19
use SprykerFeature\Zed\SelfServicePortal\Business\SspModel\DataImport\DataSet\SspAssetModelDataSetInterface;
20
21
class SspModelAssetWriterStep implements DataImportStepInterface
22
{
23
    public function __construct(protected EventFacadeInterface $eventFacade)
24
    {
25
    }
26
27
    public function execute(DataSetInterface $dataSet): void
28
    {
29
        $assetReference = $dataSet[SspAssetModelDataSetInterface::COLUMN_ASSET_REFERENCE];
30
        $modelReference = $dataSet[SspAssetModelDataSetInterface::COLUMN_MODEL_REFERENCE];
31
32
        $sspAssetEntity = SpySspAssetQuery::create()
33
            ->filterByReference($assetReference)
34
            ->findOne();
35
36
        if (!$sspAssetEntity) {
37
            throw new EntityNotFoundException($assetReference);
38
        }
39
40
        $sspModelEntity = SpySspModelQuery::create()
41
            ->filterByReference($modelReference)
42
            ->findOne();
43
44
        if (!$sspModelEntity) {
45
            throw new EntityNotFoundException($modelReference);
46
        }
47
48
        $sspAssetModelEntity = SpySspAssetToSspModelQuery::create()
49
            ->filterByFkSspAsset($sspAssetEntity->getIdSspAsset())
50
            ->filterByFkSspModel($sspModelEntity->getIdSspModel())
51
            ->findOneOrCreate();
52
53
        if ($sspAssetModelEntity->isNew()) {
54
            $sspAssetModelEntity->save();
55
        }
56
57
        $this->triggerAssetPublishEvent($sspAssetEntity->getIdSspAsset());
58
        $this->triggerModelPublishEvent($sspModelEntity->getIdSspModel());
59
    }
60
61
    protected function triggerAssetPublishEvent(int $idSspAsset): void
62
    {
63
        $eventEntityTransfer = new EventEntityTransfer();
64
        $eventEntityTransfer->setId($idSspAsset);
65
66
        $this->eventFacade->trigger(SharedSelfServicePortalConfig::SSP_ASSET_PUBLISH, $eventEntityTransfer);
67
    }
68
69
    protected function triggerModelPublishEvent(int $idSspModel): void
70
    {
71
        $eventEntityTransfer = new EventEntityTransfer();
72
        $eventEntityTransfer->setId($idSspModel);
73
74
        $this->eventFacade->trigger(SharedSelfServicePortalConfig::SSP_MODEL_PUBLISH, $eventEntityTransfer);
75
    }
76
}
77