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

GenerateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

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 15
ccs 0
cts 14
cp 0
rs 9.4285
cc 1
eloc 12
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 Dflydev\DotAccessConfiguration\Configuration;
15
use Sculpin\Bundle\SculpinBundle\HttpServer\HttpServer;
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
     * @var CompositeDataSource
38
     */
39
    private $compositeDataSource;
40
41
    /**
42
     * @var Configuration
43
     */
44
    private $configuration;
45
    /**
46
     * @var string
47
     */
48
    private $outputDirectory;
49
50
    public function __construct(
51
        string $outputDirectory,
52
        Filesystem $filesystem,
53
        Sculpin $sculpin,
54
        CompositeDataSource $compositeDataSource,
55
        Configuration $configuration
56
    ) {
57
        $this->filesystem = $filesystem;
58
        $this->sculpin = $sculpin;
59
        $this->compositeDataSource = $compositeDataSource;
60
        $this->configuration = $configuration;
61
        $this->outputDirectory = $outputDirectory;
62
63
        parent::__construct();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function configure()
70
    {
71
        $this->setName('generate');
72
        $this->setDescription('Generate a site from source.');
73
        $this->addOption('clean', null, InputOption::VALUE_NONE, 'Cleans the output directory prior to generation.');
74
        $this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.');
75
        $this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site');
76
        $this->addOption('url', null, InputOption::VALUE_REQUIRED, 'Override URL.');
77
        $this->addOption('port', null, InputOption::VALUE_REQUIRED, 'Port');
78
        $this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.');
79
80
        $this->setHelp('The <info>generate</info> command generates a site.');
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function execute(InputInterface $input, OutputInterface $output)
87
    {
88
        if ($input->getOption('clean')) {
89
            $this->clean($output, $this->outputDirectory);
90
        }
91
92
        $watch = $input->getOption('watch') ?: false;
93
        $sourceSet = new SourceSet();
94
95
        if ($url = $input->getOption('url')) {
96
            $this->configuration->set('url', $url);
97
        }
98
99
        if ($input->getOption('server')) {
100
            $this->sculpin->run($this->compositeDataSource, $sourceSet);
101
102
            $kernel = $this->container->get('kernel');
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103
104
            $httpServer = new HttpServer(
105
                $output,
106
                $this->outputDirectory,
107
                $kernel->getEnvironment(),
108
                $kernel->isDebug(),
109
                $input->getOption('port')
110
            );
111
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