Completed
Push — master ( be7a6a...a2a00e )
by David
04:11
created

NoCandidateFoundException::stringify()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.5555
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 30
1
<?php
2
3
namespace Http\Discovery\Exception;
4
5
use Http\Discovery\Exception;
6
7
/**
8
 * When we have used a strategy but no candidates provided by that strategy could be used.
9
 *
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
final class NoCandidateFoundException extends \Exception implements Exception
13
{
14
    /**
15
     * @param string $strategy
16
     * @param array  $candidates
17
     */
18 7
    public function __construct($strategy, array $candidates)
19
    {
20 7
        $classes = array_map(
21 7
            function ($a) {
22
                return $a['class'];
23 7
            },
24
            $candidates
25 7
        );
26
27 7
        $message = sprintf(
28 7
            'No valid candidate found using strategy "%s". We tested the following candidates: %s.',
29 7
            $strategy,
30 7
            implode(', ', array_map([$this, 'stringify'], $classes))
31 7
        );
32
33 7
        parent::__construct($message);
34 7
    }
35
36
    private function stringify($mixed)
37
    {
38
        if (is_string($mixed)) {
39
            return $mixed;
40
        }
41
42
        if (is_array($mixed) && 2 === count($mixed)) {
43
            return sprintf('%s::%s', $this->stringify($mixed[0]), $mixed[1]);
44
        }
45
46
        return is_object($mixed) ? get_class($mixed) : gettype($mixed);
47
    }
48
}
49