Completed
Push — master ( fa31eb...190b3f )
by Tim
9s
created

BunchSubject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

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 8
nc 1
nop 5
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Subjects\BunchSubject
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\MemberNames;
24
use TechDivision\Import\Attribute\Utils\RegistryKeys;
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 attributes.
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 BunchSubject 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 array with the pre-loaded attribute IDs.
51
     *
52
     * @var array
53
     */
54
    protected $preLoadedAttributeIds = array();
55
56
    /**
57
     * Initialize the subject instance.
58
     *
59
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface         $configuration              The subject configuration instance
60
     * @param \TechDivision\Import\Services\RegistryProcessorInterface                 $registryProcessor          The registry processor instance
61
     * @param \TechDivision\Import\Utils\Generators\GeneratorInterface                 $coreConfigDataUidGenerator The UID generator for the core config data
62
     * @param array                                                                    $systemLoggers              The array with the system loggers instances
63
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor    The attribute bunch processor instance
64
     */
65
    public function __construct(
66
        SubjectConfigurationInterface $configuration,
67
        RegistryProcessorInterface $registryProcessor,
68
        GeneratorInterface $coreConfigDataUidGenerator,
69
        array $systemLoggers,
70
        AttributeBunchProcessorInterface $attributeBunchProcessor
71
    ) {
72
73
        // pass the parameters to the parent constructor
74
        parent::__construct($configuration, $registryProcessor, $coreConfigDataUidGenerator, $systemLoggers);
75
76
        // initialize the attribute bunch processor
77
        $this->attributeBunchProcessor = $attributeBunchProcessor;
78
    }
79
80
    /**
81
     * Clean up the global data after importing the bunch.
82
     *
83
     * @param string $serial The serial of the actual import
84
     *
85
     * @return void
86
     */
87
    public function tearDown($serial)
88
    {
89
90
        // invoke the parent method
91
        parent::tearDown($serial);
92
93
        // load the registry processor
94
        $registryProcessor = $this->getRegistryProcessor();
95
96
        // update the status
97
        $registryProcessor->mergeAttributesRecursive(
98
            $serial,
99
            array(
100
                RegistryKeys::PRE_LOADED_ATTRIBUTE_IDS => $this->preLoadedAttributeIds,
101
            )
102
        );
103
    }
104
105
    /**
106
     * Pre-load the attribute ID for the EAV attribute with the passed code.
107
     *
108
     * @param string $attributeCode The code of the EAV attribute to pre-load
109
     *
110
     * @return void
111
     */
112
    public function preLoadAttributeId($attributeCode)
113
    {
114
115
        // load the EAV attribute by the passed code
116
        $attribute = $this->loadAttributeByAttributeCode($attributeCode);
117
118
        // temporary persist the pre-loaded attribute code => ID mapping
119
        $this->preLoadedAttributeIds[$attributeCode]= $attribute[MemberNames::ATTRIBUTE_ID];
120
    }
121
122
    /**
123
     * Return's the attribute bunch processor instance.
124
     *
125
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
126
     */
127
    protected function getAttributeBunchProcessor()
128
    {
129
        return $this->attributeBunchProcessor;
130
    }
131
132
    /**
133
     * Load's and return's the EAV attribute with the passed code.
134
     *
135
     * @param string $attributeCode The code of the EAV attribute to load
136
     *
137
     * @return array The EAV attribute
138
     */
139
    protected function loadAttributeByAttributeCode($attributeCode)
140
    {
141
        return $this->getAttributeBunchProcessor()->loadAttributeByAttributeCode($attributeCode);
142
    }
143
}
144