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

RouterDispatchCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\App;
6
use Brendt\Stitcher\Stitcher;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Routing\Matcher\UrlMatcher;
12
use Symfony\Component\Routing\RequestContext;
13
use Symfony\Component\Routing\Route;
14
use Symfony\Component\Routing\RouteCollection;
15
16
class RouterDispatchCommand extends Command
17
{
18
19
    const URL = 'url';
20
21
    /**
22
     * @var Stitcher
23
     */
24
    private $stitcher;
25
26
    public function __construct(Stitcher $stitcher) {
27
        parent::__construct();
28
29
        $this->stitcher = $stitcher;
30
    }
31
32
    protected function configure() {
33
        $this->setName('router:dispatch')
34
            ->setDescription('Simulate routing of an URL')
35
            ->setHelp("Simulate routing of an URL.")
36
            ->addArgument(self::URL, InputArgument::REQUIRED);
37
    }
38
39
    /**
40
     * @param InputInterface  $input
41
     * @param OutputInterface $output
42
     *
43
     * @return void
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output) {
46
        $site = $this->stitcher->loadSite();
47
48
        $url = $input->getArgument(self::URL);
49
        $routes = [];
50
        foreach ($site as $page) {
51
            $routes[] = $page->getId();
52
        }
53
        $routeCollection = new RouteCollection();
54
55
        foreach ($routes as $route) {
56
            $routeCollection->add($route, new Route($route));
57
        }
58
59
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
60
        $result = $matcher->match($url);
61
62
        $output->writeln("<fg=green>{$url}</> matches <fg=green>{$result['_route']}</>");
63
    }
64
}
65