Completed
Push — master ( bce25e...d1b735 )
by Daniel
03:13
created

AgentFinder::getAgent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ObjectAgent;
6
7
class AgentFinder
8
{
9
    private $agents;
10
11
    public function __construct(array $agents)
12
    {
13
        array_map(function (AgentInterface $agent) {
0 ignored issues
show
Unused Code introduced by
The parameter $agent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
        }, $agents);
15
        $this->agents = $agents;
16
    }
17
18
    public function findAgentFor(string $classFqn)
19
    {
20
        foreach ($this->agents as $agent) {
21
            if ($agent->supports($classFqn)) {
22
                return $agent;
23
            }
24
        }
25
26
        $classes = array_map(function ($element) {
27
            return get_class($element);
28
        }, $this->agents);
29
30
        throw new Exception\AgentNotFoundException(sprintf(
31
            'Could not find an agent supporting class "%s". Registered agents: "%s"',
32
            $classFqn, implode('", "', $classes)
33
        ));
34
    }
35
}
36