CorsMiddleware::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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
38
        if (null !== $logger) {
39
            $this->analyzer->setLogger($logger);
40
        }
41
    }
42
43
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
44
    {
45
        $cors = $this->analyzer->analyze($request);
46
47
        switch ($cors->getRequestType()) {
48
            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...
49
            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...
50
            case AnalysisResultInterface::ERR_METHOD_NOT_SUPPORTED:
51
            case AnalysisResultInterface::ERR_HEADERS_NOT_SUPPORTED:
52
                return $response->withStatus(403);
53
            case AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE:
54
                return $next($request, $response);
55
            case AnalysisResultInterface::TYPE_PRE_FLIGHT_REQUEST:
56
                foreach ($cors->getResponseHeaders() as $name => $value) {
57
                    $response = $response->withHeader($name, $value);
58
                }
59
60
                return $response->withStatus(200);
61
            default:
62
                foreach ($cors->getResponseHeaders() as $name => $value) {
63
                    $response = $response->withHeader($name, $value);
64
                }
65
66
                return $next($request, $response);
67
        }
68
    }
69
70
    /**
71
     * @return SettingsStrategyInterface
72
     */
73
    public function getSettings()
74
    {
75
        return $this->settings;
76
    }
77
78
    /**
79
     * @param AnalyzerInterface $analyzer
80
     *
81
     * @return $this
82
     */
83
    public function setAnalyzer($analyzer)
84
    {
85
        $this->analyzer = $analyzer;
86
87
        return $this;
88
    }
89
}
90