Factory::getOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace App\Provider;
4
5
use App\Model\Project;
6
use App\Provider\ProviderInterface;
7
use RuntimeException;
8
9
/**
10
 * Provider factory responsible for managing provider instances
11
 *
12
 * @author Ronan Chilvers <[email protected]>
13
 */
14
class Factory
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $instances = [];
20
21
    /**
22
     * Add a provider instance
23
     *
24
     * @param \App\Provider\ProviderInterface
25
     * @author Ronan Chilvers <[email protected]>
26
     */
27
    public function addProvider(ProviderInterface $instance)
28
    {
29
        $this->instances[] = $instance;
30
    }
31
32
    /**
33
     * Get a suitable instance for a given project
34
     *
35
     * @param \App\Model\Project $project
36
     * @return \App\Provider\ProviderInterface
37
     * @throws RuntimeException If a suitable provider is not found
38
     * @author Ronan Chilvers <[email protected]>
39
     */
40
    public function forProject(Project $project)
41
    {
42
        foreach ($this->instances as $instance) {
43
            if ($instance->handles($project)) {
44
                return $instance;
45
            }
46
        }
47
48
        throw new RuntimeException('No suitable instance found for project provider ' . $project->provider);
49
    }
50
51
    /**
52
     * Get the project provider options as an array
53
     *
54
     * @return array
55
     * @author Ronan Chilvers <[email protected]>
56
     */
57
    public function getOptions()
58
    {
59
        $options = [];
60
        foreach ($this->instances as $instance) {
61
            $label = $instance->getLabel();
62
            $options[strtolower($label)] = $label;
63
        }
64
65
        return $options;
66
    }
67
}
68