Restful::peel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 2
b 0
f 1
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Middlewares;
6
7
use Nymfonya\Component\Http\Request;
8
use Nymfonya\Component\Http\Interfaces\MiddlewareInterface;
9
use Nymfonya\Component\Container;
10
11
/**
12
 * App\Middleware\Restful
13
 *
14
 * Patch kernel action from request method
15
 */
16
class Restful implements MiddlewareInterface
17
{
18
    use \App\Middlewares\Reuse\TInit;
19
20
    const _SIGN = 'X-Middleware-Restful';
21
    const METHOD_ACTIONS = [
22
        Request::METHOD_GET => 'index',
23
        Request::METHOD_POST => 'store',
24
        Request::METHOD_PUT => 'update',
25
        Request::METHOD_PATCH => 'update',
26
        Request::METHOD_DELETE => 'delete',
27
        Request::METHOD_OPTIONS => 'preflight',
28
    ];
29
30
    /**
31
     * peel
32
     *
33
     * @param Container $container
34
     * @param \Closure $next
35
     * @return \Closure
36
     */
37 7
    public function peel(Container $container, \Closure $next)
38
    {
39 7
        $this->init($container);
40 7
        $this->process();
41 7
        return $next($container);
42
    }
43
44
    /**
45
     * process
46
     *
47
     */
48 1
    private function process()
49
    {
50 1
        if ($this->enabled) {
51 1
            $this->response->getHeaderManager()->add(
52 1
                self::_SIGN,
53 1
                strval(microtime(true))
54
            );
55 1
            if ($this->required()) {
56 1
                $pureCa = preg_replace('/\?.*/', '', $this->caUri()) . '/';
57 1
                $caFrags = explode('/', $pureCa);
58 1
                $controller = $caFrags[0];
59 1
                $met = $this->request->getMethod();
60 1
                $action = self::METHOD_ACTIONS[$met];
61 1
                $this->kernel->setAction([$controller, $action]);
62
            }
63
        }
64
    }
65
66
    /**
67
     * required
68
     *
69
     * @return boolean
70
     */
71 1
    protected function required(): bool
72
    {
73 1
        return (!$this->isExclude() && $this->requestUriPrefix() === $this->prefix);
74
    }
75
76
    /**
77
     * check exclusion uri fragment from exclude regexps.
78
     * note that negate with ! means allow.
79
     *
80
     * @return Boolean
81
     */
82 1
    protected function isExclude(): bool
83
    {
84 1
        $reqFrag = $this->caUri();
85 1
        $countExc = count($this->exclude);
86 1
        for ($i = 0; $i < $countExc; $i++) {
87 1
            $matches = [];
88 1
            $match = preg_match($this->exclude[$i], $reqFrag, $matches);
89 1
            if ($match) {
90 1
                return true;
91
            }
92
        }
93 1
        return false;
94
    }
95
96
    /**
97
     * return controller action from uri
98
     *
99
     * @return string
100
     */
101 1
    protected function caUri(): string
102
    {
103 1
        return str_replace($this->prefix, '', $this->request->getUri());
104
    }
105
106
    /**
107
     * uriPrefix
108
     *
109
     * @return string
110
     */
111 1
    protected function requestUriPrefix(): string
112
    {
113 1
        return substr($this->request->getUri(), 0, strlen($this->prefix));
114
    }
115
}
116