Completed
Pull Request — master (#9)
by Tomáš
03:16
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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
ccs 0
cts 17
cp 0
rs 9.4285
c 1
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\Core;
13
14
use Dflydev\DotAccessConfiguration\Configuration;
15
use Symplify\PHP7_Sculpin\Core\Converter\ConverterManager;
16
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents;
17
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent;
18
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterManager;
19
use Symplify\PHP7_Sculpin\Core\Generator\GeneratorManager;
20
use Symplify\PHP7_Sculpin\Core\Io\ConsoleIo;
21
use Symplify\PHP7_Sculpin\Core\Output\SourceOutput;
22
use Symplify\PHP7_Sculpin\Core\Output\FilesystemWriter;
23
use Symplify\PHP7_Sculpin\Core\Permalink\SourcePermalinkFactory;
24
use Symplify\PHP7_Sculpin\Core\Source\DataSourceInterface;
25
use Symplify\PHP7_Sculpin\Core\Source\SourceSet;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
28
final class Sculpin
29
{
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    private $eventDispatcher;
34
35
    /**
36
     * @var SourcePermalinkFactory
37
     */
38
    private $permalinkFactory;
39
40
    /**
41
     * @var FilesystemWriter
42
     */
43
    private $writer;
44
45
    /**
46
     * @var GeneratorManager
47
     */
48
    private $generatorManager;
49
50
    /**
51
     * @var FormatterManager
52
     */
53
    private $formatterManager;
54
55
    /**
56
     * @var ConverterManager
57
     */
58
    private $converterManager;
59
60
    /**
61
     * @var ConsoleIo
62
     */
63
    private $consoleIo;
64
65
    public function __construct(
66
        EventDispatcherInterface $eventDispatcher,
67
        SourcePermalinkFactory $permalinkFactory,
68
        FilesystemWriter $writer,
69
        GeneratorManager $generatorManager,
70
        FormatterManager $formatterManager,
71
        ConverterManager $converterManager,
72
        ConsoleIo $consoleIo
73
    ) {
74
        $this->eventDispatcher = $eventDispatcher;
75
        $this->permalinkFactory = $permalinkFactory;
76
        $this->writer = $writer;
77
        $this->generatorManager = $generatorManager;
78
        $this->formatterManager = $formatterManager;
79
        $this->converterManager = $converterManager;
80
        $this->consoleIo = $consoleIo;
81
    }
82
83
    public function run(DataSourceInterface $dataSource, SourceSet $sourceSet)
84
    {
85
        $found = false;
86
87
        $dataSource->refresh($sourceSet);
88
89
        $this->eventDispatcher->dispatch(SculpinEvents::EVENT_BEFORE_RUN, new SourceSetEvent($sourceSet));
90
91
        if ($updatedSources = array_filter($sourceSet->updatedSources(), function ($source) {
92
            return !$source->isGenerated();
93
        })) {
94
            if (!$found) {
95
                $this->consoleIo->write('Detected new or updated files');
96
                $found = true;
97
            }
98
99
            foreach ($updatedSources as $source) {
100
                $this->generatorManager->generate($source, $sourceSet);
101
            }
102
        }
103
104
        foreach ($sourceSet->updatedSources() as $source) {
105
            $permalink = $this->permalinkFactory->create($source);
106
            $source->setPermalink($permalink);
107
            $source->data()->set('url', $permalink->relativeUrlPath());
108
        }
109
110 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...
111
            if (!$found) {
112
                $this->consoleIo->write('Detected new or updated files');
113
                $found = true;
114
            }
115
116
            foreach ($updatedSources as $source) {
117
                $this->converterManager->convertSource($source);
118
119
                if ($source->canBeFormatted()) {
120
                    $source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source));
121
                }
122
            }
123
        }
124
125 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...
126
            if (!$found) {
127
                $this->consoleIo->write('Detected new or updated files');
128
                $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...
129
            }
130
131
            foreach ($updatedSources as $source) {
132
                if ($source->canBeFormatted()) {
133
                    $source->setFormattedContent($this->formatterManager->formatSourcePage($source));
134
                } else {
135
                    $source->setFormattedContent($source->content());
136
                }
137
            }
138
        }
139
140
        foreach ($sourceSet->updatedSources() as $source) {
141
            if ($source->isGenerator() || $source->shouldBeSkipped()) {
142
                continue;
143
            }
144
145
            $this->writer->write(new SourceOutput($source));
146
        }
147
    }
148
}
149