Passed
Pull Request — master (#2)
by Sergey
03:30
created

ApiExecService::getMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * Project: json_rpc_server
6
 * User: sv
7
 * Date: 25.06.19
8
 * Time: 7:54
9
 */
10
11
declare(strict_types=1);
12
13
namespace Onnov\JsonRpcServer\Service;
14
15
use JsonMapper;
16
use JsonMapper_Exception;
17
use Onnov\JsonRpcServer\ApiMethodAbstract;
18
use Onnov\JsonRpcServer\ApiMethodInterface;
19
use Onnov\JsonRpcServer\Exception\InternalErrorException;
20
use Onnov\JsonRpcServer\Exception\MethodNotFoundException;
21
use Onnov\JsonRpcServer\Exception\ParseErrorException;
22
use Onnov\JsonRpcServer\Model\RpcModel;
23
use Onnov\JsonRpcServer\Model\RpcRequest;
24
use Onnov\JsonRpcServer\Model\RunModel;
25
use Onnov\JsonRpcServer\Traits\JsonHelperTrait;
26
use Onnov\JsonRpcServer\Validator\JsonRpcSchema;
27
use Onnov\JsonRpcServer\Validator\JsonSchemaValidator;
28
29
class ApiExecService
30
{
31 1
    use JsonHelperTrait;
32
33
    /** @var JsonSchemaValidator */
34
    protected $validator;
35
36
    /** @var JsonRpcSchema */
37
    protected $rpcSchema;
38
39
    /** @var JsonMapper */
40
    private $mapper;
41
42
    /**
43
     * ApiExecService constructor.
44
     *
45
     * @param JsonSchemaValidator $validator
46
     * @param JsonRpcSchema $rpcSchema
47
     * @param JsonMapper $mapper
48
     */
49 6
    public function __construct(
50
        JsonSchemaValidator $validator,
51
        JsonRpcSchema $rpcSchema,
52
        JsonMapper $mapper
53
    ) {
54 6
        $this->validator = $validator;
55 6
        $this->rpcSchema = $rpcSchema;
56 6
        $this->mapper = $mapper;
57 6
    }
58
59
    /**
60
     * @param RunModel $model
61
     * @param RpcModel $rpc
62
     *
63
     * @return mixed
64
     */
65 3
    public function exe(
66
        RunModel $model,
67
        RpcModel $rpc
68
    ) {
69 3
        $factory = $model->getApiFactory();
70 3
        $method = $rpc->getMethod();
71
        /** Проверим существование метода */
72 3
        if ($factory->has($method) === false) {
73 1
            throw new MethodNotFoundException(
74 1
                'Method "' . $method . '" not found'
75
            );
76
        }
77
78
        /** Создаем экземпляр класса
79
         *
80
         * @var ApiMethodAbstract $class
81
         */
82 2
        $class = $factory->get($method);
83
84
        /** Проверим соответствие интерфейсу */
85 2
        $interfaces = (array)class_implements($class);
86
        if (
87 2
            (bool)$interfaces === false
88 2
            || in_array(ApiMethodInterface::class, $interfaces, true) === false
89
        ) {
90
            throw new InternalErrorException(
91
                'Method "' . $method . '" does not match Interface'
92
            );
93
        }
94
95
        /** Валидируем парамертры ЗАПРОСА */
96 2
        if ($class->requestSchema() !== null) {
97
            $this->getValidator()->validate(
98
                $class->requestSchema(),
99
                $rpc->getParams(),
100
                'requestParams'
101
            );
102
        }
103
104 2
        $paramsObject = null;
105 2
        if ($class->customParamsObject() !== null) {
106
            try {
107
                $paramsObject = $this->getMapper()->map($rpc->getParams(), $class->customParamsObject());
108
            } catch (JsonMapper_Exception $e) {
109
                throw new ParseErrorException(
110
                    $e->getMessage(),
111
                    $e->getCode(),
112
                    $e->getPrevious()
113
                );
114
            }
115
        }
116
117
        /** засетим в метод RpcRequest */
118 2
        $class->setRpcRequest(new RpcRequest($rpc, $paramsObject));
119
120
        /** Выполним метод */
121 2
        $res = $class->execute()->getResult();
122
123 2
        if ($model->isResponseCheck() && $class->responseSchema() !== null) {
124
            /** Валидируем парамертры ОТВЕТА */
125
            $this->getValidator()->validate(
126
                $class->responseSchema(),
127
                $res,
128
                'responseParams'
129
            );
130
        }
131
132 2
        return $res;
133
    }
134
135
    /**
136
     * @return JsonSchemaValidator
137
     */
138
    public function getValidator(): JsonSchemaValidator
139
    {
140
        return $this->validator;
141
    }
142
143
    /**
144
     * @return JsonRpcSchema
145
     */
146
    public function getRpcSchema(): JsonRpcSchema
147
    {
148
        return $this->rpcSchema;
149
    }
150
151
    /**
152
     * @return JsonMapper
153
     */
154
    public function getMapper(): JsonMapper
155
    {
156
        return $this->mapper;
157
    }
158
}
159