ApiMiddleware::__invoke()   B
last analyzed

Complexity

Conditions 7
Paths 84

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 7.2694

Importance

Changes 0
Metric Value
cc 7
nc 84
nop 3
dl 0
loc 47
rs 8.223
c 0
b 0
f 0
ccs 28
cts 34
cp 0.8235
crap 7.2694
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Middleware;
13
14
use CakeDC\Api\Service\ConfigReader;
15
use CakeDC\Api\Service\ServiceRegistry;
16
use Cake\Core\Configure;
17
use Exception;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
/**
22
 * Applies routing rules to the request and creates the controller
23
 * instance if possible.
24
 */
25
class ApiMiddleware
26
{
27
28
    /**
29
     * @param \Psr\Http\Message\ServerRequestInterface $request The request.
30
     * @param \Psr\Http\Message\ResponseInterface $response The response.
31
     * @param callable $next The next middleware to call.
32
     * @return \Psr\Http\Message\ResponseInterface A response.
33
     */
34 56
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
35
    {
36 56
        $prefix = Configure::read('Api.prefix');
37 56
        if (empty($prefix)) {
38 56
            $prefix = 'api';
39 56
        }
40 56
        $useVersioning = Configure::read('Api.useVersioning');
41 56
        if ($useVersioning) {
42
            $versionPrefix = Configure::read('Api.versionPrefix');
43
            $expr = '#/' . $prefix . '/(?<version>' . $versionPrefix . '\d+)' . '/' . '(?<service>[^/?]+)' . '(?<base>/?.*)#';
44
        } else {
45 56
            $expr = '#/' . $prefix . '/' . '(?<service>[^/?]+)' . '(?<base>/?.*)#';
46
        }
47
48 56
        $path = $request->getUri()->getPath();
49 56
        if (preg_match($expr, $path, $matches)) {
50 56
            $version = isset($matches['version']) ? $matches['version'] : null;
51 56
            $service = $matches['service'];
52
53 56
            $url = '/' . $service;
54 56
            if (!empty($matches['base'])) {
55 25
                $url .= $matches['base'];
56 25
            }
57
            $options = [
58 56
                'service' => $service,
59 56
                'version' => $version,
60 56
                'request' => $request,
61 56
                'response' => $response,
62 56
                'baseUrl' => $url,
63 56
            ];
64
65
            try {
66 56
                $options += (new ConfigReader())->serviceOptions($service, $version);
67 56
                $Service = ServiceRegistry::getServiceLocator()->get($service, $options);
68 56
                $result = $Service->dispatch();
69
70 56
                $response = $Service->respond($result);
71 56
            } catch (Exception $e) {
72
                $response->withStatus(400);
73
                $response = $response->withStringBody($e->getMessage());
74
            }
75
76 56
            return $response;
77
        }
78
79
        return $next($request, $response);
80
    }
81
}
82