Completed
Pull Request — master (#12)
by Tomáš
02:40
created

ProxySourceCollectionDataProvider::provideData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
crap 2
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\PostsBundle\ProxySourceCollection;
13
14
//use Doctrine\Common\Inflector\Inflector;
15
use Symplify\PHP7_Sculpin\DataProvider\DataProviderInterface;
16
use Symplify\PHP7_Sculpin\Event\ConvertEvent;
17
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
18
use Symplify\PHP7_Sculpin\Event\SourceSetEvent;
19
use Symplify\PHP7_Sculpin\Formatter\FormatterManager;
20
use Symplify\PHP7_Sculpin\Source\Filter\FilterInterface;
21
use Symplify\PHP7_Sculpin\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 $collection = [];
29
    private $filter;
30
    private $map;
31
    private $factory;
32
33
    public function __construct(
34
        FormatterManager $formatterManager,
35
        string $dataProviderName,
36
        ProxySourceCollection $collection,
37
        FilterInterface $filter,
38
        MapInterface $map,
39
        SimpleProxySourceItemFactory $factory
40
    ) {
41
        $this->formatterManager = $formatterManager;
42
        $this->dataProviderName = $dataProviderName;
43
        $this->collection = $collection; // ?: new ProxySourceCollection();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Documentation Bug introduced by
It seems like $collection 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...
44
        $this->filter = $filter;
45
        $this->map = $map;
46
        $this->factory = $factory;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function provideData() : ProxySourceCollection
53
    {
54
        return $this->collection;
55
    }
56
57
    public static function getSubscribedEvents() : array
58
    {
59
        return [
60
            SculpinEvents::BEFORE_RUN => [
61
                ['beforeRun', 0],
62
                ['beforeRunPost', -100],
63
            ],
64
            SculpinEvents::AFTER_CONVERT => 'afterConvert',
65
        ];
66
    }
67
68
    public function beforeRun(SourceSetEvent $sourceSetEvent)
69
    {
70
        foreach ($sourceSetEvent->updatedSources() as $source) {
71
            if ($source->isGenerated()) {
72
                // We want to skip generated sources in case someone is
73
                // doing something like a redirect where virtual sources are
74
                // created like a redirect plugin.
75
76
                // NOTE: This means that a generator cannot create proxy
77
                // source collection items. This could be limiting in the
78
                // future...
79
                continue;
80
            }
81
            if ($this->filter->match($source)) {
82
                $this->map->process($source);
83
                $this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source);
84
            }
85
        }
86
        $foudAtLeastOne = false;
87
88
        foreach ($sourceSetEvent->allSources() as $source) {
89
            if ($this->filter->match($source)) {
90
                $foudAtLeastOne = true;
91
                break;
92
            }
93
        }
94
95
        if (!$foudAtLeastOne) {
96
            echo 'Didnt find at least one of this type : '.$this->dataProviderName.PHP_EOL;
97
        }
98
99
        $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...
100
    }
101
102
    public function beforeRunPost(SourceSetEvent $sourceSetEvent)
103
    {
104
        $anItemHasChanged = false;
105
        foreach ($this->collection as $item) {
106
            if ($item->hasChanged()) {
107
                $anItemHasChanged = true;
108
                $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...
109
                break;
110
            }
111
        }
112
        if ($anItemHasChanged) {
113
            foreach ($sourceSetEvent->allSources() as $source) {
114
                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...
115
                    $source->forceReprocess();
116
                }
117
            }
118
        }
119
//        foreach ($this->collection as $item) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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