Completed
Pull Request — master (#6)
by Tomáš
04:54
created

ProxySourceCollectionDataProvider::beforeRunPost()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 22
cp 0
rs 6.6037
c 0
b 0
f 0
cc 8
eloc 14
nc 18
nop 1
crap 72
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\ProxySourceCollection;
13
14
use Doctrine\Common\Inflector\Inflector;
15
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderInterface;
16
use Symplify\PHP7_Sculpin\Core\Event\ConvertEvent;
17
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents;
18
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent;
19
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterManager;
20
use Symplify\PHP7_Sculpin\Core\Source\Filter\FilterInterface;
21
use Symplify\PHP7_Sculpin\Core\Source\Map\MapInterface;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
24
final class ProxySourceCollectionDataProvider implements DataProviderInterface, EventSubscriberInterface
25
{
26
    private $formatterManager;
27
    private $dataProviderName;
28
    private $dataSingularName;
29
    private $collection = [];
30
    private $filter;
31
    private $map;
32
    private $factory;
33
34
    public function __construct(
35
        FormatterManager $formatterManager,
36
        $dataProviderName,
37
        $dataSingularName,
38
        ProxySourceCollection $collection = null,
39
        FilterInterface $filter,
40
        MapInterface $map,
41
        ProxySourceItemFactoryInterface $factory = null
42
    ) {
43
        $this->formatterManager = $formatterManager;
44
        $this->dataProviderName = $dataProviderName;
45
        $this->dataSingularName = $dataSingularName ?: Inflector::singularize($dataProviderName);
46
        $this->collection = $collection ?: new ProxySourceCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like $collection ?: new \Symp...ProxySourceCollection() of type object<Symplify\PHP7_Scu...\ProxySourceCollection> is incompatible with the declared type array of property $collection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
        $this->filter = $filter;
48
        $this->map = $map;
49
        $this->factory = $factory ?: new SimpleProxySourceItemFactory();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function provideData() : ProxySourceCollection
56
    {
57
        return $this->collection;
58
    }
59
60
    public static function getSubscribedEvents() : array
61
    {
62
        return [
63
            SculpinEvents::EVENT_BEFORE_RUN => [
64
                ['beforeRun', 0],
65
                ['beforeRunPost', -100],
66
            ],
67
            SculpinEvents::EVENT_AFTER_CONVERT => 'afterConvert',
68
        ];
69
    }
70
71
    public function beforeRun(SourceSetEvent $sourceSetEvent)
72
    {
73
        foreach ($sourceSetEvent->updatedSources() as $source) {
74
            if ($source->isGenerated()) {
75
                // We want to skip generated sources in case someone is
76
                // doing something like a redirect where virtual sources are
77
                // created like a redirect plugin.
78
79
                // NOTE: This means that a generator cannot create proxy
80
                // source collection items. This could be limiting in the
81
                // future...
82
                continue;
83
            }
84
            if ($this->filter->match($source)) {
85
                $this->map->process($source);
86
                $this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source);
87
            }
88
        }
89
        $foudAtLeastOne = false;
90
91
        foreach ($sourceSetEvent->allSources() as $source) {
92
            if ($this->filter->match($source)) {
93
                $foudAtLeastOne = true;
94
                break;
95
            }
96
        }
97
98
        if (!$foudAtLeastOne) {
99
            echo 'Didnt find at least one of this type : '.$this->dataProviderName.PHP_EOL;
100
        }
101
102
        $this->collection->init();
0 ignored issues
show
Bug introduced by
The method init cannot be called on $this->collection (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
103
    }
104
105
    public function beforeRunPost(SourceSetEvent $sourceSetEvent)
106
    {
107
        $anItemHasChanged = false;
108
        foreach ($this->collection as $item) {
109
            if ($item->hasChanged()) {
110
                $anItemHasChanged = true;
111
                $this->collection->init();
0 ignored issues
show
Bug introduced by
The method init cannot be called on $this->collection (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
112
                break;
113
            }
114
        }
115
        if ($anItemHasChanged) {
116
            foreach ($sourceSetEvent->allSources() as $source) {
117
                if ($source->data()->get('use') and in_array($this->dataProviderName, $source->data()->get('use'))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
118
                    $source->forceReprocess();
119
                }
120
            }
121
        }
122
        foreach ($this->collection as $item) {
123
            $item->data()->set('next_'.$this->dataSingularName, $item->nextItem());
124
            $item->data()->set('previous_'.$this->dataSingularName, $item->previousItem());
125
        }
126
    }
127
128
    public function afterConvert(ConvertEvent $convertEvent)
129
    {
130
        $sourceId = $convertEvent->source()->sourceId();
131
        if (isset($this->collection[$sourceId])) {
132
            $item = $this->collection[$sourceId];
133
            $item->setBlocks($this->formatterManager->formatSourceBlocks($convertEvent->source()));
134
        }
135
    }
136
}
137