Completed
Push — master ( d96b32...57aa95 )
by Sinnarasa
02:20
created

SmartMatch::matchMvc()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
c 6
b 1
f 2
dl 0
loc 26
rs 6.7272
cc 7
eloc 21
nc 8
nop 0
1
<?php
2
3
namespace JetFire\Routing\Match;
4
5
use JetFire\Routing\Router;
6
7
/**
8
 * Class SmartMatch
9
 * @package JetFire\Routing\Match
10
 */
11
class SmartMatch implements MatcherInterface
12
{
13
14
    /**
15
     * @var
16
     */
17
    private $router;
18
19
    /**
20
     * @param Router $router
21
     */
22
    public function __construct(Router $router)
23
    {
24
        $this->router = $router;
25
    }
26
27
    /**
28
     * @return bool
29
     */
30
    public function match()
31
    {
32
        if ($this->matchTemplate() || $this->matchMvc()) {
33
            $this->router->route->setResponse(['code' => 202, 'message' => 'Accepted']);
34
            return true;
35
        }
36
        return false;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    private function matchTemplate()
43
    {
44
        foreach ($this->router->getConfig()['viewExtension'] as $extension) {
45
            for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
46
                $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl()));
47
                $end = array_pop($url);
48
                $url = implode('/', array_map('ucwords', $url)).'/'.$end;
49
                if (is_file(($template = rtrim($this->router->collection->getRoutes('path_' . $i), '/') . $url . $extension))) {
50
                    $this->router->route->setTarget([
51
                        'dispatcher' => 'JetFire\Routing\Dispatcher\TemplateDispatcher',
52
                        'template' => $template,
53
                        'extension' => str_replace('.', '', $extension),
54
                        'callback' => $this->router->getConfig()['viewCallback']
55
                    ]);
56
                    $this->router->route->addDetail('block', $this->router->collection->getRoutes('path_' . $i));
57
                    return true;
58
                }
59
            }
60
        }
61
        return false;
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    private function matchMvc()
68
    {
69
        $routes = array_slice(explode('/', $this->router->route->getUrl()), 1);
70
        $i = 0;
71
        do{
72
            $route =  ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i))
73
                ? array_slice($routes, 1) : $routes;
74
            if (isset($route[0])) {
75
                $class =  (class_exists($this->router->collection->getRoutes('namespace_' . $i). ucfirst($route[0]) . 'Controller'))
76
                    ? $this->router->collection->getRoutes('namespace_' . $i). ucfirst($route[0]) . 'Controller'
77
                    : ucfirst($route[0]) . 'Controller';
78
                if (isset($route[1]) && method_exists($class, $route[1])) {
79
                    $this->router->route->setTarget([
80
                        'dispatcher' => 'JetFire\Routing\Dispatcher\MvcDispatcher',
81
                        'di' => $this->router->getConfig()['di'],
82
                        'controller' => $class,
83
                        'action' => $route[1]
84
                    ]);
85
                    $this->router->route->addDetail('parameters', array_slice($route, 2));
86
                    return true;
87
                }
88
            }
89
            ++$i;
90
        }while($i < $this->router->collection->countRoutes);
91
        return false;
92
    }
93
94
}
95