Passed
Push — develop ( 844064...9bd21a )
by Brent
03:07
created

GenerateCommand::saveGeneralFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

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