Completed
Push — master ( 124fed...6caa0a )
by Tomáš
12s
created

ProxySourceCollectionDataProvider   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 130
ccs 0
cts 78
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A provideData() 0 4 1
A getSubscribedEvents() 0 10 1
C beforeRun() 0 34 7
B beforeRunPost() 0 18 7
A afterConvert() 0 8 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 Symplify\PHP7_Sculpin\DataProvider\DataProviderInterface;
15
use Symplify\PHP7_Sculpin\Event\ConvertEvent;
16
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
17
use Symplify\PHP7_Sculpin\Event\SourceSetEvent;
18
use Symplify\PHP7_Sculpin\Formatter\FormatterManager;
19
use Symplify\PHP7_Sculpin\Source\Filter\FilterInterface;
20
use Symplify\PHP7_Sculpin\Source\Map\MapInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
final class ProxySourceCollectionDataProvider implements DataProviderInterface, EventSubscriberInterface
24
{
25
    /**
26
     * @var FormatterManager
27
     */
28
    private $formatterManager;
29
30
    /**
31
     * @var string
32
     */
33
    private $dataProviderName;
34
35
    /**
36
     * @var array|ProxySourceCollection
37
     */
38
    private $collection = [];
39
40
    /**
41
     * @var FilterInterface
42
     */
43
    private $filter;
44
45
    /**
46
     * @var MapInterface
47
     */
48
    private $map;
49
50
    /**
51
     * @var SimpleProxySourceItemFactory
52
     */
53
    private $factory;
54
55
    public function __construct(
56
        FormatterManager $formatterManager,
57
        string $dataProviderName,
58
        ProxySourceCollection $collection,
59
        FilterInterface $filter,
60
        MapInterface $map,
61
        SimpleProxySourceItemFactory $factory
62
    ) {
63
        $this->formatterManager = $formatterManager;
64
        $this->dataProviderName = $dataProviderName;
65
        $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...
66
        $this->filter = $filter;
67
        $this->map = $map;
68
        $this->factory = $factory;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function provideData() : ProxySourceCollection
75
    {
76
        return $this->collection;
77
    }
78
79
    public static function getSubscribedEvents() : array
80
    {
81
        return [
82
            SculpinEvents::BEFORE_RUN => [
83
                ['beforeRun', 0],
84
                ['beforeRunPost', -100],
85
            ],
86
            SculpinEvents::AFTER_CONVERT => 'afterConvert',
87
        ];
88
    }
89
90
    public function beforeRun(SourceSetEvent $sourceSetEvent)
91
    {
92
        foreach ($sourceSetEvent->updatedSources() as $source) {
93
            if ($source->isGenerated()) {
94
                // We want to skip generated sources in case someone is
95
                // doing something like a redirect where virtual sources are
96
                // created like a redirect plugin.
97
98
                // NOTE: This means that a generator cannot create proxy
99
                // source collection items. This could be limiting in the
100
                // future...
101
                continue;
102
            }
103
104
            if ($this->filter->match($source)) {
105
                $this->map->process($source);
106
                $this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source);
107
            }
108
        }
109
        $foudAtLeastOne = false;
110
111
        foreach ($sourceSetEvent->allSources() as $source) {
112
            if ($this->filter->match($source)) {
113
                $foudAtLeastOne = true;
114
                break;
115
            }
116
        }
117
118
        if (!$foudAtLeastOne) {
119
            echo 'Didn\'t find at least one of this type : '.$this->dataProviderName.PHP_EOL;
120
        }
121
122
        $this->collection->init();
123
    }
124
125
    public function beforeRunPost(SourceSetEvent $sourceSetEvent)
126
    {
127
        $anItemHasChanged = false;
128
        foreach ($this->collection as $item) {
129
            if ($item->hasChanged()) {
130
                $anItemHasChanged = true;
131
                $this->collection->init();
132
                break;
133
            }
134
        }
135
        if ($anItemHasChanged) {
136
            foreach ($sourceSetEvent->allSources() as $source) {
137
                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...
138
                    $source->forceReprocess();
139
                }
140
            }
141
        }
142
    }
143
144
    public function afterConvert(ConvertEvent $convertEvent)
145
    {
146
        $sourceId = $convertEvent->source()->sourceId();
147
        if (isset($this->collection[$sourceId])) {
148
            $item = $this->collection[$sourceId];
149
            $item->setBlocks($this->formatterManager->formatSourceBlocks($convertEvent->source()));
150
        }
151
    }
152
}
153