Completed
Pull Request — master (#10)
by Tomáš
04:50 queued 01:55
created

Sculpin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 17
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 7
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;
13
14
use Symplify\PHP7_Sculpin\Converter\ConverterManager;
15
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
16
use Symplify\PHP7_Sculpin\Event\SourceSetEvent;
17
use Symplify\PHP7_Sculpin\Formatter\FormatterManager;
18
use Symplify\PHP7_Sculpin\Generator\GeneratorManager;
19
use Symplify\PHP7_Sculpin\Io\ConsoleIo;
20
use Symplify\PHP7_Sculpin\Output\SourceOutput;
21
use Symplify\PHP7_Sculpin\Output\FilesystemWriter;
22
use Symplify\PHP7_Sculpin\Permalink\SourcePermalinkFactory;
23
use Symplify\PHP7_Sculpin\Source\DataSourceInterface;
24
use Symplify\PHP7_Sculpin\Source\SourceSet;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
27
final class Sculpin
28
{
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34
    /**
35
     * @var SourcePermalinkFactory
36
     */
37
    private $permalinkFactory;
38
39
    /**
40
     * @var FilesystemWriter
41
     */
42
    private $writer;
43
44
    /**
45
     * @var GeneratorManager
46
     */
47
    private $generatorManager;
48
49
    /**
50
     * @var FormatterManager
51
     */
52
    private $formatterManager;
53
54
    /**
55
     * @var ConverterManager
56
     */
57
    private $converterManager;
58
59
    /**
60
     * @var ConsoleIo
61
     */
62
    private $consoleIo;
63
64
    public function __construct(
65
        EventDispatcherInterface $eventDispatcher,
66
        SourcePermalinkFactory $permalinkFactory,
67
        FilesystemWriter $writer,
68
        GeneratorManager $generatorManager,
69
        FormatterManager $formatterManager,
70
        ConverterManager $converterManager,
71
        ConsoleIo $consoleIo
72
    ) {
73
        $this->eventDispatcher = $eventDispatcher;
74
        $this->permalinkFactory = $permalinkFactory;
75
        $this->writer = $writer;
76
        $this->generatorManager = $generatorManager;
77
        $this->formatterManager = $formatterManager;
78
        $this->converterManager = $converterManager;
79
        $this->consoleIo = $consoleIo;
80
    }
81
82
    public function run(DataSourceInterface $dataSource, SourceSet $sourceSet)
83
    {
84
        $found = false;
85
86
        $dataSource->refresh($sourceSet);
87
88
        $this->eventDispatcher->dispatch(SculpinEvents::EVENT_BEFORE_RUN, new SourceSetEvent($sourceSet));
89
90
        if ($updatedSources = array_filter($sourceSet->updatedSources(), function ($source) {
91
            return !$source->isGenerated();
92
        })) {
93
            if (!$found) {
94
                $this->consoleIo->write('Detected new or updated files');
95
                $found = true;
96
            }
97
98
            foreach ($updatedSources as $source) {
99
                $this->generatorManager->generate($source, $sourceSet);
100
            }
101
        }
102
103
        foreach ($sourceSet->updatedSources() as $source) {
104
            $permalink = $this->permalinkFactory->create($source);
105
            $source->setPermalink($permalink);
106
            $source->data()->set('url', $permalink->relativeUrlPath());
107
        }
108
109 View Code Duplication
        if ($updatedSources = $sourceSet->updatedSources()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            if (!$found) {
111
                $this->consoleIo->write('Detected new or updated files');
112
                $found = true;
113
            }
114
115
            foreach ($updatedSources as $source) {
116
                $this->converterManager->convertSource($source);
117
118
                if ($source->canBeFormatted()) {
119
                    $source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source));
120
                }
121
            }
122
        }
123
124 View Code Duplication
        if ($updatedSources = $sourceSet->updatedSources()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            if (!$found) {
126
                $this->consoleIo->write('Detected new or updated files');
127
                $found = true;
0 ignored issues
show
Unused Code introduced by
$found is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
            }
129
130
            foreach ($updatedSources as $source) {
131
                if ($source->canBeFormatted()) {
132
                    $source->setFormattedContent($this->formatterManager->formatSourcePage($source));
133
                } else {
134
                    $source->setFormattedContent($source->content());
135
                }
136
            }
137
        }
138
139
        foreach ($sourceSet->updatedSources() as $source) {
140
            if ($source->isGenerator() || $source->shouldBeSkipped()) {
141
                continue;
142
            }
143
144
            $this->writer->write(new SourceOutput($source));
145
        }
146
    }
147
}
148