Operator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C operation() 0 45 12
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
            if (!count($data[$key])) {
36
                switch ($operation) {
37
                    default:
38
                    case self::MEAN:
39
                    case self::MEAN_HARMONIC:
40
                    case self::MEDIAN:
41
                        $data[$key] = 0;
42
                        break;
43
                }
44
                continue;
45
            }
46
47
            $stats = new Stats($data[$key]);
48
            switch ($operation) {
49
                default:
50
                    if (!method_exists($stats, $operation)) {
51
                        $operation = self::MEAN;
52
                    }
53
                    $data[$key] = $stats->$operation();
54
                case self::MEAN:
55
                    $data[$key] = $stats->mean();
56
                    break;
57
                case self::MEAN_HARMONIC:
58
                    $data[$key] = $stats->harmonicMean();
59
                    break;
60
                case self::MEDIAN:
61
                    $data[$key] = $stats->median();
62
                    break;
63
            }
64
        }
65
66
        $aggregate->setData($data);
67
    }
68
69
}