Completed
Push — master ( dfaf51...188ba1 )
by Samuel
02:15
created

Api::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Kelemen\ApiNette;
4
5
use Kelemen\ApiNette\Route\RouteResolver;
6
use Kelemen\ApiNette\Route\Route;
7
use Kelemen\ApiNette\Route\RouteContainer;
8
use Nette\DI\Container;
9
use Nette\DI\MissingServiceException;
10
use Nette\Http\IResponse;
11
use Nette\Http\Request;
12
use Nette\Http\Response;
13
use Kelemen\ApiNette\Response\JsonApiResponse;
14
15
class Api
16
{
17
    /** @var RouteResolver */
18
    private $routeResolver;
19
20
    /** @var RouteContainer */
21
    private $routes;
22
23
    /** @var Request */
24
    private $request;
25
26
    /** @var Response */
27
    private $response;
28
29
    /** @var Container */
30
    private $container;
31
32
    /**
33
     * @param RouteResolver $routeResolver
34
     * @param Request $request
35
     * @param Response $response
36
     * @param Container $container
37
     */
38 4
    public function __construct(
39
        RouteResolver $routeResolver,
40
        Request $request,
41
        Response $response,
42
        Container $container
43
    ) {
44 4
        $this->routeResolver = $routeResolver;
45 4
        $this->request = $request;
46 4
        $this->response = $response;
47 4
        $this->container = $container;
48 4
        $this->routes = new RouteContainer();
49 4
    }
50
51
    /**
52
     * Add api call
53
     * @param string $method
54
     * @param string $pattern
55
     * @param string $handler
56
     * @param array $params
57
     */
58 2
    public function add($method, $pattern, $handler, $params = [])
59
    {
60 2
        $this->routes->add(new Route($method, $pattern, $handler, $params));
61 2
    }
62
63
    /**
64
     * Add get api call
65
     * @param string $pattern
66
     * @param string $handler
67
     * @param array $params
68
     */
69
    public function get($pattern, $handler, $params = [])
70
    {
71
        $this->add('get', $pattern, $handler, $params);
72
    }
73
74
    /**
75
     * Add post api call
76
     * @param string $pattern
77
     * @param string $handler
78
     * @param array $params
79
     */
80 2
    public function post($pattern, $handler, $params = [])
81
    {
82 2
        $this->add('post', $pattern, $handler, $params);
83 2
    }
84
85
    /**
86
     * Add put api call
87
     * @param string $pattern
88
     * @param string $handler
89
     * @param array $params
90
     */
91
    public function put($pattern, $handler, $params = [])
92
    {
93
        $this->add('put', $pattern, $handler, $params);
94
    }
95
96
    /**
97
     * Add patch api call
98
     * @param string $pattern
99
     * @param string $handler
100
     * @param array $params
101
     */
102
    public function patch($pattern, $handler, $params = [])
103
    {
104
        $this->add('patch', $pattern, $handler, $params);
105
    }
106
107
    /**
108
     * Add delete api call
109
     * @param string $pattern
110
     * @param string $handler
111
     * @param array $params
112
     */
113
    public function delete($pattern, $handler, $params = [])
114
    {
115
        $this->add('delete', $pattern, $handler, $params);
116
    }
117
118
    /**
119
     * Add options api call
120
     * @param string $pattern
121
     * @param string $handler
122
     * @param array $params
123
     */
124
    public function options($pattern, $handler, $params = [])
125
    {
126
        $this->add('options', $pattern, $handler, $params);
127
    }
128
129
    /**
130
     * Run api
131
     * @param string $url
132
     * @return JsonApiResponse
133
     */
134 4
    public function run($url)
135
    {
136 4
        $resolvedRoute = $this->routeResolver->resolve($this->routes, $url);
137 4
        if (!$resolvedRoute) {
138 2
            return new JsonApiResponse(IResponse::S400_BAD_REQUEST, ['error' => 'Unresolved api route']);
139
        }
140
141 2
        $handler = $this->getFromContainer($resolvedRoute->getRoute()->getHandler());
142 2
        if (!$handler) {
143
            // @TODO: log error
144 2
            return new JsonApiResponse(IResponse::S500_INTERNAL_SERVER_ERROR, ['error' => 'Internal server error']);
145
        }
146
147
        $handler->prepare($resolvedRoute);
148
        $middleware = $resolvedRoute->getRoute()->getConfig('middleware');
149
        $runner = new Runner($middleware ?: [], $handler, $this->container);
150
        $response = $runner($this->request, $this->response);
151
        return $response;
152
    }
153
154
    /**
155
     * Get service by type or name from container
156
     * @param string $entry
157
     * @return bool|object
158
     */
159 2 View Code Duplication
    private function getFromContainer($entry)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        try {
162 2
            if (substr($entry, 0, 1) === '#') {
163 2
                return $this->container->getService(substr($entry, 1));
164
            }
165
166
            return $this->container->getByType($entry);
167 2
        } catch (MissingServiceException $e) {
168 2
            return false;
169
        }
170
    }
171
}
172