Completed
Push — master ( 8ec403...fa21f3 )
by Evgeny
03:18
created

DescribeAction::validates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 13
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016, 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, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action;
13
14
use CakeDC\Api\Exception\ValidationException;
15
use CakeDC\Api\Service\CrudService;
16
use CakeDC\Api\Service\ServiceRegistry;
17
use Cake\Validation\Validator;
18
19
/**
20
 * Class DescribeAction
21
 *
22
 * @package CakeDC\Api\Service\Action
23
 */
24
class DescribeAction extends Action
25
{
26
27
    /**
28
     * Apply validation process.
29
     *
30
     * @return bool|array
31
     */
32
    public function validates()
33
    {
34
        $validator = new Validator();
35
        $validator
36
            ->requirePresence('service', 'create')
37
            ->notEmpty('service');
38
        $errors = $validator->errors($this->data());
39
        if (!empty($errors)) {
40
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
41
        }
42
43
        return true;
44
    }
45
46
    /**
47
     * Describe service.
48
     * For services that inherited from CrudService it provides action description using CrudDescribeAction.
49
     *
50
     * @return mixed
51
     */
52 1
    public function execute()
53
    {
54 1
        $serviceName = $this->data()['service'];
55 1
        $service = ServiceRegistry::get($serviceName);
56 1
        if ($service instanceof CrudService) {
57
            $route = [
58 1
                'plugin' => null,
59 1
                'controller' => $serviceName,
60 1
                'action' => 'describe',
61 1
                '_method' => 'OPTIONS',
62 1
                'pass' => [],
63 1
                'map' => [],
64 1
                '_matchedRoute' => '/' . $serviceName,
65 1
            ];
66 1
            $action = $service->buildActionClass('\CakeDC\Api\Service\Action\CrudDescribeAction', $route);
67 1
            $service->request($this->service()->request());
68 1
            $service->response($this->service()->response());
69
70 1
            return $action->execute();
71
        }
72
73
        return [];
74
    }
75
}
76