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

ClearAttributeObserver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\ClearAttributeObserver
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\Observers;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
use TechDivision\Import\Attribute\Utils\ColumnKeys;
25
use TechDivision\Import\Attribute\Utils\MemberNames;
26
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
27
28
/**
29
 * Observer that removes the EAV attribute with the code found in the CSV file.
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 ClearAttributeObserver extends AbstractAttributeImportObserver
38
{
39
40
    /**
41
     * The attribute processor instance.
42
     *
43
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
44
     */
45
    protected $attributeBunchProcessor;
46
47
    /**
48
     * Initializes the observer with the passed subject instance.
49
     *
50
     * @param \TechDivision\Import\Subjects\SubjectInterface                           $subject                 The observer's subject instance
51
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
52
     */
53
    public function __construct(
54
        SubjectInterface $subject,
55
        AttributeBunchProcessorInterface $attributeBunchProcessor
56
    ) {
57
58
        // pass the subject through to the parend observer
59
        parent::__construct($subject);
60
61
        // initialize the attribute processor
62
        $this->attributeBunchProcessor = $attributeBunchProcessor;
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return array The processed row
69
     */
70
    protected function process()
71
    {
72
73
        // query whether or not, we've found a new EAV attribute code => means we've found a new attribute
74
        if ($this->hasBeenProcessed($attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE))) {
75
            return;
76
        }
77
78
        // try to load the EAV attribute with the code found in the CSV file
79
        $attribute = $this->loadAttributeByAttributeCode($attributeCode);
80
81
        // delete the EAV attribute with the code found in the CSV file
82
        $this->deleteAttribute(array(MemberNames::ATTRIBUTE_ID => $attribute[MemberNames::ATTRIBUTE_ID]));
83
84
        // temporary persist the ID of the deleted attribute
85
        $this->setLastAttributeId($attribute[MemberNames::ATTRIBUTE_ID]);
86
    }
87
88
    /**
89
     * Return's the attribute bunch processor instance.
90
     *
91
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
92
     */
93
    protected function getAttributeBunchProcessor()
94
    {
95
        return $this->attributeBunchProcessor;
96
    }
97
98
    /**
99
     * Load's and return's the EAV attribute with the passed code.
100
     *
101
     * @param string $attributeCode The code of the EAV attribute to load
102
     *
103
     * @return array The EAV attribute
104
     */
105
    protected function loadAttributeByAttributeCode($attributeCode)
106
    {
107
        return $this->getAttributeBunchProcessor()->loadAttributeByAttributeCode($attributeCode);
108
    }
109
110
    /**
111
     * Delete's the EAV attribute with the passed attributes.
112
     *
113
     * @param array       $row  The attributes of the EAV attribute to delete
114
     * @param string|null $name The name of the prepared statement that has to be executed
115
     *
116
     * @return void
117
     */
118
    protected function deleteAttribute($row, $name = null)
119
    {
120
        $this->getAttributeBunchProcessor()->deleteAttribute($row, $name);
121
    }
122
}
123