Completed
Push — master ( cd83e1...f37936 )
by devosc
03:15
created

Plugin::params()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Url;
7
8
use Mvc5\Arg;
9
use Mvc5\Request\Request;
10
11
class Plugin
12
{
13
    /**
14
     * @var callable
15
     */
16
    protected $generator;
17
18
    /**
19
     * @var Request
20
     */
21
    protected $request;
22
23
    /**
24
     * @param Request $request
25
     * @param callable $generator
26
     */
27 7
    function __construct(Request $request, callable $generator)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
    {
29 7
        $this->generator = $generator;
30 7
        $this->request   = $request;
31 7
    }
32
33
    /**
34
     * @return callable
35
     */
36 3
    protected function generator()
37
    {
38 3
        return $this->generator;
39
    }
40
41
    /**
42
     * @param null|string $name
43
     * @return string
44
     */
45 3
    protected function name($name = null)
46
    {
47 3
        return $name ?? $this->request->name();
48
    }
49
50
    /**
51
     * @param array $options
52
     * @return array
53
     */
54 2
    protected function options(array $options = [])
55
    {
56
        return $options + [
57 2
            Arg::HOST   => $this->request->host(),
58 2
            Arg::PORT   => $this->request->port(),
59 2
            Arg::SCHEME => $this->request->scheme()
60
        ];
61
    }
62
63
    /**
64
     * @param $name
65
     * @param array $params
66
     * @return array
67
     */
68
    protected function params($name, array $params = [])
69 2
    {
70
        return $name ? $params : $params + $this->request->params();
71 2
    }
72
73
    /**
74
     * @param string $name
75
     * @param array $params
76
     * @param array $options
77
     * @return string
78
     */
79
    protected function url($name, array $params = [], array $options = [])
80 1
    {
81
        return ($this->generator())($name, $params, $options);
82 1
    }
83
84
    /**
85
     * @param null $name
86
     * @param array $params
87
     * @param array $options
88
     * @return string
89
     */
90
    function __invoke($name = null, array $params = [], array $options = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
    {
92
        return $this->url($this->name($name), $this->params($name, $params), $this->options($options));
93
    }
94
}
95