Completed
Push — phpstan ( 995292...330403 )
by Akihito
07:50 queued 05:42
created

NamedParameter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getParameters() 0 11 2
A getErrorMessage() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Exception\ParameterException;
8
use function get_class;
9
use function is_array;
10
use Ray\Aop\WeavedInterface;
11
use Ray\Di\InjectorInterface;
12
13
final class NamedParameter implements NamedParameterInterface
14
{
15
    /**
16
     * @var InjectorInterface
17
     */
18
    private $injector;
19
20
    /**
21
     * @var NamedParamMetasInterface
22
     */
23
    private $paramMetas;
24
25
    public function __construct(NamedParamMetasInterface $paramMetas, InjectorInterface $injector)
26
    {
27
        $this->paramMetas = $paramMetas;
28
        $this->injector = $injector;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getParameters(callable $callable, array $query) : array
35
    {
36
        $metas = ($this->paramMetas)($callable);
37
        $parameters = [];
38
        foreach ($metas as $varName => $param) {
39
            /* @var $param ParamInterface */
40
            $parameters[] = $param($varName, $query, $this->injector);
41
        }
42
43
        return $parameters;
44
    }
45
46
    private function getErrorMessage(callable $callable, ParameterException $e) : string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
47
    {
48
        if (is_array($callable) && count($callable) === 2) {
49
            $object = $callable[0];
50
            $class = $callable[0] instanceof WeavedInterface ? (new \ReflectionClass($object))->getParentClass()->getName() : get_class($object);
51
52
            return sprintf('%s in %s::%s', $e->getMessage(), $class, (string) $callable[1]);
53
        }
54
55
        return sprintf('%s', $e->getMessage());
56
    }
57
}
58