Completed
Push — master ( c5fa82...4254ea )
by Aurimas
10:02
created

CorsMiddleware::getSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thruster\Component\HttpMiddlewares;
4
5
use Neomerx\Cors\Analyzer;
6
use Neomerx\Cors\Contracts\AnalysisResultInterface;
7
use Neomerx\Cors\Contracts\AnalyzerInterface;
8
use Neomerx\Cors\Contracts\Strategies\SettingsStrategyInterface;
9
use Neomerx\Cors\Strategies\Settings;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Log\LoggerInterface;
13
14
/**
15
 * Class CorsMiddleware
16
 *
17
 * @package Thruster\Component\HttpMiddlewares
18
 * @author  Aurimas Niekis <[email protected]>
19
 */
20
class CorsMiddleware
21
{
22
    /**
23
     * @var AnalyzerInterface
24
     */
25
    private $analyzer;
26
27
    /**
28
     * @var SettingsStrategyInterface
29
     */
30
    private $settings;
31
32
    public function __construct(SettingsStrategyInterface $settings = null, LoggerInterface $logger = null)
33
    {
34
        $this->settings = $settings ?? new Settings();
35
36
        $this->analyzer = Analyzer::instance($this->settings);
37
        $this->analyzer->setLogger($logger);
0 ignored issues
show
Bug introduced by
It seems like $logger defined by parameter $logger on line 32 can be null; however, Psr\Log\LoggerAwareInterface::setLogger() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
38
    }
39
40
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        $cors = $this->analyzer->analyze($request);
43
44
        switch ($cors->getRequestType()) {
45
            case AnalysisResultInterface::ERR_NO_HOST_HEADER:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
46
            case AnalysisResultInterface::ERR_ORIGIN_NOT_ALLOWED:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
47
            case AnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED:
48
            case AnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED:
49
                return $response->withStatus(403);
50
            case AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE:
51
                return $next($request, $response);
52
            case AnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST:
53
                foreach ($cors->getResponseHeaders() as $name => $value) {
54
                    $response = $response->withHeader($name, $value);
55
                }
56
                return $response->withStatus(200);
57
            default:
58
                /** @var ResponseInterface $response */
59
                $response = $next($request, $response);
60
61
                foreach ($cors->getResponseHeaders() as $name => $value) {
62
                    $response = $response->withHeader($name, $value);
63
                }
64
65
                return $response;
66
        }
67
    }
68
69
    /**
70
     * @return SettingsStrategyInterface
71
     */
72
    public function getSettings()
73
    {
74
        return $this->settings;
75
    }
76
77
    /**
78
     * @param AnalyzerInterface $analyzer
79
     *
80
     * @return $this
81
     */
82
    public function setAnalyzer($analyzer)
83
    {
84
        $this->analyzer = $analyzer;
85
86
        return $this;
87
    }
88
}
89