Passed
Push — master ( 1f976a...e46236 )
by Brent
02:31
created

DevController::getPage()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 25
rs 8.439
1
<?php
2
3
namespace brendt\stitcher\controller;
4
5
use brendt\stitcher\Config;
6
use brendt\stitcher\exception\StitcherException;
7
use brendt\stitcher\factory\AdapterFactory;
8
use brendt\stitcher\Stitcher;
9
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
10
use Symfony\Component\Routing\Matcher\UrlMatcher;
11
use Symfony\Component\Routing\RequestContext;
12
use Symfony\Component\Routing\Route;
13
use Symfony\Component\Routing\RouteCollection;
14
15
/**
16
 * The developer controller is used to render pages on the fly (on an HTTP request). This controller enables a
17
 * developer make code changes and see those changes real-time without re-compiling the whole site.
18
 */
19
class DevController {
20
21
    /**
22
     * @var Stitcher
23
     */
24
    protected $stitcher;
25
26
    /**
27
     * Create a new developer controller with optional configuration path and -file.
28
     *
29
     * @param string $path
30
     * @param string $name
31
     */
32
    public function __construct($path = './', $name = 'config.dev.yml') {
33
        Config::load($path, $name);
34
35
        $this->stitcher = new Stitcher();
36
    }
37
38
    /**
39
     * Run the developer controller. This function will read the request URL and dispatch the according route.
40
     *
41
     * @param string $url
42
     *
43
     * @return string
44
     */
45
    public function run($url = null) {
0 ignored issues
show
Coding Style introduced by
run uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
        if ($url === null) {
47
            $request = explode('?', $_SERVER['REQUEST_URI']);
48
            $url = reset($request);
49
        }
50
51
        try {
52
            return $this->getPage($url);
53
        } catch (StitcherException $e) {
54
            return $e->getMessage();
55
        } catch (ResourceNotFoundException $e) {
56
            return "404";
57
        }
58
    }
59
60
    /**
61
     * Create the route container
62
     *
63
     * @return \Symfony\Component\Routing\RouteCollection
64
     */
65
    protected function createRouteCollection() {
66
        $routeCollection = new RouteCollection();
67
        $site = $this->stitcher->loadSite();
68
69
        foreach ($site as $page) {
70
            $route = $page->getId();
71
72
            $routeCollection->add($route, new Route($route));
73
74
            if ($page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER)) {
75
                $paginationRoute = $route . '/page-{page}';
76
                $routeCollection->add($paginationRoute, new Route($paginationRoute));
77
            }
78
        }
79
80
        return $routeCollection;
81
    }
82
83
    /**
84
     * @param $url
85
     *
86
     * @return mixed
87
     */
88
    protected function getPage($url) {
89
        $routeCollection = $this->createRouteCollection();
90
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
91
        $routeResult = $matcher->match($url);
92
        $route = $routeResult['_route'];
93
94
        $id = isset($routeResult['id']) ? $routeResult['id'] : null;
95
96
        if (isset($routeResult['page'])) {
97
            $route = str_replace('/page-{page}', '', $route);
98
            $id = $routeResult['page'];
99
        }
100
101
        $blanket = $this->stitcher->stitch($route, $id);
102
103
        if (isset($blanket[$route])) {
104
            return $blanket[$route];
105
        }
106
107
        if (isset($blanket[$url])) {
108
            return $blanket[$url];
109
        }
110
111
        throw new ResourceNotFoundException();
112
    }
113
114
}
115