Completed
Pull Request — master (#2)
by Tim
04:47
created

AbstractAttributeSubject::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Subjects\AbstractAttributeSubject
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 2016 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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Subjects\AbstractSubject;
25
26
/**
27
 * The abstract product subject implementation that provides basic attribute
28
 * handling business logic.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 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-attribute
34
 * @link      http://www.techdivision.com
35
 */
36
abstract class AbstractAttributeSubject extends AbstractSubject
37
{
38
39
    /**
40
     * The ID of the attribute that has been created recently.
41
     *
42
     * @var integer
43
     */
44
    protected $lastAttributeId;
45
46
    /**
47
     * The array with the available entity types.
48
     *
49
     * @var array
50
     */
51
    protected $entityTypes = array();
52
53
    /**
54
     * Mappings for attribute code => CSV column header.
55
     *
56
     * @var array
57
     */
58
    protected $headerMappings = array();
59
60
    /**
61
     * The attribute code => attribute ID mapping.
62
     *
63
     * @var array
64
     */
65
    protected $attributeCodeIdMapping = array();
66
67
    /**
68
     * Intializes the previously loaded global data for exactly one bunch.
69
     *
70
     * @param string $serial The serial of the actual import
71
     *
72
     * @return void
73
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
74
     */
75
    public function setUp($serial)
76
    {
77
78
        // load the status of the actual import
79
        $status = $this->getRegistryProcessor()->getAttribute($serial);
80
81
        // load the global data we've prepared initially
82
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
83
84
        // prepare the callbacks
85
        parent::setUp($serial);
86
    }
87
88
    /**
89
     * Clean up the global data after importing the bunch.
90
     *
91
     * @param string $serial The serial of the actual import
92
     *
93
     * @return void
94
     */
95
    public function tearDown($serial)
96
    {
97
98
        // invoke the parent method
99
        parent::tearDown($serial);
100
101
        // update the status
102
        $this->getRegistryProcessor()->mergeAttributesRecursive(
103
            $serial,
104
            array(
105
                RegistryKeys::FILES => array($this->getFilename() => array(RegistryKeys::STATUS => 1))
106
            )
107
        );
108
    }
109
110
    /**
111
     * Return's the header mappings for the actual entity.
112
     *
113
     * @return array The header mappings
114
     */
115
    public function getHeaderMappings()
116
    {
117
        return $this->headerMappings;
118
    }
119
120
    /**
121
     * Set's the ID of the attribute that has been created recently.
122
     *
123
     * @param integer $lastAttributeId The attribute ID
124
     *
125
     * @return void
126
     */
127
    public function setLastAttributeId($lastAttributeId)
128
    {
129
        $this->lastAttributeId = $lastAttributeId;
130
    }
131
132
    /**
133
     * Return's the ID of the attribute that has been created recently.
134
     *
135
     * @return integer The attribute ID
136
     */
137
    public function getLastAttributeId()
138
    {
139
        return $this->lastAttributeId;
140
    }
141
142
    /**
143
     * Map's the passed attribute code to the attribute ID that has been created recently.
144
     *
145
     * @param string $attributeCode The attribute code that has to be mapped
146
     *
147
     * @return void
148
     */
149
    public function addAttributeCodeIdMapping($attributeCode)
150
    {
151
        $this->attributeCodeIdMapping[$attributeCode] = $this->getLastAttributeId();
152
    }
153
154
    /**
155
     * Queries whether or not the attribute with the passed code has already been processed.
156
     *
157
     * @param string $attributeCode The attribute code to check
158
     *
159
     * @return boolean TRUE if the path has been processed, else FALSE
160
     */
161
    public function hasBeenProcessed($attributeCode)
162
    {
163
        return isset($this->attributeCodeIdMapping[$attributeCode]);
164
    }
165
166
    /**
167
     * Return's the entity type for the passed code.
168
     *
169
     * @param string $entityTypeCode The entity type code
170
     *
171
     * @return array The requested entity type
172
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
173
     */
174
    public function getEntityType($entityTypeCode)
175
    {
176
177
        // query whether or not, the entity type with the passed code is available
178
        if (isset($this->entityTypes[$entityTypeCode])) {
179
            return $this->entityTypes[$entityTypeCode];
180
        }
181
182
        // throw an exception, if not
183
        throw new \Exception(
184
            sprintf(
185
                'Can\'t find entity type with code %s in file %s on line %d',
186
                $entityTypeCode,
187
                $this->getFilename(),
188
                $this->getLineNumber()
189
            )
190
        );
191
    }
192
}
193