Test Failed
Push — develop ( 4544b5...b9d221 )
by Brent
03:13
created

GenerateCommand::saveGeneralFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\Event\Event;
6
use Brendt\Stitcher\Site\Page;
7
use Brendt\Stitcher\Site\Site;
8
use Brendt\Stitcher\Parser\Site\SiteParser;
9
use Brendt\Stitcher\Stitcher;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
class GenerateCommand extends Command implements EventSubscriberInterface
18
{
19
20
    const ROUTE = 'route';
21
22
    /**
23
     * @var Stitcher
24
     */
25
    private $stitcher;
26
27
    /**
28
     * @var OutputInterface
29
     */
30
    private $output;
31
32
    /**
33
     * @var string
34
     */
35
    private $publicDir;
36
37
    public function __construct(string $publicDir, Stitcher $stitcher, EventDispatcher $eventDispatcher) {
38
        parent::__construct();
39
40
        $this->publicDir = $publicDir;
41
        $this->stitcher = $stitcher;
42
        $eventDispatcher->addSubscriber($this);
43
    }
44
45
    protected function configure() {
46
        $this->setName('site:generate')
47
            ->setDescription('Generate the website')
48
            ->setHelp("This command generates the website based on the data in the src/ folder.")
49
            ->addArgument(self::ROUTE, null, 'Specify a route to render');
50
    }
51
52
    /**
53
     * @param InputInterface  $input
54
     * @param OutputInterface $output
55
     *
56
     * @return void
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output) {
59
        $this->output = $output;
60
61
        $route = $input->getArgument(self::ROUTE);
62
63
        $startTime = microtime(true);
64
        $this->stitcher->stitch($route);
65
66
        if (!$route) {
67
            $this->saveGeneralFiles();
68
        }
69
70
        $endTime = microtime(true);
71
        $processTime = round($endTime - $startTime, 3);
72
73
        if ($route) {
74
            $output->writeln("\n<fg=green>{$route}</> successfully generated in <fg=green>{$this->publicDir}</>. ({$processTime}s)");
75
        } else {
76
            $output->writeln("\nSite successfully generated in <fg=green>{$this->publicDir}</>. ({$processTime}s)");
77
        }
78
    }
79
80
    public static function getSubscribedEvents() {
81
        return [
82
            SiteParser::EVENT_PAGE_PARSED => 'onPageParsed',
83
        ];
84
    }
85
86
    public function onPageParsed(Event $event) {
87
        $pageId = $event->getData()['pageId'] ?? null;
88
        $this->output->writeln("<fg=green>✔</> {$pageId}");
89
    }
90
91
    private function saveGeneralFiles() {
92
        $this->stitcher->saveHtaccess();
93
        $this->output->writeln("\n<fg=green>✔</> .htaccess");
94
95
        if ($this->stitcher->getSiteMap()->isEnabled()) {
96
            $this->stitcher->saveSitemap();
97
            $this->output->writeln("<fg=green>✔</> sitemap.xml");
98
        }
99
    }
100
}
101