Passed
Push — master ( 564b7e...61dc89 )
by Tim
06:19 queued 11s
created

AbstractMsiSubject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Subjects\AbstractMsiSubject
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    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-msi
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Msi\Subjects;
22
23
use TechDivision\Import\Subjects\AbstractSubject;
24
use TechDivision\Import\Product\Msi\Utils\RegistryKeys;
25
use TechDivision\Import\Services\RegistryProcessorInterface;
26
use Doctrine\Common\Collections\Collection;
27
use League\Event\EmitterInterface;
28
use TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface;
29
use TechDivision\Import\Utils\Generators\GeneratorInterface;
30
31
/**
32
 * The abstract subject implementation that provides basic MSI inventory source item
33
 * handling business logic.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2018 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-product-msi
39
 * @link      http://www.techdivision.com
40
 */
41
abstract class AbstractMsiSubject extends AbstractSubject
42
{
43
44
    /**
45
     * The ID of the source item that has been created recently.
46
     *
47
     * @var string
48
     */
49
    protected $lastSourceItemId;
50
51
    /**
52
     * The mapping for the SKU/source code to the created source item IDs.
53
     *
54
     * @var array
55
     */
56
    protected $skuSourceItemIdMapping = array();
57
58
    /**
59
     * The available inventory sources.
60
     *
61
     * @var array
62
     */
63
    protected $inventorySources = array();
64
65
    /**
66
     * The MSI bunch processor instance.
67
     *
68
     * @var \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface
69
     */
70
    protected $msiBunchProcessor;
71
72
    /**
73
     * Initialize the subject instance.
74
     *
75
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor          The registry processor instance
76
     * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data
77
     * @param \Doctrine\Common\Collections\Collection                  $systemLoggers              The array with the system loggers instances
78
     * @param \League\Event\EmitterInterface                           $emitter                    The event emitter instance
79
     */
80
    public function __construct(
81
        RegistryProcessorInterface $registryProcessor,
82
        GeneratorInterface $coreConfigDataUidGenerator,
83
        Collection $systemLoggers,
84
        EmitterInterface $emitter,
85
        MsiBunchProcessorInterface $msiBunchProcessor
86
    ) {
87
88
        // set the MSI bunch processor instance
89
        $this->msiBunchProcessor = $msiBunchProcessor;
90
91
        // pass the instances through to the parent instance
92
        parent::__construct($registryProcessor, $coreConfigDataUidGenerator, $systemLoggers, $emitter);
93
    }
94
95
    /**
96
     * Returns the MSI bunch processor instance.
97
     *
98
     * @return \TechDivision\Import\Product\Msi\Services\MsiBunchProcessorInterface The MSI bunch processor instance
99
     */
100
    protected function getMsiBunchProcessor()
101
    {
102
        return $this->msiBunchProcessor;
103
    }
104
105
    /**
106
     * Set's the ID of the source item that has been created recently.
107
     *
108
     * @param string $lastSourceItemId The entity ID
109
     *
110
     * @return void
111
     */
112
    public function setLastSourceItemId($lastSourceItemId)
113
    {
114
        $this->lastSourceItemId = $lastSourceItemId;
115
    }
116
117
    /**
118
     * Return's the ID of the source item that has been created recently.
119
     *
120
     * @return string The entity Id
121
     */
122
    public function getLastSourceItemId()
123
    {
124
        return $this->lastSourceItemId;
125
    }
126
127
    /**
128
     * Queries whether or not the SKU/source code has already been processed.
129
     *
130
     * @param string $sku        The SKU to check been processed
131
     * @param string $sourceCode The source code to check been processed
132
     *
133
     * @return boolean TRUE if the SKU/source code has been processed, else FALSE
134
     */
135
    public function hasBeenProcessed($sku, $sourceCode)
136
    {
137
        return isset($this->skuSourceItemIdMapping[$sku][$sourceCode]);
138
    }
139
140
    /**
141
     * Add the passed SKU/source code => source item ID mapping.
142
     *
143
     * @param string $sku        The SKU
144
     * @param string $sourceCode The source code
145
     *
146
     * @return void
147
     */
148
    public function addSkuSourceItemIdMapping($sku, $sourceCode)
149
    {
150
        $this->skuSourceItemIdMapping[$sku][$sourceCode] = $this->getLastSourceItemId();
151
    }
152
153
    /**
154
     * Queries whether or not the inventory source with the passed code is available.
155
     *
156
     * @param string $sourceCode The code of the inventory source to query for
157
     *
158
     * @return boolean TRUE if the inventory source with the passed code is available, FALSE if not
159
     */
160
    public function hasInventorySource($sourceCode)
161
    {
162
        return isset($this->inventorySources[$sourceCode]);
163
    }
164
165
    /**
166
     * Returns the inventory source with the passed code.
167
     *
168
     * @param string $sourceCode The code of the inventory source to return
169
     *
170
     * @return array The inventory source for the passed code
171
     * @throws \Exception Is thrown, if the inventory source with the passed code is NOT available
172
     */
173
    public function getInventorySourceBySourceCode($sourceCode)
174
    {
175
176
        // query whether or not the inventory source is available
177
        if (isset($this->inventorySources[$sourceCode])) {
178
            return $this->inventorySources[$sourceCode];
179
        }
180
181
        // throw a new exception
182
        throw new \Exception(
183
            $this->appendExceptionSuffix(
184
                sprintf('Can\'t load inventory source with code "%s"', $sourceCode)
185
            )
186
        );
187
    }
188
189
    /**
190
     * Intializes the previously loaded global data for exactly one bunch.
191
     *
192
     * @param string $serial The serial of the actual import
193
     *
194
     * @return void
195
     */
196
    public function setUp($serial)
197
    {
198
199
        // invoke the parent method
200
        parent::setUp($serial);
201
202
        // load the available inventory sources
203
        $this->inventorySources = $this->getMsiBunchProcessor()->loadInventorySources();
204
    }
205
206
    /**
207
     * Clean up the global data after importing the bunch.
208
     *
209
     * @param string $serial The serial of the actual import
210
     *
211
     * @return void
212
     */
213
    public function tearDown($serial)
214
    {
215
216
        // load the registry processor
217
        $registryProcessor = $this->getRegistryProcessor();
218
219
        // update the status
220
        $registryProcessor->mergeAttributesRecursive(
221
            $serial,
222
            array(
223
                RegistryKeys::SKU_SOURCE_ITEM_ID_MAPPING => $this->skuSourceItemIdMapping
224
            )
225
        );
226
227
        // invoke the parent method
228
        parent::tearDown($serial);
229
    }
230
}
231