Completed
Push — master ( 6d2274...b3864e )
by Pierre
02:41
created

Cors::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 28
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 34
ccs 24
cts 24
cp 1
crap 4
rs 9.472
1
<?php
2
3
namespace App\Middlewares;
4
5
use App\Http\Request;
6
use App\Http\Response;
7
use App\Http\Headers;
8
use App\Http\Interfaces\Middleware\ILayer;
9
use App\Container;
10
11
/**
12
 * App\Middleware\Cors
13
 *
14
 * Patch initial response to accept CORS requests
15
 */
16
class Cors implements ILayer
17
{
18
19
    use \App\Middlewares\Reuse\TInit;
20
21
    const _PREFLIGHT = 'preflight';
22
    const _SIGN = 'X-Middleware-Cors';
23
24
    /**
25
     * peel
26
     *
27
     * @param Container $container
28
     * @param \Closure $next
29
     * @return \Closure
30
     */
31 8
    public function peel(Container $container, \Closure $next)
32
    {
33 8
        $this->init($container);
34 8
        $this->process();
35 8
        return $next($container);
36
    }
37
38
    /**
39
     * process
40
     *
41
     */
42 2
    private function process()
43
    {
44 2
        if ($this->enabled) {
45 2
            $this->response->getHeaderManager()->add(
46 2
                self::_SIGN,
47 2
                microtime(true)
48
            );
49 2
            if ($this->required()) {
50 2
                if (Request::METHOD_OPTIONS == $this->request->getMethod()) {
51 1
                    $pureCa = preg_replace('/\?.*/', '', $this->caUri()) . '/';
52 1
                    $caFrags = explode('/', $pureCa);
53 1
                    $controller = $caFrags[0];
54 1
                    $this->kernel->setAction([$controller, self::_PREFLIGHT]);
55
                }
56 2
                $this->response
57 2
                    ->setCode(Response::HTTP_NOT_FOUND)
58 2
                    ->setContent([
59 2
                        Response::_ERROR_CODE => Response::HTTP_NOT_FOUND,
60
                        Response::_ERROR_MSG => 'Not found'
61
                    ])
62 2
                    ->getHeaderManager()
63 2
                    ->add(Headers::CONTENT_TYPE, 'application/json; charset=utf-8')
64 2
                    ->addMany([
65 2
                        Headers::HEADER_ACA_ORIGIN  =>  '*',
66 2
                        Headers::HEADER_ACA_CREDENTIALS => 'true',
67 2
                        Headers::HEADER_ACA_METHODS => implode(',', [
68 2
                            Request::METHOD_GET, Request::METHOD_POST, Request::METHOD_PUT,
69
                            Request::METHOD_DELETE, Request::METHOD_OPTIONS
70
                        ]),
71 2
                        Headers::HEADER_ACA_HEADERS =>  implode(',', [
72 2
                            'Access-Control-Allow-Headers', 'Origin', 'Accept',
73
                            'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method',
74
                            'Access-Control-Request-Headers', 'Access-Control-Allow-Origin',
75
                            'Authorization'
76
                        ])
77
                    ]);
78
            }
79
        }
80
    }
81
82
    /**
83
     * required
84
     *
85
     * @return boolean
86
     */
87 1
    protected function required(): bool
88
    {
89 1
        return (!$this->isExclude()
90 1
            && $this->requestUriPrefix() === $this->prefix);
91
    }
92
93
    /**
94
     * isExclude
95
     *
96
     * @return Boolean
97
     */
98 1
    protected function isExclude(): bool
99
    {
100 1
        $disallowed = $this->configParams['exclude'];
101 1
        $countEx = count($disallowed);
102 1
        for ($c = 0; $c < $countEx; ++$c) {
103 1
            $excludeUri = $this->prefix . $disallowed[$c];
104 1
            $isExclude = ($excludeUri == $this->request->getUri());
105 1
            if ($isExclude) {
106 1
                return true;
107
            }
108
        }
109 1
        return false;
110
    }
111
112
    /**
113
     * return controller action from uri
114
     *
115
     * @return string
116
     */
117 1
    protected function caUri():string
118
    {
119 1
        return str_replace($this->prefix, '', $this->request->getUri());
120
    }
121
122
    /**
123
     * uriPrefix
124
     *
125
     * @return string
126
     */
127 1
    protected function requestUriPrefix(): string
128
    {
129 1
        return substr($this->request->getUri(), 0, strlen($this->prefix));
130
    }
131
}
132