Passed
Push — master ( 7b7005...55ec05 )
by Brent
02:29
created

GenerateCommand::onSiteParserInit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 17
rs 9.4285
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
    const ROUTE = 'route';
20
21
    private $stitcher;
22
    private $output;
23
    private $publicDir;
24
25
    public function __construct(string $publicDir, Stitcher $stitcher, EventDispatcher $eventDispatcher) {
26
        parent::__construct();
27
28
        $this->publicDir = $publicDir;
29
        $this->stitcher = $stitcher;
30
        $eventDispatcher->addSubscriber($this);
31
    }
32
33
    protected function configure() {
34
        $this->setName('site:generate')
35
            ->setDescription('Generate the website')
36
            ->setHelp("This command generates the website based on the data in the src/ folder.")
37
            ->addArgument(self::ROUTE, null, 'Specify a route to render');
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output) {
41
        $this->output = $output;
42
43
        $route = $input->getArgument(self::ROUTE);
44
45
        $startTime = microtime(true);
46
        $this->stitcher->stitch($route);
47
48
        if (!$route) {
49
            $this->saveGeneralFiles();
50
        }
51
52
        $endTime = microtime(true);
53
        $processTime = round($endTime - $startTime, 3);
54
55
        if ($route) {
56
            $output->writeln("\n<fg=green>{$route}</> successfully generated in <fg=green>{$this->publicDir}</>. ({$processTime}s)");
57
        } else {
58
            $output->writeln("\nSite successfully generated in <fg=green>{$this->publicDir}</>. ({$processTime}s)");
59
        }
60
    }
61
62
    public static function getSubscribedEvents() {
63
        return [
64
            SiteParser::EVENT_PAGE_PARSED => 'onPageParsed',
65
        ];
66
    }
67
68
    public function onPageParsed(Event $event) {
69
        $pageId = $event->getData()['pageId'] ?? null;
70
        $this->output->writeln("<fg=green>✔</> {$pageId}");
71
    }
72
73
    private function saveGeneralFiles() {
74
        $this->stitcher->saveHtaccess();
75
        $this->output->writeln("\n<fg=green>✔</> .htaccess");
76
77
        if ($this->stitcher->getSiteMap()->isEnabled()) {
78
            $this->stitcher->saveSitemap();
79
            $this->output->writeln("<fg=green>✔</> sitemap.xml");
80
        }
81
    }
82
}
83