Completed
Pull Request — master (#4)
by Tomáš
02:58
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
//            $httpServer = new HttpServer(
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
//                $output,
114
//                $this->outputDirectory,
115
//                $input->getOption('port')
116
//            );
117
118 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...
119
                $httpServer->addPeriodicTimer(1, function () use ($sourceSet) {
120
                    clearstatcache();
121
                    $sourceSet->reset();
122
123
                    $this->sculpin->run($this->compositeDataSource, $sourceSet);
124
                });
125
            }
126
127
            $httpServer->run();
128 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...
129
            do {
130
                $this->sculpin->run($this->compositeDataSource, $sourceSet);
131
132
                if ($watch) {
133
                    sleep(2);
134
                    clearstatcache();
135
                    $sourceSet->reset();
136
                }
137
            } while ($watch);
138
        }
139
    }
140
141
    private function clean(OutputInterface $output, string $dir)
142
    {
143
        if ($this->filesystem->exists($dir)) {
144
            $output->writeln(sprintf('Deleting %s', $dir));
145
            $this->filesystem->remove($dir);
146
        }
147
    }
148
}
149