AdapterResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\analyzer;
13
14
use cloak\analyzer\adapter\AdapterNotFoundException;
15
use cloak\analyzer\adapter\AdapterNotAvailableException;
16
17
18
/**
19
 * Class AdapterResolver
20
 * @package cloak\analyzer
21
 */
22
class AdapterResolver implements AdapterResolvable
23
{
24
25
    /**
26
     * @var array
27
     */
28
    private $adapters;
29
30
31
    /**
32
     * @param array $adapters
33
     */
34
    public function __construct(array $adapters)
35
    {
36
        $this->adapters = $adapters;
37
    }
38
39
    /**
40
     * @return \cloak\analyzer\AnalyzeAdapter
41
     * @throws AdapterNotFoundException
42
     */
43
    public function resolve()
44
    {
45
        $result = null;
46
        $exceptions = [];
47
48
        foreach ($this->adapters as $adapter) {
49
            try {
50
                $result = new $adapter();
51
                break;
52
            } catch (AdapterNotAvailableException $exception) {
53
                $exceptions[] = $exception->getMessage();
54
            }
55
        }
56
57
        if (count($exceptions) === count($this->adapters)) {
58
            throw new AdapterNotFoundException($exceptions);
59
        }
60
61
        return $result;
62
    }
63
64
}
65