Completed
Push — 18.x ( ac68fb )
by Marcus
06:04
created

OptionSubject::setUp()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 18
cp 0
rs 9.536
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12
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\Attribute\Utils\ConfigurationKeys;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Subjects\FileUploadSubjectInterface;
26
use TechDivision\Import\Subjects\FileUploadTrait;
27
28
/**
29
 * The subject implementation that handles the business logic to persist attribute options.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-attribute
35
 * @link      http://www.techdivision.com
36
 */
37
class OptionSubject extends AbstractAttributeSubject implements OptionSubjectInterface, FileUploadSubjectInterface
38
{
39
40
    /**
41
     * The trait that provides file upload functionality.
42
     *
43
     * @var \TechDivision\Import\Subjects\FileUploadTrait
44
     */
45
    use FileUploadTrait;
46
47
    /**
48
     * The ID of the option that has been created recently.
49
     *
50
     * @var integer
51
     */
52
    protected $lastOptionId;
53
54
    /**
55
     * The value => option ID mapping.
56
     *
57
     * @var array
58
     */
59
    protected $attributeCodeValueOptionIdMapping = array();
60
61
    /**
62
     * Initializes the previously loaded global data for exactly one bunch.
63
     *
64
     * @param string $serial The serial of the actual import
65
     *
66
     * @return void
67
     */
68
    public function setUp($serial)
69
    {
70
71
        // initialize media directory => can be absolute or relative
72
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) {
73
            $this->setMediaDir(
74
                $this->resolvePath(
75
                    $this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY)
76
                )
77
            );
78
        }
79
80
        // initialize images directory => can be absolute or relative
81
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) {
82
            $this->setImagesFileDir(
83
                $this->resolvePath(
84
                    $this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)
85
                )
86
            );
87
        }
88
89
        // prepare the callbacks
90
        parent::setUp($serial);
91
    }
92
93
    /**
94
     * Map's the passed attribue code and value to the option ID that has been created recently.
95
     *
96
     * @param string $attributeCode The attriburte code that has to be mapped
97
     * @param string $value         The value that has to be mapped
98
     *
99
     * @return void
100
     */
101
    public function addAddtributeCodeValueOptionIdMapping($attributeCode, $value)
102
    {
103
        $this->attributeCodeValueOptionIdMapping[$attributeCode][$value] = $this->getLastEntityId();
104
    }
105
106
    /**
107
     * Queries whether or not the attribute with the passed code/value has already been processed.
108
     *
109
     * @param string $attributeCode The attribute code to check
110
     * @param string $value         The option value to check
111
     *
112
     * @return boolean TRUE if the path has been processed, else FALSE
113
     */
114
    public function hasBeenProcessed($attributeCode, $value)
115
    {
116
        return isset($this->attributeCodeValueOptionIdMapping[$attributeCode][$value]);
117
    }
118
119
    /**
120
     * Return's the ID of the attribute that has been created recently.
121
     *
122
     * @return integer The attribute ID
123
     */
124
    public function getLastEntityId()
125
    {
126
        return $this->getLastOptionId();
127
    }
128
129
    /**
130
     * Set's the ID of the option that has been created recently.
131
     *
132
     * @param integer $lastOptionId The option ID
133
     *
134
     * @return void
135
     */
136
    public function setLastOptionId($lastOptionId)
137
    {
138
        $this->lastOptionId = $lastOptionId;
139
    }
140
141
    /**
142
     * Return's the ID of the option that has been created recently.
143
     *
144
     * @return integer The option ID
145
     */
146
    public function getLastOptionId()
147
    {
148
        return $this->lastOptionId;
149
    }
150
151
    /**
152
     * Pre-load the option ID for the passed EAV attribute option.
153
     *
154
     * @param array $attributeOption The EAV attribute option with the ID that has to be pre-loaded
155
     *
156
     * @return void
157
     */
158
    public function preLoadOptionId(array $attributeOption)
159
    {
160
        $this->setLastOptionId($attributeOption[MemberNames::OPTION_ID]);
161
    }
162
}
163