Passed
Pull Request — master (#204)
by
unknown
23:41
created

resolver::process_component()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 2
b 0
f 0
nc 5
nop 0
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 5
rs 9.3888
1
<?php
2
/**
3
 * @package midcom.routing
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\routing;
10
11
use Symfony\Component\HttpFoundation\Request;
12
use midcom;
13
use midcom_error_forbidden;
14
use midcom_error_notfound;
15
use Symfony\Component\Routing\Router;
16
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
17
18
/**
19
 * @package midcom.routing
20
 */
21
class resolver
22
{
23
    /**
24
     * @var Request
25
     */
26
    private $request;
27
28
    /**
29
     * @var parser
30
     */
31
    private $parser;
32
33
    /**
34
     * @var \midcom_core_context
35
     */
36
    private $context;
37
38
    /**
39
     * @param Request $request
40
     */
41 338
    public function __construct(Request $request)
42
    {
43 338
        $this->request = $request;
44 338
        $this->context = $request->attributes->get('context');
45 338
        $this->parser = new parser($this->context);
46 338
    }
47
48
    /**
49
     * @param string $component
50
     * @param array $request_switch
51
     */
52 341
    public static function get_router($component, array $request_switch = []) : Router
53
    {
54 341
        $loader = new loader;
55 341
        if (!empty($request_switch)) {
56 120
            return new Router($loader, $request_switch);
57
        }
58 296
        $identifier = str_replace('.', '_', $component);
59 296
        return new Router($loader, $component, [
60 296
            'cache_dir' => midcom::get()->config->get('cache_base_directory') . 'routing/' . $identifier,
61
        ]);
62
    }
63
64
    /**
65
     * @throws midcom_error_notfound
66
     */
67 338
    public function process_midcom() : bool
68
    {
69 338
        if ($url = $this->parser->find_urlmethod()) {
70
            $router = self::get_router('midcom');
71
            $router->getContext()
72
                ->fromRequest($this->request);
73
74
            try {
75
                $result = $router->match($url);
76
            } catch (ResourceNotFoundException $e) {
77
                throw new midcom_error_notfound('This URL method is unknown.');
78
            }
79
            $this->request->attributes->add($result);
80
81
            return true;
82
        }
83 338
        return false;
84
    }
85
86
    /**
87
     * Basically this method will parse the URL and search for a component that can
88
     * handle the request. If one is found, it will process the request, if not, it
89
     * will report an error, depending on the situation.
90
     *
91
     * Details: The logic will traverse the node tree, and for the last node it will load
92
     * the component that is responsible for it. This component gets the chance to
93
     * accept the request, which is basically a call to can_handle. If the component
94
     * declares to be able to handle the call, its handle function is executed. Depending
95
     * if the handle was successful or not, it will either display an HTTP error page or
96
     * prepares the content handler to display the content later on.
97
     *
98
     * If the parsing process doesn't find any component that declares to be able to
99
     * handle the request, an HTTP 404 - Not Found error is triggered.
100
     *
101
     * @throws midcom_error_forbidden
102
     * @throws midcom_error_notfound
103
     */
104 338
    public function process_component()
105
    {
106 338
        $topic = $this->parser->find_topic();
107 338
        $this->request->attributes->set('argv', $this->parser->argv);
108
109
        // Get component interface class
110 338
        $component_interface = midcom::get()->componentloader->get_interface_class($topic->component);
111 338
        $viewer = $component_interface->get_viewer($topic);
112
113
        // Make can_handle check
114 338
        $result = $viewer->get_handler($this->request);
115 338
        if (!$result) {
116 12
            debug_add("Component {$topic->component} in {$topic->name} declared unable to handle request.", MIDCOM_LOG_INFO);
117 12
            throw new midcom_error_notfound("This page is not available on this server.");
118
        }
119 338
        $this->context->set_key(MIDCOM_CONTEXT_SHOWCALLBACK, [$viewer, 'show']);
120
121 338
        foreach ($result as $key => $value) {
122 338
            if ($key === 'handler') {
123 338
                $key = '_controller';
124 338
                $value[1] = '_handler_' . $value[1];
125 338
            } elseif ($key === '_route') {
126 338
                $key = 'handler_id';
127
            }
128 338
            $this->request->attributes->set($key, $value);
129
        }
130 338
        $viewer->handle();
131
    }
132
}