Test Failed
Push — master ( 0748b7...112f34 )
by Marcio
09:50
created

RouterRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 18
eloc 32
c 1
b 0
f 1
dl 0
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B checkMethods() 0 18 8
A validMethods() 0 3 1
A symfonyRequest() 0 3 1
A getMethod() 0 9 2
A symfonyResponse() 0 3 1
A validMethod() 0 15 4
1
<?php
2
/**
3
 *
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @link      https://github.com/knut7/framework/ for the canonical source repository
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd MIT lIcense
14
 * @author    Marcio Zebedeu - [email protected]
15
 * @version   1.0.14
16
 *
17
 *
18
 */
19
20
namespace Ballybran\Core\Http;
21
22
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ballybran\Core\Http\Response. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class RouterRequest
26
{
27
    /**
28
     * @var string $validMethods Valid methods for Router
29
     */
30
    protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XPOST|XPUT|XDELETE|XPATCH';
31
    /**
32
     * @var Request $request
33
     */
34
    private $request;
35
    /**
36
     * @var Response $response
37
     */
38
    private $response;
39
40
    /**
41
     * RouterRequest constructor.
42
     *
43
     * @param Request  $request
44
     * @param Response $response
45
     */
46
    public function __construct(Request $request, Response $response)
47
    {
48
        $this->request = $request;
49
        $this->response = $response;
50
    }
51
52
    /**
53
     * @return Request
54
     */
55
    public function symfonyRequest(): Request
56
    {
57
        return $this->request;
58
    }
59
60
    /**
61
     * @return Response
62
     */
63
    public function symfonyResponse(): Response
64
    {
65
        return $this->response;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function validMethods(): string
72
    {
73
        return $this->validMethods;
74
    }
75
76
    /**
77
     * Request method validation
78
     *
79
     * @param string $data
80
     * @param string $method
81
     *
82
     * @return bool
83
     */
84
    public function validMethod(string $data, string $method): bool
85
    {
86
        $valid = false;
87
        if (strstr($data, '|')) {
88
            foreach (explode('|', $data) as $value) {
89
                $valid = $this->checkMethods($value, $method);
90
                if ($valid) {
91
                    break;
92
                }
93
            }
94
        } else {
95
            $valid = $this->checkMethods($data, $method);
96
        }
97
98
        return $valid;
99
    }
100
101
    /**
102
     * Get the request method used, taking overrides into account
103
     *
104
     * @return string
105
     */
106
    public function getMethod(): string
107
    {
108
        $method = $this->request->getMethod();
109
        $formMethod = $this->request->request->get('_method');
110
        if (!empty($formMethod)) {
111
            $method = strtoupper($formMethod);
112
        }
113
114
        return $method;
115
    }
116
117
    /**
118
     * check method valid
119
     *
120
     * @param string $value
121
     * @param string $method
122
     *
123
     * @return bool
124
     */
125
    protected function checkMethods(string $value, string $method): bool
126
    {
127
        if (in_array($value, explode('|', $this->validMethods))) {
128
            if ($this->request->isXmlHttpRequest() && $value === 'AJAX') {
129
                return true;
130
            }
131
132
            if ($this->request->isXmlHttpRequest() && strpos($value, 'X') === 0
133
                && $method === ltrim($value, 'X')) {
134
                return true;
135
            }
136
137
            if (in_array($value, [$method, 'ANY'])) {
138
                return true;
139
            }
140
        }
141
142
        return false;
143
    }
144
}
145