Completed
Pull Request — master (#3)
by Tomáš
06:46
created

GenerateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 15
cp 0
rs 9.4285
cc 1
eloc 13
nc 1
nop 5
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 Sculpin\Bundle\SculpinBundle\Command;
13
14
use Sculpin\Bundle\SculpinBundle\HttpServer\HttpServerFactory;
15
use Sculpin\Core\Configuration\Configuration;
16
use Sculpin\Core\Sculpin;
17
use Sculpin\Core\Source\CompositeDataSource;
18
use Sculpin\Core\Source\SourceSet;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Filesystem\Filesystem;
24
25
final class GenerateCommand extends Command
26
{
27
    /**
28
     * @var Filesystem
29
     */
30
    private $filesystem;
31
32
    /**
33
     * @var Sculpin
34
     */
35
    private $sculpin;
36
37
    /**
38
     * @var CompositeDataSource
39
     */
40
    private $dataSource;
41
42
    /**
43
     * @var Configuration
44
     */
45
    private $configuration;
46
47
    /**
48
     * @var HttpServerFactory
49
     */
50
    private $httpServerFactory;
51
52
    /**
53
     * @var SourceSet
54
     */
55
    private $sourceSet;
56
57
    public function __construct(
58
        Filesystem $filesystem,
59
        Sculpin $sculpin,
60
        CompositeDataSource $dataSource,
61
        Configuration $configuration,
62
        HttpServerFactory $httpServerFactory
63
    ) {
64
        $this->filesystem = $filesystem;
65
        $this->sculpin = $sculpin;
66
        $this->dataSource = $dataSource;
67
        $this->configuration = $configuration;
68
        $this->httpServerFactory = $httpServerFactory;
69
        $this->sourceSet = new SourceSet();
70
71
        parent::__construct();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function configure()
78
    {
79
        $this->setName('generate');
80
        $this->setDescription('Generate a site from source.');
81
82
        $this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.');
83
        $this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site');
84
        $this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.');
85
86
        $this->setHelp('The <info>generate</info> command generates a site.');
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function execute(InputInterface $input, OutputInterface $output)
93
    {
94
        $docroot = $this->configuration->getOutputDir();
95
96
        $this->clean($output, $docroot);
97
98
        $watch = $input->getOption('watch') ?: false;
99
100
        if ($input->getOption('server')) {
101
            $this->sculpin->run($this->dataSource, $this->sourceSet);
102
            $httpServer = $this->httpServerFactory->create($docroot);
103
104 View Code Duplication
            if ($watch) {
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...
105
                $httpServer->addPeriodicTimer(1, function () {
106
                    clearstatcache();
107
                    $this->sourceSet->reset();
108
109
                    $this->sculpin->run($this->dataSource, $this->sourceSet);
110
                });
111
            }
112
113
            $httpServer->run();
114 View Code Duplication
        } else {
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...
115
            do {
116
                $this->sculpin->run($this->dataSource, $this->sourceSet);
117
118
                if ($watch) {
119
                    sleep(2);
120
                    clearstatcache();
121
                    $this->sourceSet->reset();
122
                }
123
            } while ($watch);
124
        }
125
    }
126
127
    private function clean(OutputInterface $output, string $dir)
128
    {
129
        if ($this->filesystem->exists($dir)) {
130
            $output->writeln(sprintf('Deleting %s', $dir));
131
            $this->filesystem->remove($dir);
132
        }
133
    }
134
}
135