Flip::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Vehikl\Flip;
4
5
class Flip
6
{
7
    private $class;
8
    private $features;
9
10 8
    public function __construct($class, array $features = [])
11
    {
12 8
        $this->class = $class;
13 8
        $this->features = $features;
14 8
    }
15
16 8
    private function applicableFeature(string $method): ?Feature
17
    {
18
        // Probably want a factory class to resolve any dependencies and cache
19 8
        foreach ($this->buildFeatures() as $feature) {
20 8
            if ($feature->hasToggle($method)) {
21 8
                return $feature;
22
            }
23
        }
24
25 2
        return null;
26
    }
27
28
    /**
29
     * @return \Generator|Feature[]
30
     */
31 8
    private function buildFeatures(): \Generator
32
    {
33 8
        foreach ($this->features as $feature) {
34 8
            yield new $feature($this->class);
35
        }
36 2
    }
37
38 8
    public function __call($method, $arguments)
39
    {
40
        // What happens if a method applies to multiple features?
41 8
        $first = $this->applicableFeature($method) ?: $this->class;
42
43 8
        return $first->{$method}($arguments);
44
    }
45
}
46