Completed
Pull Request — master (#5)
by Tim
02:21
created

OptionSubject::getLastEntityId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\StoreViewCodes;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Utils\Generators\GeneratorInterface;
26
use TechDivision\Import\Services\RegistryProcessorInterface;
27
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
28
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
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
40
{
41
42
    /**
43
     * The attribute processor instance.
44
     *
45
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
46
     */
47
    protected $attributeBunchProcessor;
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
     * Initialize the subject instance.
65
     *
66
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface         $configuration              The subject configuration instance
67
     * @param \TechDivision\Import\Services\RegistryProcessorInterface                 $registryProcessor          The registry processor instance
68
     * @param \TechDivision\Import\Utils\Generators\GeneratorInterface                 $coreConfigDataUidGenerator The UID generator for the core config data
69
     * @param array                                                                    $systemLoggers              The array with the system loggers instances
70
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor    The attribute bunch processor instance
71
     */
72
    public function __construct(
73
        SubjectConfigurationInterface $configuration,
74
        RegistryProcessorInterface $registryProcessor,
75
        GeneratorInterface $coreConfigDataUidGenerator,
76
        array $systemLoggers,
77
        AttributeBunchProcessorInterface $attributeBunchProcessor
78
    ) {
79
80
        // pass the parameters to the parent constructor
81
        parent::__construct($configuration, $registryProcessor, $coreConfigDataUidGenerator, $systemLoggers);
82
83
        // initialize the attribute bunch processor
84
        $this->attributeBunchProcessor = $attributeBunchProcessor;
85
    }
86
87
    /**
88
     * Return's the attribute bunch processor instance.
89
     *
90
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
91
     */
92
    protected function getAttributeBunchProcessor()
93
    {
94
        return $this->attributeBunchProcessor;
95
    }
96
97
    /**
98
     * Map's the passed attribue code and value to the option ID that has been created recently.
99
     *
100
     * @param string $attributeCode The attriburte code that has to be mapped
101
     * @param string $value         The value that has to be mapped
102
     *
103
     * @return void
104
     */
105
    public function addAddtributeCodeValueOptionIdMapping($attributeCode, $value)
106
    {
107
        $this->attributeCodeValueOptionIdMapping[$attributeCode][$value] = $this->getLastEntityId();
108
    }
109
110
    /**
111
     * Queries whether or not the attribute with the passed code/value has already been processed.
112
     *
113
     * @param string $attributeCode The attribute code to check
114
     * @param string $value         The option value to check
115
     *
116
     * @return boolean TRUE if the path has been processed, else FALSE
117
     */
118
    public function hasBeenProcessed($attributeCode, $value)
119
    {
120
        return isset($this->attributeCodeValueOptionIdMapping[$attributeCode][$value]);
121
    }
122
123
    /**
124
     * Return's the ID of the attribute that has been created recently.
125
     *
126
     * @return integer The attribute ID
127
     */
128
    public function getLastEntityId()
129
    {
130
        return $this->getLastOptionId();
131
    }
132
133
    /**
134
     * Set's the ID of the option that has been created recently.
135
     *
136
     * @param integer $lastOptionId The option ID
137
     *
138
     * @return void
139
     */
140
    public function setLastOptionId($lastOptionId)
141
    {
142
        $this->lastOptionId = $lastOptionId;
143
    }
144
145
    /**
146
     * Return's the ID of the option that has been created recently.
147
     *
148
     * @return integer The option ID
149
     */
150
    public function getLastOptionId()
151
    {
152
        return $this->lastOptionId;
153
    }
154
155
    /**
156
     * Pre-load the option ID for the EAV attribute option with the passed attribute code/value.
157
     *
158
     * @param string $attributeCode The code of the EAV attribute to pre-load
159
     * @param string $value         The option admin store view value of the EAV attribute option to pre-load
160
     *
161
     * @return void
162
     */
163
    public function preLoadOptionId($attributeCode, $value)
164
    {
165
166
        // load the ID of the admin store
167
        $storeId = $this->stores[StoreViewCodes::ADMIN][MemberNames::STORE_ID];
168
169
        // load the EAV attribute option with the passed value
170
        $attributeOption = $this->getAttributeBunchProcessor()->loadAttributeOptionByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value);
171
172
        // set the EAV attribute option ID
173
        $this->setLastOptionId($attributeOption[MemberNames::OPTION_ID]);
174
    }
175
}
176