Completed
Push — master ( da6ba5...e77248 )
by
unknown
01:56
created

ReduceAttributeOptionValueListener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Listeners\ReduceAttributeOptionValueListener
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    Martin Eissenführer <[email protected]>
15
 * @copyright 2021 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Listeners;
22
23
use League\Event\EventInterface;
24
use League\Event\AbstractListener;
25
use TechDivision\Import\Utils\CacheKeys;
26
use TechDivision\Import\Services\RegistryProcessorInterface;
27
28
/**
29
 * An listener implementation that reduces and sorts the array with the exported attribute option values.
30
 *
31
 * @author    Martin Eissenführer <[email protected]>
32
 * @copyright 2021 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
35
 * @link      http://www.techdivision.com
36
 */
37
abstract class ReduceAttributeOptionValueListener extends AbstractListener
38
{
39
40
    /**
41
     * The registry processor instance.
42
     *
43
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
44
     */
45
    protected $registryProcessor;
46
47
    /**
48
     * Initializes the listener with the registry processor instance.
49
     *
50
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The processor instance
51
     */
52
    public function __construct(RegistryProcessorInterface $registryProcessor)
53
    {
54
        $this->registryProcessor = $registryProcessor;
55
    }
56
57
    /**
58
     * return the artefact name from option values
59
     *
60
     * @return string
61
     */
62
    abstract public function getArtefactName();
63
64
    /**
65
     * Handle the event.
66
     *
67
     * @param \League\Event\EventInterface $event The event that triggered the listener
68
     *
69
     * @return void
70
     */
71
    public function handle(EventInterface $event)
72
    {
73
74
        // try to load the availalbe artefacts from the registry processor
75
        if ($artefacts = $this->registryProcessor->getAttribute(CacheKeys::ARTEFACTS)) {
76
            // query whether or not categories are available
77
            if (isset($artefacts[$this->getArtefactName()])) {
78
                // initialize the array for the sorted und merged categories
79
                $toExport = array();
80
81
                // load the categories from the artefacts
82
                $arts = $artefacts[$this->getArtefactName()];
83
                // iterate over the categories
84
                foreach ($arts as $attributeOptionValues) {
85
                    foreach ($attributeOptionValues as $attributeOptionValue) {
86
                        // Generate unique key for artefact to avoid duplicates
87
                        $attributeCode = md5(\json_encode($attributeOptionValue));
88
                        // query whether or not the attribute code has already been processed
89
                        if (isset($toExport[$attributeCode])) {
90
                            continue;
91
                        }
92
93
                        // if not, add it to the array
94
                        $toExport[$attributeCode] = $attributeOptionValue;
95
                    }
96
                }
97
98
                // replace them in the array with the artefacts
99
                $artefacts[$this->getArtefactName()] = array($toExport);
100
                // override the old artefacts
101
                $this->registryProcessor->setAttribute(CacheKeys::ARTEFACTS, $artefacts, array(), array(), true);
102
            }
103
        }
104
    }
105
}
106