Completed
Push — master ( 91bd08...d24b22 )
by Tomáš
11s
created

ProxySourceTaxonomyDataProvider::beforeRun()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 17
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Contrib\Taxonomy;
13
14
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderInterface;
15
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderManager;
16
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents;
17
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent;
18
use Symplify\PHP7_Sculpin\Core\Sculpin;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
final class ProxySourceTaxonomyDataProvider implements DataProviderInterface, EventSubscriberInterface
22
{
23
    private $taxons = [];
24
    private $dataProviderManager;
25
    private $dataProviderName;
26
    private $taxonomyKey;
27
28
    public function __construct(
29
        DataProviderManager $dataProviderManager,
30
        string $dataProviderName,
31
        string $taxonomyKey
32
    ) {
33
        $this->dataProviderManager = $dataProviderManager;
34
        $this->dataProviderName = $dataProviderName;
35
        $this->taxonomyKey = $taxonomyKey;
36
    }
37
38
    public function provideData() : array
39
    {
40
        return $this->taxons;
41
    }
42
43
    public static function getSubscribedEvents()
44
    {
45
        return [
46
            SculpinEvents::EVENT_BEFORE_RUN => 'beforeRun',
47
        ];
48
    }
49
50
    public function beforeRun(SourceSetEvent $sourceSetEvent)
0 ignored issues
show
Unused Code introduced by
The parameter $sourceSetEvent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        $taxons = [];
53
        $dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName);
54
55
        foreach ($dataProvider->provideData() as $item) {
56
            if ($itemTaxons = $item->data()->get($this->taxonomyKey)) {
57
                $normalizedItemTaxons = [];
58
                foreach ((array) $itemTaxons as $itemTaxon) {
59
                    $normalizedItemTaxon = trim($itemTaxon);
60
                    $taxons[$normalizedItemTaxon][] = $item;
61
                    $normalizedItemTaxons[] = $normalizedItemTaxon;
62
                }
63
                $item->data()->set($this->taxonomyKey, $normalizedItemTaxons);
64
            }
65
        }
66
67
        $this->taxons = $taxons;
68
    }
69
}
70