AbstractMsiSubject   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 189
ccs 0
cts 35
cp 0
rs 10
wmc 11

10 Methods

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