Completed
Push — master ( 997958...c1a544 )
by Adam
02:44
created

AbstractFactory::__callStatic()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
1
<?php
2
3
namespace BestServedCold\Benchmark\Factory;
4
5
use BestServedCold\PhalueObjects\Metric;
6
7
/**
8
 * Class AbstractFactory
9
 *
10
 * @package BestServedCold\Benchmark\Factory
11
 * @method  static array now()
12
 */
13
abstract class AbstractFactory
14
{
15
    /**
16
     * @var array
17
     */
18
    protected static $metrics = [];
19
20
    /**
21
     * @var array
22
     */
23
    protected static $allowedMethods = [];
24
25
    /**
26
     * @param  string $name
27
     * @param  array $arguments
28
     * @throws \Exception
29
     * @return array
30
     */
31 9
    public static function __callStatic($name, array $arguments = [])
32
    {
33 9
        if (in_array($name, static::$allowedMethods)) {
34 9
            return self::map($name, empty($arguments) ? static::$metrics : reset($arguments));
35
        }
36
37 2
        throw new \Exception('[' . $name . '] not an allowed method in context [' . get_class() . ']' . PHP_EOL);
38
    }
39
40
    /**
41
     * @param  string $method
42
     * @param  array  $metrics
43
     * @return array
44
     */
45
    public static function map($method, array $metrics)
46
    {
47 9
        return array_map(function($metric) use ($method) {
48 9
            return self::runMethod($metric, $method);
49 9
        }, $metrics);
50
    }
51
52
    /**
53
     * @param  $metric
54
     * @param  $method
55
     * @return array
56
     */
57 9
    protected static function runMethod($metric, $method)
58
    {
59 9
        return $metric instanceof Metric ? $metric::now()->$method($metric) : $metric::$method();
60
    }
61
62
    /**
63
     * @param array $metrics
64
     */
65 1
    public static function metrics(array $metrics)
66
    {
67 1
        static::$metrics = $metrics;
68 1
    }
69
}
70