Completed
Pull Request — master (#25)
by Michal
15:14 queued 12:43
created

BaseHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 9
Bugs 0 Features 5
Metric Value
wmc 12
c 9
b 0
f 5
lcom 2
cbo 4
dl 0
loc 112
ccs 24
cts 32
cp 0.75
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A description() 0 4 1
A params() 0 4 1
A tags() 0 4 1
A getFractal() 0 7 2
A setEndpointIdentifier() 0 4 1
A getEndpoint() 0 4 1
A setupLinkGenerator() 0 5 1
A createLink() 0 15 3
handle() 0 1 ?
1
<?php
2
3
namespace Tomaj\NetteApi\Handlers;
4
5
use League\Fractal\Manager;
6
use Nette\Application\LinkGenerator;
7
use Nette\InvalidStateException;
8
use Tomaj\NetteApi\EndpointInterface;
9
10
abstract class BaseHandler implements ApiHandlerInterface
11
{
12
    /**
13
     * @var Manager
14
     */
15
    private $fractal;
16
17
    /**
18
     * @var EndpointInterface
19
     */
20
    private $endpoint;
21
22
    /**
23
     * @var  LinkGenerator
24
     */
25
    protected $linkGenerator;
26
27 45
    public function __construct()
28
    {
29 45
        $this->fractal = new Manager();
30 45
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function description()
36
    {
37
        return '';
38
    }
39
    
40
    /**
41
     * {@inheritdoc}
42
     */
43 9
    public function params()
44
    {
45 9
        return [];
46
    }
47
    
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function tags()
52
    {
53
        return [];
54
    }
55
56
    protected function getFractal()
57
    {
58
        if (!$this->fractal) {
59
            throw new InvalidStateException("Fractal manager isnt initialized. Did you call parent::__construct() in your handler constructor?");
60
        }
61
        return $this->fractal;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 24
    public function setEndpointIdentifier(EndpointInterface $endpoint)
68
    {
69 24
        $this->endpoint = $endpoint;
70 24
    }
71
72
    /**
73
     * @return EndpointInterface
74
     */
75 9
    public function getEndpoint()
76
    {
77 9
        return $this->endpoint;
78
    }
79
80
    /**
81
     * Set link generator to handler
82
     *
83
     * @param LinkGenerator $linkGenerator
84
     *
85
     * @return $this
86
     */
87 6
    public function setupLinkGenerator(LinkGenerator $linkGenerator)
88
    {
89 6
        $this->linkGenerator = $linkGenerator;
90 6
        return $this;
91
    }
92
93
    /**
94
     * Create link to actual handler endpoint
95
     *
96
     * @param array   $params
97
     *
98
     * @return string
99
     * @throws \Nette\Application\UI\InvalidLinkException it handler doesn't have linkgenerator or endpoint
100
     */
101 9
    public function createLink($params)
102
    {
103 9
        if (!$this->linkGenerator) {
104 3
            throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler");
105
        }
106 6
        if (!$this->endpoint) {
107 3
            throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler");
108
        }
109 3
        $params = array_merge([
110 3
            'version' => $this->endpoint->getVersion(),
111 3
            'package' => $this->endpoint->getPackage(),
112 3
            'apiAction' => $this->endpoint->getApiAction()
113 3
        ], $params);
114 3
        return $this->linkGenerator->link('Api:Api:default', $params);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    abstract public function handle($params);
121
}
122