Completed
Push — master ( d2c9b2...2cde9b )
by
unknown
08:05 queued 04:31
created

ApiController::initialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2017, 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 - 2017, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Controller;
13
14
use CakeDC\Api\Service\ConfigReader;
15
use CakeDC\Api\Service\ServiceRegistry;
16
use Exception;
17
18
class ApiController extends AppController
19
{
20
21
    /**
22
     * @var ServiceRegistry
23
     */
24
    public $Services;
25
26
    /**
27
     * Initialize controller.
28
     *
29
     * @return void
30
     */
31 56
    public function initialize()
32
    {
33 56
        parent::initialize();
34 56
        if ($this->components()->has('Auth')) {
35 56
            $this->Auth->allow(['process', 'describe', 'listing']);
36 56
        }
37 56
        if ($this->components()->has('RememberMe')) {
38 56
            $this->components()->unload('RememberMe');
39 56
        }
40 56
    }
41
42
    /**
43
     * Process api request
44
     *
45
     * @return \Cake\Http\Client\Response|\Cake\Http\Response|null
46
     */
47 56
    public function process()
48
    {
49 56
        return $this->_process();
50
    }
51
52
    /**
53
     * Process listing api request.
54
     *
55
     * @return void
56
     */
57
    public function listing()
58
    {
59
        $this->request['service'] = 'listing';
60
        $options = [
61
            'className' => 'CakeDC/Api.Listing'
62
        ];
63
        $this->_process($options);
64
    }
65
66
    /**
67
     * Process describe api request.
68
     *
69
     * @return void
70
     */
71
    public function describe()
72
    {
73
        $this->request['service'] = 'describe';
74
        $options = [
75
            'className' => 'CakeDC/Api.Describe'
76
        ];
77
        $this->_process($options);
78
    }
79
80
    /**
81
     * Process api request
82
     *
83
     * @param array $options Options
84
     * @return \Cake\Http\Client\Response|\Cake\Http\Response|null
85
     */
86 56
    protected function _process($options = [])
87
    {
88 56
        $this->autoRender = false;
89
        try {
90 56
            if (!empty($this->request['service'])) {
91 56
                $service = $this->request['service'];
92 56
                $version = null;
93 56
                if (!empty($this->request['version'])) {
94
                    $version = $this->request['version'];
95
                }
96
97 56
                $url = '/' . $service;
98 56
                if (!empty($this->request->getParam('pass'))) {
99 25
                    $url .= '/' . join('/', $this->request->getParam('pass'));
100 25
                }
101
                $options += [
102 56
                    'version' => $version,
103
                    // 'controller' => $this,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104 56
                    'request' => $this->request,
105 56
                    'response' => $this->response,
106 56
                    'baseUrl' => $url,
107
                ];
108 56
                $options += (new ConfigReader())->serviceOptions($service, $version);
109 56
                $Service = ServiceRegistry::get($service, $options);
110 56
                $result = $Service->dispatch();
111
112 56
                return $Service->respond($result);
113
            }
114
            $this->response = $this->response->withStringBody(__('Service not found'))->withStatus(404);
115
        } catch (Exception $e) {
116
            $this->response = $this->response->withStringBody($e->getMessage())->withStatus(400);
117
        }
118
119
        return $this->response;
120
    }
121
}
122