Completed
Push — 19.x ( 24d584 )
by Tim
02:01
created

OptionSubject::setUp()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 29

Duplication

Lines 14
Ratio 48.28 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 14
loc 29
ccs 0
cts 21
cp 0
rs 8.8337
c 0
b 0
f 0
cc 6
nc 18
nop 1
crap 42
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Subjects\OptionSubject
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\BackendTypeKeys;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Attribute\Utils\ConfigurationKeys;
26
use TechDivision\Import\Subjects\FileUploadTrait;
27
use TechDivision\Import\Subjects\FileUploadSubjectInterface;
28
use TechDivision\Import\Subjects\CastValueSubjectInterface;
29
30
/**
31
 * The subject implementation that handles the business logic to persist attribute options.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-attribute
37
 * @link      http://www.techdivision.com
38
 */
39
class OptionSubject extends AbstractAttributeSubject implements OptionSubjectInterface, FileUploadSubjectInterface, CastValueSubjectInterface
40
{
41
42
    /**
43
     * The trait that provides file upload functionality.
44
     *
45
     * @var \TechDivision\Import\Subjects\FileUploadTrait
46
     */
47
    use FileUploadTrait;
48
49
    /**
50
     * The ID of the option that has been created recently.
51
     *
52
     * @var integer
53
     */
54
    protected $lastOptionId;
55
56
    /**
57
     * The value => option ID mapping.
58
     *
59
     * @var array
60
     */
61
    protected $attributeCodeValueOptionIdMapping = array();
62
63
    /**
64
     * Initializes the previously loaded global data for exactly one bunch.
65
     *
66
     * @param string $serial The serial of the actual import
67
     *
68
     * @return void
69
     */
70
    public function setUp($serial)
71
    {
72
73
        // initialize the flag whether to copy images or not
74
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) {
75
            $this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES));
76
        }
77
78
        // initialize media directory => can be absolute or relative
79 View Code Duplication
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            try {
81
                $this->setMediaDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY)));
82
            } catch (\InvalidArgumentException $iae) {
83
                $this->getSystemLogger()->warning($iae);
84
            }
85
        }
86
87
        // initialize images directory => can be absolute or relative
88 View Code Duplication
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            try {
90
                $this->setImagesFileDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)));
91
            } catch (\InvalidArgumentException $iae) {
92
                $this->getSystemLogger()->warning($iae);
93
            }
94
        }
95
96
        // prepare the callbacks
97
        parent::setUp($serial);
98
    }
99
100
    /**
101
     * Map's the passed attribue code and value to the option ID that has been created recently.
102
     *
103
     * @param string $attributeCode The attriburte code that has to be mapped
104
     * @param string $value         The value that has to be mapped
105
     *
106
     * @return void
107
     */
108
    public function addAddtributeCodeValueOptionIdMapping($attributeCode, $value)
109
    {
110
        $this->attributeCodeValueOptionIdMapping[$attributeCode][$value] = $this->getLastEntityId();
111
    }
112
113
    /**
114
     * Queries whether or not the attribute with the passed code/value has already been processed.
115
     *
116
     * @param string $attributeCode The attribute code to check
117
     * @param string $value         The option value to check
118
     *
119
     * @return boolean TRUE if the path has been processed, else FALSE
120
     */
121
    public function hasBeenProcessed($attributeCode, $value)
122
    {
123
        return isset($this->attributeCodeValueOptionIdMapping[$attributeCode][$value]);
124
    }
125
126
    /**
127
     * Return's the ID of the attribute that has been created recently.
128
     *
129
     * @return integer The attribute ID
130
     */
131
    public function getLastEntityId()
132
    {
133
        return $this->getLastOptionId();
134
    }
135
136
    /**
137
     * Set's the ID of the option that has been created recently.
138
     *
139
     * @param integer $lastOptionId The option ID
140
     *
141
     * @return void
142
     */
143
    public function setLastOptionId($lastOptionId)
144
    {
145
        $this->lastOptionId = $lastOptionId;
146
    }
147
148
    /**
149
     * Return's the ID of the option that has been created recently.
150
     *
151
     * @return integer The option ID
152
     */
153
    public function getLastOptionId()
154
    {
155
        return $this->lastOptionId;
156
    }
157
158
    /**
159
     * Pre-load the option ID for the passed EAV attribute option.
160
     *
161
     * @param array $attributeOption The EAV attribute option with the ID that has to be pre-loaded
162
     *
163
     * @return void
164
     */
165
    public function preLoadOptionId(array $attributeOption)
166
    {
167
        $this->setLastOptionId($attributeOption[MemberNames::OPTION_ID]);
168
    }
169
170
    /**
171
     * Cast's the passed value based on the backend type information.
172
     *
173
     * @param string $backendType The backend type to cast to
174
     * @param mixed  $value       The value to be casted
175
     *
176
     * @return mixed The casted value
177
     */
178
    public function castValueByBackendType($backendType, $value)
179
    {
180
181
        // cast the value to a valid timestamp
182
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) {
183
            return $this->getDateConverter()->convert($value);
184
        }
185
186
        // cast the value to a string that represents the float/decimal value, because
187
        // PHP will cast float values implicitly to the system locales format when
188
        // rendering as string, e. g. with echo
189
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT ||
190
            $backendType === BackendTypeKeys::BACKEND_TYPE_DECIMAL
191
        ) {
192
            return (string) $this->getNumberConverter()->parse($value);
193
        }
194
195
        // cast the value to an integer
196
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) {
197
            return (integer) $value;
198
        }
199
200
        // we don't need to cast strings
201
        return $value;
202
    }
203
}
204