MatchScoreTargetSorter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMatchingTarget() 0 18 4
1
<?php
2
3
namespace Bankiru\Seo\Target;
4
5
use Bankiru\Seo\DestinationInterface;
6
use Bankiru\Seo\Exception\MatchingException;
7
use Bankiru\Seo\TargetRepositoryInterface;
8
9
final class MatchScoreTargetSorter implements TargetSorter
10
{
11
    /** @var TargetRepositoryInterface */
12
    private $targetRepository;
13
14
    /**
15
     * MatchScoreTargetSorter constructor.
16
     *
17
     * @param TargetRepositoryInterface $targetRepository
18
     */
19 10
    public function __construct(TargetRepositoryInterface $targetRepository)
20
    {
21 10
        $this->targetRepository = $targetRepository;
22 10
    }
23
24
    /** {@inheritdoc} */
25 9
    public function getMatchingTarget(DestinationInterface $destination)
26
    {
27 9
        $sorted = [];
28 9
        foreach ($this->targetRepository->findByRoute($destination->getRoute()) as $target) {
29 7
            $score = $target->match($destination);
30 7
            if (null !== $score) {
31 4
                $sorted[$score] = $target;
32 4
            }
33 9
        }
34
35 9
        if (count($sorted) === 0) {
36 5
            throw MatchingException::noTarget();
37
        }
38
39 4
        krsort($sorted);
40
41 4
        return array_shift($sorted);
42 1
    }
43
}
44