Completed
Pull Request — feature/app (#7)
by Yo
01:44
created

MethodManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 10 2
A validateParamsIfNeeded() 0 9 3
A __construct() 0 4 1
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Manager;
3
4
use Yoanm\JsonRpcServer\App\Creator\CustomExceptionCreator;
5
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
6
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface;
7
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface;
8
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException;
9
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
10
11
/**
12
 * Class MethodManager
13
 */
14
class MethodManager
15
{
16
    /** @var MethodResolverInterface */
17
    private $methodResolver;
18
    /** @var CustomExceptionCreator */
19
    private $customExceptionCreator;
20
21
    /**
22
     * @param MethodResolverInterface $methodResolver
23
     * @param CustomExceptionCreator  $customExceptionCreator
24
     */
25 3
    public function __construct(MethodResolverInterface $methodResolver, CustomExceptionCreator $customExceptionCreator)
26
    {
27 3
        $this->methodResolver = $methodResolver;
28 3
        $this->customExceptionCreator = $customExceptionCreator;
29 3
    }
30
31
    /**
32
     * @param string $methodName
33
     * @param array  $paramList
34
     *
35
     * @return mixed
36
     *
37
     * @throws JsonRpcInvalidParamsException
38
     * @throws JsonRpcMethodNotFoundException
39
     * @throws JsonRpcExceptionInterface
40
     */
41 3
    public function apply(string $methodName, array $paramList = null)
42
    {
43 3
        $method = $this->methodResolver->resolve($methodName);
44
45 3
        $this->validateParamsIfNeeded($method, $methodName, $paramList);
0 ignored issues
show
Bug introduced by
It seems like $paramList can also be of type null; however, parameter $paramList of Yoanm\JsonRpcServer\App\...alidateParamsIfNeeded() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $this->validateParamsIfNeeded($method, $methodName, /** @scrutinizer ignore-type */ $paramList);
Loading history...
46
47
        try {
48 2
            return $method->apply($paramList);
49 1
        } catch (\Exception $applyException) {
50 1
            throw $this->customExceptionCreator->createFor($applyException);
51
        }
52
    }
53
54
    /**
55
     * @param JsonRpcMethodInterface $method
56
     * @param string                 $methodName
57
     * @param array                  $paramList
58
     *
59
     * @throws JsonRpcInvalidParamsException
60
     *
61
     * @return void
62
     */
63 3
    private function validateParamsIfNeeded(JsonRpcMethodInterface $method, string $methodName, array $paramList)
64
    {
65 3
        if (is_array($paramList)) {
0 ignored issues
show
introduced by
The condition is_array($paramList) is always true.
Loading history...
66
            try {
67 3
                $method->validateParams($paramList);
68 1
            } catch (\Exception $validationException) {
69 1
                throw new JsonRpcInvalidParamsException(
70 1
                    $methodName,
71 1
                    $validationException->getMessage()
72
                );
73
            }
74
        }
75 2
    }
76
}
77