Passed
Pull Request — master (#22)
by Vincent
06:37
created

anonymous//src/Query/Expression/Aggregate.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
1
<?php
2
3
namespace Bdf\Prime\Query\Expression;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
7
/**
8
 * Aggregation expressions
9
 * Can be use on Query::select() method
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
10
 *
11
 * Usage:
12
 * <code>
13
 * User::select([
14
 *     'firstId' => Aggregate::min(id),
15
 *     'lastId' => Aggregate::max(id),
16
 *     'count' => Aggregate::count(id),
17
 * ]);
18
 * </code>
19
 */
20
abstract class Aggregate implements ExpressionInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $attribute;
26
27
    /**
28
     * Max constructor.
29
     *
30
     * @param string $attribute The attribute name
31
     */
32 5
    public function __construct(string $attribute)
33
    {
34 5
        $this->attribute = $attribute;
35 5
    }
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $compiler should have a doc-comment as per coding-style.
Loading history...
38
     * {@inheritdoc}
39
     */
40 5
    final public function build($query, $compiler)
41
    {
42 5
        return $this->expression(
43 5
            $compiler->platform()->grammar(),
44 5
            $compiler->quoteIdentifier($query, $query->preprocessor()->field($this->attribute))
45
        );
46
    }
47
48
    /**
49
     * Get the aggregate expression
50
     *
51
     * @param AbstractPlatform $platform The database platform
52
     * @param string $attribute The attribute to aggregate
53
     *
54
     * @return string
55
     */
56
    abstract protected function expression(AbstractPlatform $platform, string $attribute): string;
57
58
    /**
59
     * Perform MIN() aggregation function
60
     *
61
     * @param string $attribute The attribute to aggregate
62
     *
63
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
64
     */
65 1
    public static function min(string $attribute): self
66
    {
67
        return new class($attribute) extends Aggregate {
68
            protected function expression(AbstractPlatform $platform, string $attribute): string
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
Coding Style introduced by
Missing doc comment for function expression()
Loading history...
introduced by
Missing function doc comment
Loading history...
69
            {
70 1
                return $platform->getMinExpression($attribute);
71
            }
72
        };
73
    }
74
75
    /**
76
     * Perform MAX() aggregation function
77
     *
78
     * @param string $attribute The attribute to aggregate
79
     *
80
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
81
     */
82 1
    public static function max(string $attribute): self
83
    {
84
        return new class($attribute) extends Aggregate {
85
            protected function expression(AbstractPlatform $platform, string $attribute): string
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
Coding Style introduced by
Missing doc comment for function expression()
Loading history...
introduced by
Missing function doc comment
Loading history...
86
            {
87 1
                return $platform->getMaxExpression($attribute);
88
            }
89
        };
90
    }
91
92
    /**
93
     * Perform AVG() aggregation function
94
     *
95
     * @param string $attribute The attribute to aggregate
96
     *
97
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
98
     */
99 1
    public static function avg(string $attribute): self
100
    {
101
        return new class($attribute) extends Aggregate {
102
            protected function expression(AbstractPlatform $platform, string $attribute): string
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
Coding Style introduced by
Missing doc comment for function expression()
Loading history...
introduced by
Missing function doc comment
Loading history...
103
            {
104 1
                return $platform->getAvgExpression($attribute);
105
            }
106
        };
107
    }
108
109
    /**
110
     * Perform COUNT() aggregation function
111
     *
112
     * @param string $attribute The attribute to aggregate
113
     *
114
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
115
     */
116 1
    public static function count(string $attribute): self
117
    {
118
        return new class($attribute) extends Aggregate {
119
            protected function expression(AbstractPlatform $platform, string $attribute): string
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
Coding Style introduced by
Missing doc comment for function expression()
Loading history...
introduced by
Missing function doc comment
Loading history...
120
            {
121 1
                return $platform->getCountExpression($attribute);
122
            }
123
        };
124
    }
125
126
    /**
127
     * Perform SUM() aggregation function
128
     *
129
     * @param string $attribute The attribute to aggregate
130
     *
131
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
132
     */
133 1
    public static function sum(string $attribute): self
134
    {
135
        return new class($attribute) extends Aggregate {
136
            protected function expression(AbstractPlatform $platform, string $attribute): string
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
Coding Style introduced by
Missing doc comment for function expression()
Loading history...
introduced by
Missing function doc comment
Loading history...
137
            {
138 1
                return $platform->getSumExpression($attribute);
139
            }
140
        };
141
    }
142
}
143