Issues (3412)

LocalizedAttributesCollectionWriter.php (1 issue)

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 Spryker\Zed\ProductLabel\Business\Label\LocalizedAttributesCollection;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer;
12
use Orm\Zed\ProductLabel\Persistence\SpyProductLabelLocalizedAttributes;
13
use Spryker\Zed\ProductLabel\Business\Touch\LabelDictionaryTouchManagerInterface;
14
use Spryker\Zed\ProductLabel\Persistence\ProductLabelQueryContainerInterface;
15
use Spryker\Zed\PropelOrm\Business\Transaction\DatabaseTransactionHandlerTrait;
16
17
class LocalizedAttributesCollectionWriter implements LocalizedAttributesCollectionWriterInterface
18
{
19
    use DatabaseTransactionHandlerTrait;
20
21
    /**
22
     * @var \Spryker\Zed\ProductLabel\Persistence\ProductLabelQueryContainerInterface
23
     */
24
    protected $queryContainer;
25
26
    /**
27
     * @var \Spryker\Zed\ProductLabel\Business\Touch\LabelDictionaryTouchManagerInterface
28
     */
29
    protected $dictionaryTouchManager;
30
31
    /**
32
     * @param \Spryker\Zed\ProductLabel\Persistence\ProductLabelQueryContainerInterface $queryContainer
33
     * @param \Spryker\Zed\ProductLabel\Business\Touch\LabelDictionaryTouchManagerInterface $dictionaryTouchManager
34
     */
35
    public function __construct(
36
        ProductLabelQueryContainerInterface $queryContainer,
37
        LabelDictionaryTouchManagerInterface $dictionaryTouchManager
38
    ) {
39
        $this->queryContainer = $queryContainer;
40
        $this->dictionaryTouchManager = $dictionaryTouchManager;
41
    }
42
43
    /**
44
     * @param \ArrayObject<int, \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer> $localizedAttributesTransferCollection
45
     *
46
     * @return void
47
     */
48
    public function save(ArrayObject $localizedAttributesTransferCollection)
49
    {
50
        $this->handleDatabaseTransaction(function () use ($localizedAttributesTransferCollection) {
51
            $this->executeSetTransaction($localizedAttributesTransferCollection);
52
        });
53
    }
54
55
    /**
56
     * @param \ArrayObject<int, \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer> $localizedAttributesTransferCollection
57
     *
58
     * @return void
59
     */
60
    protected function executeSetTransaction(ArrayObject $localizedAttributesTransferCollection)
61
    {
62
        $hasModified = false;
63
64
        foreach ($localizedAttributesTransferCollection as $localizedAttributesTransfer) {
65
            $this->assertLocalizedAttributes($localizedAttributesTransfer);
66
            $hasModified |= $this->persistLocalizedAttributes($localizedAttributesTransfer);
67
        }
68
69
        if ($hasModified) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hasModified of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
70
            $this->touchDictionary();
71
        }
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer
76
     *
77
     * @return void
78
     */
79
    protected function assertLocalizedAttributes(ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer)
80
    {
81
        $localizedAttributesTransfer
82
            ->requireFkLocale()
83
            ->requireFkProductLabel();
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer
88
     *
89
     * @return bool
90
     */
91
    protected function persistLocalizedAttributes(ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer)
92
    {
93
        $localizedAttributesEntity = $this->findOrCreateEntity($localizedAttributesTransfer);
94
        $this->updateEntityFromTransfer($localizedAttributesEntity, $localizedAttributesTransfer);
95
96
        if (!$localizedAttributesEntity->isModified()) {
97
            return false;
98
        }
99
100
        $localizedAttributesEntity->save();
101
        $this->updateTransferFromEntity($localizedAttributesTransfer, $localizedAttributesEntity);
102
103
        return true;
104
    }
105
106
    /**
107
     * @param \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer
108
     *
109
     * @return \Orm\Zed\ProductLabel\Persistence\SpyProductLabelLocalizedAttributes
110
     */
111
    protected function findOrCreateEntity(ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer)
112
    {
113
        $localizedAttributesEntity = $this
114
            ->queryContainer
115
            ->queryLocalizedAttributesByIdProductLabelAndIdLocale(
116
                $localizedAttributesTransfer->getFkProductLabel(),
117
                $localizedAttributesTransfer->getFkLocale(),
118
            )
119
            ->findOneOrCreate();
120
121
        return $localizedAttributesEntity;
122
    }
123
124
    /**
125
     * @return void
126
     */
127
    protected function touchDictionary()
128
    {
129
        $this->dictionaryTouchManager->touchActive();
130
    }
131
132
    /**
133
     * @param \Orm\Zed\ProductLabel\Persistence\SpyProductLabelLocalizedAttributes $localizedAttributesEntity
134
     * @param \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer
135
     *
136
     * @return void
137
     */
138
    protected function updateEntityFromTransfer(
139
        SpyProductLabelLocalizedAttributes $localizedAttributesEntity,
140
        ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer
141
    ) {
142
        $localizedAttributesEntity->fromArray($localizedAttributesTransfer->toArray());
143
    }
144
145
    /**
146
     * @param \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer
147
     * @param \Orm\Zed\ProductLabel\Persistence\SpyProductLabelLocalizedAttributes $localizedAttributesEntity
148
     *
149
     * @return void
150
     */
151
    protected function updateTransferFromEntity(
152
        ProductLabelLocalizedAttributesTransfer $localizedAttributesTransfer,
153
        SpyProductLabelLocalizedAttributes $localizedAttributesEntity
154
    ) {
155
        $localizedAttributesTransfer->fromArray($localizedAttributesEntity->toArray(), true);
156
    }
157
}
158