Completed
Pull Request — master (#4)
by Tomáš
03:05
created

GenerateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

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