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

DevController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A run() 0 14 4
A createRouteCollection() 0 17 3
B getPage() 0 25 5
1
<?php
2
3
namespace Brendt\Stitcher\Application;
4
5
use Brendt\Stitcher\Exception\StitcherException;
6
use Brendt\Stitcher\Factory\AdapterFactory;
7
use Brendt\Stitcher\Stitcher;
8
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
9
use Symfony\Component\Routing\Matcher\UrlMatcher;
10
use Symfony\Component\Routing\RequestContext;
11
use Symfony\Component\Routing\Route;
12
use Symfony\Component\Routing\RouteCollection;
13
14
/**
15
 * The developer controller is used to render pages on the fly (on an HTTP request). This controller enables a
16
 * developer make code changes and see those changes real-time without re-compiling the whole site.
17
 */
18
class DevController
19
{
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 Stitcher $stitcher
30
     */
31
    public function __construct(Stitcher $stitcher) {
32
        $this->stitcher = $stitcher;
33
    }
34
35
    /**
36
     * Run the developer controller. This function will read the request URL and dispatch the according route.
37
     *
38
     * @param string $url
39
     *
40
     * @return string
41
     */
42
    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...
43
        if ($url === null) {
44
            $request = explode('?', $_SERVER['REQUEST_URI']);
45
            $url = reset($request);
46
        }
47
48
        try {
49
            return $this->getPage($url);
50
        } catch (StitcherException $e) {
51
            return $e->getMessage();
52
        } catch (ResourceNotFoundException $e) {
53
            return "404";
54
        }
55
    }
56
57
    /**
58
     * Create the route container
59
     *
60
     * @return \Symfony\Component\Routing\RouteCollection
61
     */
62
    protected function createRouteCollection() {
63
        $routeCollection = new RouteCollection();
64
        $site = $this->stitcher->loadSite();
65
66
        foreach ($site as $page) {
67
            $route = $page->getId();
68
69
            $routeCollection->add($route, new Route($route));
70
71
            if ($page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER)) {
72
                $paginationRoute = $route . '/page-{page}';
73
                $routeCollection->add($paginationRoute, new Route($paginationRoute));
74
            }
75
        }
76
77
        return $routeCollection;
78
    }
79
80
    /**
81
     * @param $url
82
     *
83
     * @return mixed
84
     */
85
    protected function getPage($url) {
86
        $routeCollection = $this->createRouteCollection();
87
        $matcher = new UrlMatcher($routeCollection, new RequestContext());
88
        $routeResult = $matcher->match($url);
89
        $route = $routeResult['_route'];
90
91
        $id = isset($routeResult['id']) ? $routeResult['id'] : null;
92
93
        if (isset($routeResult['page'])) {
94
            $route = str_replace('/page-{page}', '', $route);
95
            $id = $routeResult['page'];
96
        }
97
98
        $blanket = $this->stitcher->stitch($route, $id);
99
100
        if (isset($blanket[$route])) {
101
            return $blanket[$route];
102
        }
103
104
        if (isset($blanket[$url])) {
105
            return $blanket[$url];
106
        }
107
108
        throw new ResourceNotFoundException();
109
    }
110
111
}
112