Aggregate   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 131
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 5
A getFieldname() 0 4 1
A getAsname() 0 4 1
A getCaption() 0 4 1
A getGroup() 0 4 1
A getDecimals() 0 4 1
A getOrder() 0 4 1
A asArray() 0 11 1
A getSQL() 0 6 1
A groupTypes() 0 10 1
A groupExists() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
namespace EngineWorks\Pivot;
4
5
use EngineWorks\DBAL\DBAL;
6
7
class Aggregate
8
{
9
    const COUNT = 'COUNT';
10
    const SUM = 'SUM';
11
    const AVG = 'AVG';
12
    const MIN = 'MIN';
13
    const MAX = 'MAX';
14
15
    const ORDERNONE = '';
16
    const ORDERASC = 'ASC';
17
    const ORDERDESC = 'DESC';
18
19
    private $fieldname;
20
    private $asname;
21
    private $caption;
22
    private $group;
23
    private $decimals;
24
    private $order;
25
26 16
    public function __construct(
27
        string $fieldname,
28
        string $asname,
29
        string $caption,
30
        string $group,
31
        int $decimals,
32
        string $order
33
    ) {
34 16
        if (! $this->groupExists($group)) {
35 1
            throw new \InvalidArgumentException('Invalid value for group');
36
        }
37 15
        if ($decimals < 0 || $decimals > 8) {
38 1
            throw new \InvalidArgumentException('Invalid value for decimals');
39
        }
40 14
        if (! in_array($order, [static::ORDERNONE, static::ORDERASC, static::ORDERDESC])) {
41 1
            throw new \InvalidArgumentException('Invalid order value');
42
        }
43 13
        $this->fieldname = $fieldname;
44 13
        $this->asname = $asname;
45 13
        $this->caption = $caption;
46 13
        $this->group = $group;
47 13
        $this->decimals = $decimals;
48 13
        $this->order = $order;
49 13
    }
50
51
    /**
52
     * @return string
53
     */
54 1
    public function getFieldname(): string
55
    {
56 1
        return $this->fieldname;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 13
    public function getAsname(): string
63
    {
64 13
        return $this->asname;
65
    }
66
67
    /**
68
     * @return string
69
     */
70 1
    public function getCaption(): string
71
    {
72 1
        return $this->caption;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    public function getGroup(): string
79
    {
80 1
        return $this->group;
81
    }
82
83
    /**
84
     * @return int
85
     */
86 10
    public function getDecimals(): int
87
    {
88 10
        return $this->decimals;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 10
    public function getOrder(): string
95
    {
96 10
        return $this->order;
97
    }
98
99 12
    public function asArray() : array
100
    {
101
        return [
102 12
            'fieldname' => $this->fieldname,
103 12
            'caption' => $this->caption,
104 12
            'group' => $this->group,
105 12
            'asname' => $this->asname,
106 12
            'decimals' => $this->decimals,
107 12
            'order' => $this->order,
108
        ];
109
    }
110
111
    /**
112
     * @param DBAL $db
113
     * @return string
114
     */
115 9
    public function getSQL(DBAL $db) : string
116
    {
117 9
        return $this->group
118 9
            . '(' . $db->sqlFieldEscape($this->fieldname) . ')'
119 9
            . ' AS ' . $db->sqlFieldEscape($this->asname);
120
    }
121
122 18
    public static function groupTypes() : array
123
    {
124
        return [
125 18
            static::COUNT,
126 18
            static::SUM,
127 18
            static::AVG,
128 18
            static::MIN,
129 18
            static::MAX,
130
        ];
131
    }
132
133 17
    public static function groupExists(string $group) : bool
134
    {
135 17
        return in_array($group, static::groupTypes());
136
    }
137
}
138