Completed
Push — master ( 495169...b31514 )
by Changwan
10:11
created

Dispatcher::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Router;
3
4
use Closure;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Wandu\Router\Contracts\Dispatchable;
7
use Wandu\Router\Contracts\LoaderInterface;
8
use Wandu\Router\Contracts\ResponsifierInterface;
9
use Wandu\Router\Loader\SimpleLoader;
10
use Wandu\Router\Responsifier\NullResponsifier;
11
12
class Dispatcher
13
{
14
    /** @var \Wandu\Router\Contracts\LoaderInterface */
15
    protected $loader;
16
17
    /** @var \Wandu\Router\Responsifier\NullResponsifier */
18
    protected $responsifier;
19
20
    /** @var array */
21
    protected $options;
22
    
23 5
    public function __construct(
24
        LoaderInterface $loader = null,
25
        ResponsifierInterface $responsifier = null,
26
        array $options = []
27
    ) {
28 5
        $this->loader = $loader ?: new SimpleLoader();
29 5
        $this->responsifier = $responsifier ?: new NullResponsifier();
0 ignored issues
show
Documentation Bug introduced by
$responsifier ?: new \Wa...fier\NullResponsifier() is of type object<Wandu\Router\Cont...\ResponsifierInterface>, but the property $responsifier was declared to be of type object<Wandu\Router\Resp...ifier\NullResponsifier>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
30 5
        $this->setOptions($options);
31 5
    }
32
33
    /**
34
     * @param array $options
35
     */
36 5
    public function setOptions(array $options)
37
    {
38 5
        $this->options = array_merge([
39 5
            'method_override_enabled' => true,
40
            'method_spoofing_enabled' => false,
41
            'defined_prefix' => '',
42
            'defined_middlewares' => [],
43
            'defined_domains' => [],
44 5
        ], $options);
45 5
    }
46
47
    /**
48
     * @return \Wandu\Router\RouteCollection
49
     */
50 4
    public function createRouteCollection(): RouteCollection
51
    {
52 4
        return new RouteCollection(
53 4
            $this->options['defined_prefix'],
54 4
            $this->options['defined_middlewares'],
55 4
            $this->options['defined_domains']
56
        );
57
    }
58
    
59
    /**
60
     * @param \Wandu\Router\Contracts\Dispatchable $dispatcher
61
     * @param \Psr\Http\Message\ServerRequestInterface $request
62
     * @return \Psr\Http\Message\ResponseInterface
63
     */
64 9
    public function dispatch(Dispatchable $dispatcher, ServerRequestInterface $request)
65
    {
66 9
        return $dispatcher->dispatch($this->loader, $this->responsifier, $this->applyVirtualMethod($request));
67
    }
68
69
    /**
70
     * @param \Psr\Http\Message\ServerRequestInterface $request
71
     * @return \Psr\Http\Message\ServerRequestInterface
72
     */
73 9
    protected function applyVirtualMethod(ServerRequestInterface $request)
74
    {
75 9
        if ($this->options['method_override_enabled'] && $request->hasHeader('X-Http-Method-Override')) {
76 2
            return $request->withMethod(strtoupper($request->getHeaderLine('X-Http-Method-Override')));
77
        }
78 7
        if ($this->options['method_spoofing_enabled']) {
79 1
            $parsedBody = $request->getParsedBody();
80 1
            if (isset($parsedBody['_method'])) {
81 1
                return $request->withMethod($parsedBody['_method']);
82
            }
83
        }
84 6
        return $request;
85
    }
86
}
87