Completed
Push — master ( 91bd08...d24b22 )
by Tomáš
11s
created

Sculpin::run()   F

Complexity

Conditions 17
Paths 324

Size

Total Lines 95
Code Lines 60

Duplication

Lines 45
Ratio 47.37 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
dl 45
loc 95
ccs 0
cts 78
cp 0
rs 3.6909
c 0
b 0
f 0
cc 17
eloc 60
nc 324
nop 2
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Configuration
32
     */
33
    private $siteConfiguration;
34
35
    /**
36
     * @var EventDispatcherInterface
37
     */
38
    private $eventDispatcher;
39
40
    /**
41
     * @var SourcePermalinkFactory
42
     */
43
    private $permalinkFactory;
44
45
    /**
46
     * @var FilesystemWriter
47
     */
48
    private $writer;
49
50
    /**
51
     * @var GeneratorManager
52
     */
53
    private $generatorManager;
54
55
    /**
56
     * @var FormatterManager
57
     */
58
    private $formatterManager;
59
60
    /**
61
     * @var ConverterManager
62
     */
63
    private $converterManager;
64
    /**
65
     * @var ConsoleIo
66
     */
67
    private $consoleIo;
68
69
    public function __construct(
70
        Configuration $siteConfiguration,
71
        EventDispatcherInterface $eventDispatcher,
72
        SourcePermalinkFactory $permalinkFactory,
73
        FilesystemWriter $writer,
74
        GeneratorManager $generatorManager,
75
        FormatterManager $formatterManager,
76
        ConverterManager $converterManager,
77
        ConsoleIo $consoleIo
78
    ) {
79
        $this->siteConfiguration = $siteConfiguration;
80
        $this->eventDispatcher = $eventDispatcher;
81
        $this->permalinkFactory = $permalinkFactory;
82
        $this->writer = $writer;
83
        $this->generatorManager = $generatorManager;
84
        $this->formatterManager = $formatterManager;
85
        $this->converterManager = $converterManager;
86
        $this->consoleIo = $consoleIo;
87
    }
88
89
    public function run(DataSourceInterface $dataSource, SourceSet $sourceSet)
90
    {
91
        $found = false;
92
        $startTime = microtime(true);
93
94
        $dataSource->refresh($sourceSet);
95
96
        $this->eventDispatcher->dispatch(SculpinEvents::EVENT_BEFORE_RUN, new SourceSetEvent($sourceSet));
97
98
        if ($updatedSources = array_filter($sourceSet->updatedSources(), function ($source) {
99
            return !$source->isGenerated();
100
        })) {
101
            if (!$found) {
102
                $this->consoleIo->write('Detected new or updated files');
103
                $found = true;
104
            }
105
106
            $total = count($updatedSources);
107
108
            $this->consoleIo->write('Generating: ', false);
109
            $this->consoleIo->write('', false);
110
            $counter = 0;
111
            $timer = microtime(true);
112
            foreach ($updatedSources as $source) {
113
                $this->generatorManager->generate($source, $sourceSet);
114
                $this->consoleIo->overwrite(sprintf('%3d%%', 100 * ((++$counter) / $total)), false);
115
            }
116
            $this->consoleIo->write(sprintf(' (%d sources / %4.2f seconds)', $total, microtime(true) - $timer));
117
        }
118
119
        foreach ($sourceSet->updatedSources() as $source) {
120
            $permalink = $this->permalinkFactory->create($source);
121
            $source->setPermalink($permalink);
122
            $source->data()->set('url', $permalink->relativeUrlPath());
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;
129
            }
130
131
            $total = count($updatedSources);
132
133
            $this->consoleIo->write('Converting: ', false);
134
            $this->consoleIo->write('', false);
135
            $counter = 0;
136
            $timer = microtime(true);
137
            foreach ($updatedSources as $source) {
138
                $this->converterManager->convertSource($source);
139
140
                if ($source->canBeFormatted()) {
141
                    $source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source));
142
                }
143
                $this->consoleIo->overwrite(sprintf('%3d%%', 100 * ((++$counter) / $total)), false);
144
            }
145
            $this->consoleIo->write(sprintf(' (%d sources / %4.2f seconds)', $total, microtime(true) - $timer));
146
        }
147
148 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...
149
            if (!$found) {
150
                $this->consoleIo->write('Detected new or updated files');
151
                $found = true;
152
            }
153
154
            $total = count($updatedSources);
155
156
            $this->consoleIo->write('Formatting: ', false);
157
            $this->consoleIo->write('', false);
158
            $counter = 0;
159
            $timer = microtime(true);
160
            foreach ($updatedSources as $source) {
161
                if ($source->canBeFormatted()) {
162
                    $source->setFormattedContent($this->formatterManager->formatSourcePage($source));
163
                } else {
164
                    $source->setFormattedContent($source->content());
165
                }
166
                $this->consoleIo->overwrite(sprintf('%3d%%', 100 * ((++$counter) / $total)), false);
167
            }
168
169
            $this->consoleIo->write(sprintf(' (%d sources / %4.2f seconds)', $total, microtime(true) - $timer));
170
        }
171
172
        foreach ($sourceSet->updatedSources() as $source) {
173
            if ($source->isGenerator() || $source->shouldBeSkipped()) {
174
                continue;
175
            }
176
177
            $this->writer->write(new SourceOutput($source));
178
        }
179
180
        if ($found) {
181
            $this->consoleIo->write(sprintf('Processing completed in %4.2f seconds', microtime(true) - $startTime));
182
        }
183
    }
184
}
185