AbstractFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 57
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 6 1
A runMethod() 0 4 2
A __callStatic() 0 8 3
A metrics() 0 4 1
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