Passed
Push — master ( 451245...2b5fd7 )
by Brent
03:56
created

GenerateCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\Console;
6
use Brendt\Stitcher\Event\Event;
7
use Brendt\Stitcher\Site\Page;
8
use Brendt\Stitcher\Site\Site;
9
use Brendt\Stitcher\SiteParser;
10
use Brendt\Stitcher\Stitcher;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Helper\ProgressBar;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
class GenerateCommand extends Command implements EventSubscriberInterface
19
{
20
21
    const ROUTE = 'route';
22
23
    /**
24
     * @var Stitcher
25
     */
26
    private $stitcher;
27
28
    /**
29
     * @var ProgressBar
30
     */
31
    private $progress;
32
33
    /**
34
     * @var OutputInterface
35
     */
36
    private $output;
37
38
    /**
39
     * @var string
40
     */
41
    private $publicDir;
42
43
    public function __construct(string $publicDir, Stitcher $stitcher, EventDispatcher $eventDispatcher) {
44
        parent::__construct();
45
46
        $this->publicDir = $publicDir;
47
        $this->stitcher = $stitcher;
48
        $eventDispatcher->addSubscriber($this);
49
    }
50
51
    protected function configure() {
52
        $this->setName('site:generate')
53
            ->setDescription('Generate the website')
54
            ->setHelp("This command generates the website based on the data in the src/ folder.")
55
            ->addArgument(self::ROUTE, null, 'Specify a route to render');
56
    }
57
58
    /**
59
     * @param InputInterface  $input
60
     * @param OutputInterface $output
61
     *
62
     * @return void
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output) {
65
        $this->output = $output;
66
67
        $route = $input->getArgument(self::ROUTE);
68
        $blanket = $this->stitcher->stitch($route);
69
        $this->stitcher->save($blanket);
70
        $this->progress->clear();
71
72
        if ($route) {
73
            $output->writeln("<fg=green>{$route}</> successfully generated in <fg=green>{$this->publicDir}</>.");
74
        } else {
75
            $output->writeln("Site successfully generated in <fg=green>{$this->publicDir}</>.");
76
        }
77
    }
78
79
    public static function getSubscribedEvents() {
80
        return [
81
            SiteParser::EVENT_PARSER_INIT => 'onSiteParserInit',
82
            SiteParser::EVENT_PAGE_PARSING => 'onPageParsing',
83
            SiteParser::EVENT_PAGE_PARSED => 'onPageParsed',
84
        ];
85
    }
86
87
    public function onSiteParserInit(Event $event) {
88
        /** @var Site $site */
89
        $site = $event->getData()['site'] ?? null;
90
91
        if (!$site) {
92
            return;
93
        }
94
95
        $amount = count($site->getPages());
96
97
        $this->progress = new ProgressBar($this->output, $amount);
98
        $this->progress->setBarWidth(40);
99
        $this->progress->setFormatDefinition('custom', "%current%/%max% [%bar%] %message%\n");
100
        $this->progress->setFormat('custom');
101
        $this->progress->setMessage('');
102
        $this->progress->start();
103
    }
104
105
    public function onPageParsing(Event $event) {
106
        /** @var Page $page */
107
        $page = $event->getData()['page'] ?? null;
108
109
        if (!$this->progress || !$page) {
110
            return;
111
        }
112
113
        $this->progress->setMessage($page->getId());
114
    }
115
116
    public function onPageParsed() {
117
        if (!$this->progress) {
118
            return;
119
        }
120
121
        $this->progress->advance();
122
    }
123
}
124