Passed
Pull Request — master (#809)
by Denys
06:35
created

SspAssetsMapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 17
dl 0
loc 39
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findSspAssetByReference() 0 13 2
A __construct() 0 2 1
A mapGlueRequestToSspAssetCollectionRequestTransferForUpdate() 0 18 2
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\Glue\SelfServicePortal\Processor\BackendApi\Mapper;
11
12
use Generated\Shared\Transfer\GlueRequestTransfer;
13
use Generated\Shared\Transfer\SspAssetCollectionRequestTransfer;
14
use Generated\Shared\Transfer\SspAssetConditionsTransfer;
15
use Generated\Shared\Transfer\SspAssetCriteriaTransfer;
16
use Generated\Shared\Transfer\SspAssetTransfer;
17
use SprykerFeature\Glue\SelfServicePortal\Processor\BackendApi\Mapper\SspAssetsMapper as SprykerSspAssetMapper;
18
use SprykerFeature\Zed\SelfServicePortal\Business\SelfServicePortalFacadeInterface;
19
20
class SspAssetsMapper extends SprykerSspAssetMapper
21
{
22
    public function __construct(protected SelfServicePortalFacadeInterface $selfServicePortalFacade)
23
    {
24
    }
25
26
    public function mapGlueRequestToSspAssetCollectionRequestTransferForUpdate(GlueRequestTransfer $glueRequestTransfer): SspAssetCollectionRequestTransfer
27
    {
28
        /** @var \Generated\Shared\Transfer\SspAssetsBackendApiAttributesTransfer $sspAssetsBackendApiAttributesTransfer */
29
        $sspAssetsBackendApiAttributesTransfer = $glueRequestTransfer->getResourceOrFail()->getAttributes();
30
31
        $sspAssetTransfer = $this->findSspAssetByReference($glueRequestTransfer->getResourceOrFail()->getId());
32
33
        if (!$sspAssetTransfer) {
34
            return (new SspAssetCollectionRequestTransfer());
35
        }
36
37
        $sspAssetTransfer
38
            ->setName($sspAssetsBackendApiAttributesTransfer->getName())
39
            ->setSerialNumber($sspAssetsBackendApiAttributesTransfer->getSerialNumber())
40
            ->setNote($sspAssetsBackendApiAttributesTransfer->getNote())
41
            ->setExternalImageUrl($sspAssetsBackendApiAttributesTransfer->getExternalImageUrl());
42
43
        return (new SspAssetCollectionRequestTransfer())->addSspAsset($sspAssetTransfer);
44
    }
45
46
    protected function findSspAssetByReference(string $assetReference): ?SspAssetTransfer
47
    {
48
        $sspAssetCollectionTransfer = $this->selfServicePortalFacade->getSspAssetCollection(
49
            (new SspAssetCriteriaTransfer())->setSspAssetConditions(
50
                (new SspAssetConditionsTransfer())->setReferences([$assetReference]),
51
            ),
52
        );
53
54
        if ($sspAssetCollectionTransfer->getSspAssets()->count() === 0) {
55
            return null;
56
        }
57
58
        return $sspAssetCollectionTransfer->getSspAssets()->getIterator()->current();
59
    }
60
}
61