Completed
Push — 2.x ( 48ed54...fe246a )
by Tim
12s queued 10s
created

TierPriceObserver::setLastEntityId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Observers\TierPriceUpdateObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Klaas-Tido Rühl <[email protected]>
15
 * @author    Tim Wagner <[email protected]>
16
 * @copyright 2019 REFUSiON GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-tier-price
19
 * @link      https://www.techdivision.com
20
 * @link      https://www.refusion.com
21
 */
22
23
namespace TechDivision\Import\Product\TierPrice\Observers;
24
25
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface;
26
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface;
27
28
/**
29
 * Observer for creating/updating/deleting tier prices from the database.
30
 *
31
 * @author    Klaas-Tido Rühl <[email protected]>
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 REFUSiON GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-product-tier-price
36
 * @link      https://www.techdivision.com
37
 * @link      https://www.refusion.com
38
 */
39
class TierPriceObserver extends AbstractProductTierPriceObserver
40
{
41
42
    /**
43
     * The trait that prepares the tier price data.
44
     *
45
     * @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait
46
     */
47
    use PrepareTierPriceTrait;
48
49
    /**
50
     * The available tier price value types.
51
     *
52
     * @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface
53
     */
54
    protected $valueTypes;
55
56
    /**
57
     * Initialize the observer with the passed product tier price processor instance.
58
     *
59
     * @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor The processor instance
60
     * @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface            $valueTypes         The tier price value types
61
     */
62
    public function __construct(TierPriceProcessorInterface $tierPriceProcessor, ValueTypesInterface $valueTypes)
63
    {
64
65
        // pass the tier price processor through to the parent instance
66
        parent::__construct($tierPriceProcessor);
67
68
        // set the value types
69
        $this->valueTypes = $valueTypes;
70
    }
71
72
    /**
73
     * Returns the tier price value types.
74
     *
75
     * @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types
76
     */
77
    protected function getValueTypes()
78
    {
79
        return $this->valueTypes;
80
    }
81
82
    /**
83
     * Process the observer's business logic.
84
     *
85
     * @return void
86
     * @throws \Exception
87
     */
88
    protected function process()
89
    {
90
91
        // intialize the tier price data
92
        $tierPrice = $this->initializeTierPrice($this->prepareAttributes());
93
94
        // persist the tier price and mark it as processed
95
        $this->addProcessedTierPrice(
96
            $this->persistTierPrice($tierPrice),
0 ignored issues
show
Bug introduced by
$this->persistTierPrice($tierPrice) of type string is incompatible with the type integer expected by parameter $valueId of TechDivision\Import\Prod...addProcessedTierPrice(). ( Ignorable by Annotation )

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

96
            /** @scrutinizer ignore-type */ $this->persistTierPrice($tierPrice),
Loading history...
97
            $tierPrice[$this->getPrimaryKeyMemberName()]
98
        );
99
    }
100
101
    /**s
102
     * Initialize the product website with the passed attributes and returns an instance.
103
     *
104
     * @param array $attr The product website attributes
105
     *
106
     * @return array The initialized product website
107
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
108
     */
109
    protected function initializeTierPrice(array $attr)
110
    {
111
        return $attr;
112
    }
113
114
    /**
115
     * Loads and returns the product with the passed SKU.
116
     *
117
     * @param string $sku The SKU of the product to load
118
     *
119
     * @return array The product
120
     */
121
    protected function loadProduct($sku)
122
    {
123
        return $this->getTierPriceProcessor()->loadProduct($sku);
0 ignored issues
show
Bug introduced by
The method loadProduct() does not exist on TechDivision\Import\Prod...PriceProcessorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to TechDivision\Import\Prod...PriceProcessorInterface. ( Ignorable by Annotation )

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

123
        return $this->getTierPriceProcessor()->/** @scrutinizer ignore-call */ loadProduct($sku);
Loading history...
124
    }
125
126
    /**
127
     * Persists the tier price with the passed data.
128
     *
129
     * @param array       $row  The tier price to persist
130
     * @param string|null $name The name of the prepared statement that has to be executed
131
     *
132
     * @return string The ID of the persisted entity
133
     */
134
    protected function persistTierPrice(array $row, $name = null)
135
    {
136
        return $this->getTierPriceProcessor()->persistTierPrice($row, $name);
137
    }
138
139
    /**
140
     * Add the ID of the processed tier price.
141
     *
142
     * @param integer $valueId  The ID of the processed tier price
143
     * @param integer $entityId The entity ID of the related product
144
     *
145
     * @return void
146
     */
147
    protected function addProcessedTierPrice($valueId, $entityId)
148
    {
149
        $this->getSubject()->addProcessedTierPrice($valueId, $entityId);
150
    }
151
152
    /**
153
     * Returns the customer group ID for the given code, if it exists.
154
     *
155
     * @param string $code The code of the requested customer group
156
     *
157
     * @return integer|null The ID of the customer group
158
     */
159
    protected function getCustomerGroupIdByCode($code)
160
    {
161
        return $this->getSubject()->getCustomerGroupIdByCode($code);
162
    }
163
164
    /**
165
     * Return's the store website for the passed code.
166
     *
167
     * @param string $code The code of the store website to return the ID for
168
     *
169
     * @return integer The store website ID
170
     * @throws \Exception Is thrown, if the store website with the requested code is not available
171
     */
172
    protected function getStoreWebsiteIdByCode($code)
173
    {
174
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
175
    }
176
177
    /**
178
     * Query whether or not the passed value type is valid.
179
     *
180
     * @param string $valueType The value type to query for
181
     *
182
     * @return boolean TRUE if the value type is valid, else FALSE
183
     */
184
    protected function isValueType($valueType)
185
    {
186
        return $this->getValueTypes()->isValueType($valueType);
187
    }
188
189
    /**
190
     * Queries whether or not the passed customer group code matches all groups or not.
191
     *
192
     * @param string $code The customer group code to query for
193
     *
194
     * @return boolean TRUE if the customer group code matches, else FALSE
195
     */
196
    protected function isAllGroups($code)
197
    {
198
        return $this->getSubject()->isAllGroups($code);
199
    }
200
}
201