Completed
Push — master ( 3579df...802965 )
by Tim
17:41 queued 16:18
created

EeBunchSubject::isUrlKeyOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Category\Ee\Subjects\EeBunchSubject
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 2019 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-category-ee
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Category\Ee\Subjects;
22
23
use TechDivision\Import\Category\Utils\RegistryKeys;
24
use TechDivision\Import\Category\Subjects\BunchSubject;
25
use TechDivision\Import\Category\Ee\Utils\MemberNames;
26
27
/**
28
 * A SLSB that handles the process to import category bunches.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-category-ee
34
 * @link      http://www.techdivision.com
35
 */
36
class EeBunchSubject extends BunchSubject
37
{
38
39
    /**
40
     * The row ID of the product that has been created recently.
41
     *
42
     * @var integer
43
     */
44
    protected $lastRowId;
45
46
    /**
47
     * The mapping for the paths to the created row IDs.
48
     *
49
     * @var array
50
     */
51
    protected $pathRowIdMapping = array();
52
53
    /**
54
     * Intializes the previously loaded global data for exactly one bunch.
55
     *
56
     * @param string $serial The serial of the actual import
57
     *
58
     * @return void
59
     */
60
    public function setUp($serial)
61
    {
62
63
        // prepare the callbacks
64
        parent::setUp($serial);
65
66
        // load the status of the actual import
67
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
68
69
        // load the available category path => row ID mappings
70
        foreach ($this->categories as $resolvedPath => $category) {
71
            $this->pathRowIdMapping[$this->unifyPath($resolvedPath)] = $category[MemberNames::ROW_ID];
72
        }
73
74
        // load the category path => row ID mappings from the previous subject
75
        if (isset($status[RegistryKeys::PATH_ROW_ID_MAPPING])) {
76
            $this->pathRowIdMapping = array_merge($this->pathRowIdMapping, $status[RegistryKeys::PATH_ROW_ID_MAPPING]);
77
        }
78
    }
79
80
    /**
81
     * Clean up the global data after importing the variants.
82
     *
83
     * @param string $serial The serial of the actual import
84
     *
85
     * @return void
86
     */
87
    public function tearDown($serial)
88
    {
89
90
        // load the registry processor
91
        $registryProcessor = $this->getRegistryProcessor();
92
93
        // update the status with the actual path => row ID mappings
94
        $registryProcessor->mergeAttributesRecursive(
95
            RegistryKeys::STATUS,
96
            array(
97
                RegistryKeys::PATH_ROW_ID_MAPPING => $this->pathRowIdMapping
98
            )
99
        );
100
101
        // invoke the parent method
102
        parent::tearDown($serial);
103
    }
104
105
    /**
106
     * Set's the row ID of the product that has been created recently.
107
     *
108
     * @param string $lastRowId The row ID
109
     *
110
     * @return void
111
     */
112 1
    public function setLastRowId($lastRowId)
113
    {
114 1
        $this->lastRowId = $lastRowId;
0 ignored issues
show
Documentation Bug introduced by
The property $lastRowId was declared of type integer, but $lastRowId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
115 1
    }
116
117
    /**
118
     * Return's the row ID of the product that has been created recently.
119
     *
120
     * @return string The row Id
121
     */
122 1
    public function getLastRowId()
123
    {
124 1
        return $this->lastRowId;
125
    }
126
127
    /**
128
     * Add the passed path => row ID mapping.
129
     *
130
     * @param string $path The path
131
     *
132
     * @return void
133
     */
134
    public function addPathRowIdMapping($path)
135
    {
136
        $this->pathRowIdMapping[$this->unifyPath($path)] = $this->getLastRowId();
137
    }
138
139
    /**
140
     * Removes the mapping, e. g. when a category has been deleted.
141
     *
142
     * @param string $path The path to delete the mapping for
143
     *
144
     * @return void
145
     */
146
    public function removePathRowIdMapping($path)
147
    {
148
        unset($this->pathRowIdMapping[$this->unifyPath($path)]);
149
    }
150
151
    /**
152
     * Return's the entity ID for the passed path.
153
     *
154
     * @param string $path The path to return the entity ID for
155
     *
156
     * @return integer The mapped entity ID
157
     * @throws \Exception Is thrown, if the path can not be mapped
158
     */
159
    public function mapPathRowId($path)
160
    {
161
162
        // query whether or not a entity ID for the passed path has been mapped
163
        if (isset($this->pathRowIdMapping[$unifiedPath = $this->unifyPath($path)])) {
164
            return $this->pathRowIdMapping[$unifiedPath];
165
        }
166
167
        // throw an exception if not
168
        throw new \Exception(
169
            $this->appendExceptionSuffix(
170
                sprintf('Can\'t map path %s to any row ID', $path)
171
            )
172
        );
173
    }
174
175
    /**
176
     * Return's the PK column name to create the product => attribute relation.
177
     *
178
     * @return string The PK column name
179
     */
180
    protected function getPrimaryKeyMemberName()
181
    {
182
        return MemberNames::ROW_ID;
183
    }
184
}
185