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

writeCollectionBySspAssetToCompanyBusinessUnitEvents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 10
c 1
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
declare(strict_types=1);
9
10
namespace SprykerFeature\Zed\SelfServicePortal\Business\Asset\Writer;
11
12
use Generated\Shared\Transfer\SspAssetConditionsTransfer;
13
use Generated\Shared\Transfer\SspAssetCriteriaTransfer;
14
use Generated\Shared\Transfer\SspAssetIncludeTransfer;
15
use Generated\Shared\Transfer\SspAssetSearchCollectionTransfer;
16
use Spryker\Zed\EventBehavior\Business\EventBehaviorFacadeInterface;
17
use SprykerFeature\Zed\SelfServicePortal\Business\Asset\Mapper\SspAssetSearchMapperInterface as MapperSspAssetSearchMapperInterface;
18
use SprykerFeature\Zed\SelfServicePortal\Business\Asset\Reader\SspAssetReaderInterface;
19
use SprykerFeature\Zed\SelfServicePortal\Persistence\SelfServicePortalEntityManagerInterface;
20
21
class SspAssetSearchWriter implements SspAssetSearchWriterInterface
22
{
23
    /**
24
     * @uses \Orm\Zed\SelfServicePortal\Persistence\Map\SpySspAssetToCompanyBusinessUnitTableMap::COL_FK_SSP_ASSET
25
     *
26
     * @var string
27
     */
28
    protected const COL_FK_SSP_ASSET_TO_COMPANY_BUSINESS_UNIT = 'spy_ssp_asset_to_company_business_unit.fk_ssp_asset';
29
30
    /**
31
     * @uses \Orm\Zed\SelfServicePortal\Persistence\Map\SpySspAssetToSspModelTableMap::COL_FK_SSP_ASSET
32
     *
33
     * @var string
34
     */
35
    protected const COL_FK_SSP_ASSET_TO_MODEL = 'spy_ssp_asset_to_ssp_model.fk_ssp_asset';
36
37
    public function __construct(
38
        protected SspAssetReaderInterface $sspAssetReader,
39
        protected EventBehaviorFacadeInterface $eventBehaviorFacade,
40
        protected MapperSspAssetSearchMapperInterface $sspAssetSearchMapper,
41
        protected SelfServicePortalEntityManagerInterface $selfServicePortalEntityManager
42
    ) {
43
    }
44
45
    /**
46
     * @param array<\Generated\Shared\Transfer\EventEntityTransfer> $eventTransfers
47
     *
48
     * @return void
49
     */
50
    public function writeCollectionBySspAssetEvents(array $eventTransfers): void
51
    {
52
        $sspAssetIds = $this->eventBehaviorFacade->getEventTransferIds($eventTransfers);
53
        $this->writeCollectionBySspAssetIds($sspAssetIds);
54
    }
55
56
    /**
57
     * @param list<\Generated\Shared\Transfer\EventEntityTransfer> $eventEntityTransfers
58
     *
59
     * @return void
60
     */
61
    public function writeCollectionBySspAssetToCompanyBusinessUnitEvents(array $eventEntityTransfers): void
62
    {
63
        $sspAssetIds = $this->eventBehaviorFacade->getEventTransferForeignKeys(
64
            $eventEntityTransfers,
65
            static::COL_FK_SSP_ASSET_TO_COMPANY_BUSINESS_UNIT,
66
        );
67
68
        if ($sspAssetIds === []) {
69
            return;
70
        }
71
72
        $this->writeCollectionBySspAssetIds($sspAssetIds);
73
    }
74
75
    /**
76
     * @param list<\Generated\Shared\Transfer\EventEntityTransfer> $eventEntityTransfers
77
     *
78
     * @return void
79
     */
80
    public function writeCollectionBySspAssetToModelEvents(array $eventEntityTransfers): void
81
    {
82
        $sspAssetIds = $this->eventBehaviorFacade->getEventTransferForeignKeys(
83
            $eventEntityTransfers,
84
            static::COL_FK_SSP_ASSET_TO_MODEL,
85
        );
86
87
        if ($sspAssetIds === []) {
88
            return;
89
        }
90
91
        $this->writeCollectionBySspAssetIds($sspAssetIds);
92
    }
93
94
    /**
95
     * @param array<int> $sspAssetIds
96
     *
97
     * @return void
98
     */
99
    protected function writeCollectionBySspAssetIds(array $sspAssetIds): void
100
    {
101
        if (!$sspAssetIds) {
102
            return;
103
        }
104
105
        $sspAssetCriteriaTransfer = $this->createSspAssetCriteriaTransfer($sspAssetIds);
106
        $sspAssetCollectionTransfer = $this->sspAssetReader->getSspAssetCollection($sspAssetCriteriaTransfer);
107
108
        $sspAssetSearchCollectionTransfer = $this->sspAssetSearchMapper
109
            ->mapSspAssetCollectionTransferToSspAssetSearchCollectionTransfer(
110
                $sspAssetCollectionTransfer,
111
                new SspAssetSearchCollectionTransfer(),
112
            );
113
114
        $foundAssetIds = [];
115
        foreach ($sspAssetSearchCollectionTransfer->getSspAssets() as $sspAssetSearchTransfer) {
116
            $foundAssetIds[] = $sspAssetSearchTransfer->getIdSspAsset();
117
            $this->selfServicePortalEntityManager->saveSspAssetSearch($sspAssetSearchTransfer);
118
        }
119
120
        $notFoundAssetIds = array_diff($sspAssetIds, $foundAssetIds);
121
122
        if (!$notFoundAssetIds) {
123
            return;
124
        }
125
126
        $this->selfServicePortalEntityManager->deleteSspAssetSearchBySspAssetIds($notFoundAssetIds);
127
    }
128
129
    /**
130
     * @param list<int> $sspAssetIds
131
     *
132
     * @return \Generated\Shared\Transfer\SspAssetCriteriaTransfer
133
     */
134
    protected function createSspAssetCriteriaTransfer(array $sspAssetIds): SspAssetCriteriaTransfer
135
    {
136
        $sspAssetConditionsTransfer = (new SspAssetConditionsTransfer())
137
            ->setSspAssetIds($sspAssetIds);
138
139
        $includeTransfer = (new SspAssetIncludeTransfer())
140
            ->setWithSspModels(true)
141
            ->setWithAssignedBusinessUnits(true);
142
143
        return (new SspAssetCriteriaTransfer())
144
            ->setSspAssetConditions($sspAssetConditionsTransfer)
145
            ->setInclude($includeTransfer);
146
    }
147
}
148