Completed
Pull Request — master (#4)
by Tomáš
03:16
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\DependencyInjection\ContainerInterface;
24
use Symfony\Component\Filesystem\Filesystem;
25
26
final class GenerateCommand extends Command
27
{
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
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
    public function __construct(
54
        ContainerInterface $container,
55
        Filesystem $filesystem,
56
        Sculpin $sculpin,
57
        CompositeDataSource $compositeDataSource,
58
        Configuration $configuration
59
    ) {
60
        $this->container = $container;
61
        $this->filesystem = $filesystem;
62
        $this->sculpin = $sculpin;
63
        $this->compositeDataSource = $compositeDataSource;
64
        $this->configuration = $configuration;
65
66
        parent::__construct();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function configure()
73
    {
74
        $this->setName('generate');
75
        $this->setDescription('Generate a site from source.');
76
        $this->addOption('clean', null, InputOption::VALUE_NONE, 'Cleans the output directory prior to generation.');
77
        $this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.');
78
        $this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site');
79
        $this->addOption('url', null, InputOption::VALUE_REQUIRED, 'Override URL.');
80
        $this->addOption('port', null, InputOption::VALUE_REQUIRED, 'Port');
81
        $this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.');
82
83
        $this->setHelp('The <info>generate</info> command generates a site.');
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function execute(InputInterface $input, OutputInterface $output)
90
    {
91
        $docroot = $this->container->getParameter('sculpin.output_dir');
92
        if ($input->getOption('clean')) {
93
            $this->clean($output, $docroot);
94
        }
95
96
        $watch = $input->getOption('watch') ?: false;
97
        $sourceSet = new SourceSet();
98
99
        $config = $this->container->get('sculpin.site_configuration');
100
        if ($url = $input->getOption('url')) {
101
            $config->set('url', $url);
102
        }
103
104
        if ($input->getOption('server')) {
105
            $this->sculpin->run($this->compositeDataSource, $sourceSet);
106
107
            $kernel = $this->container->get('kernel');
108
109
            $httpServer = new HttpServer(
110
                $output,
111
                $docroot,
112
                $kernel->getEnvironment(),
113
                $kernel->isDebug(),
114
                $input->getOption('port')
115
            );
116
117 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...
118
                $httpServer->addPeriodicTimer(1, function () use ($sourceSet) {
119
                    clearstatcache();
120
                    $sourceSet->reset();
121
122
                    $this->sculpin->run($this->compositeDataSource, $sourceSet);
123
                });
124
            }
125
126
            $httpServer->run();
127 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...
128
            do {
129
                $this->sculpin->run($this->compositeDataSource, $sourceSet);
130
131
                if ($watch) {
132
                    sleep(2);
133
                    clearstatcache();
134
                    $sourceSet->reset();
135
                }
136
            } while ($watch);
137
        }
138
    }
139
140
    private function clean(OutputInterface $output, string $dir)
141
    {
142
        if ($this->filesystem->exists($dir)) {
143
            $output->writeln(sprintf('Deleting %s', $dir));
144
            $this->filesystem->remove($dir);
145
        }
146
    }
147
}
148