Completed
Push — master ( 94841b...577c5d )
by David
08:23
created

Operator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C operation() 0 33 8
1
<?php
2
3
namespace Dwo\Aggregator;
4
5
use Dwo\Aggregator\Model\Aggregate;
6
use Malenki\Math\Stats\Stats;
7
8
/**
9
 * Class Operator
10
 *
11
 * @author Dave Www <[email protected]>
12
 */
13
class Operator
14
{
15
    const MEAN = 'mean';
16
    const MEAN_HARMONIC = 'mean_harmonic';
17
    const MEDIAN = 'median';
18
19
    /**
20
     * @param Aggregate $aggregate
21
     * @param array     $saveKeys
22
     */
23
    public static function operation(Aggregate $aggregate, array $saveKeys = [])
24
    {
25
        $data = $aggregate->getData();
26
27
        $operations = [];
28
        foreach (array_keys($data) as $key) {
29
            if (isset($saveKeys[$key])) {
30
                $operations[$key] = $saveKeys[$key];
31
            }
32
        }
33
34
        foreach ($operations as $key => $operation) {
35
            $stats = new Stats($data[$key]);
36
            switch ($operation) {
37
                default:
38
                    if (!method_exists($stats, $operation)) {
39
                        $operation = self::MEAN;
40
                    }
41
                    $data[$key] = $stats->$operation();
42
                case self::MEAN:
43
                    $data[$key] = $stats->mean();
44
                    break;
45
                case self::MEAN_HARMONIC:
46
                    $data[$key] = $stats->harmonicMean();
47
                    break;
48
                case self::MEDIAN:
49
                    $data[$key] = $stats->median();
50
                    break;
51
            }
52
        }
53
54
        $aggregate->setData($data);
55
    }
56
57
}