RequestObjectFactory::validateParams()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 4
nc 2
nop 1
crap 4
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Request;
12
13
use Strider2038\JsonRpcClient\Exception\InvalidRequestParamsException;
14
15
/**
16
 * @author Igor Lazarev <[email protected]>
17
 */
18
class RequestObjectFactory
19
{
20
    private IdGeneratorInterface $idGenerator;
21
22
    public function __construct(IdGeneratorInterface $idGenerator)
23 56
    {
24
        $this->idGenerator = $idGenerator;
25 56
    }
26 56
27
    /**
28
     * @param array|object|null $params
29
     *
30
     * @throws InvalidRequestParamsException
31
     */
32
    public function createRequest(string $method, $params = null): RequestObject
33 40
    {
34
        $this->validateParams($params);
35 40
        $id = $this->idGenerator->generateId();
36 39
37
        return new RequestObject($id, $method, $params);
38 39
    }
39
40
    /**
41
     * @param array|object|null $params
42
     *
43
     * @throws InvalidRequestParamsException
44
     */
45
    public function createNotification(string $method, $params = null): NotificationObject
46 13
    {
47
        $this->validateParams($params);
48 13
49
        return new NotificationObject($method, $params);
50 12
    }
51
52
    private function validateParams($params): void
53 46
    {
54
        if (null !== $params && !is_object($params) && !is_array($params)) {
55 46
            throw new InvalidRequestParamsException($params);
56 2
        }
57
    }
58
}
59