1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Metrics; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Concerns\HasRoundedValue; |
8
|
|
|
use Arcanedev\LaravelMetrics\Results\ValueResult; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Value |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class Value extends Metric |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Traits |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use HasRoundedValue; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Getters & Setters |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the metric type. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
36 |
|
public function type(): string |
35
|
|
|
{ |
36
|
36 |
|
return 'value'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
40
|
|
|
| Main Methods |
41
|
|
|
| ----------------------------------------------------------------- |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Calculate the `count` of the metric. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
48
|
|
|
* @param string|string $column |
49
|
|
|
* |
50
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
51
|
|
|
*/ |
52
|
4 |
|
public function count($model, $column = null) |
53
|
|
|
{ |
54
|
4 |
|
return $this->aggregate('count', $model, $column); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Calculate the `average` of the metric. |
59
|
|
|
* |
60
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
61
|
|
|
* @param string $column |
62
|
|
|
* |
63
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
64
|
|
|
*/ |
65
|
12 |
|
public function average($model, string $column) |
66
|
|
|
{ |
67
|
12 |
|
return $this->aggregate('avg', $model, $column); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Calculate the `sum` of the metric. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
74
|
|
|
* @param string $column |
75
|
|
|
* |
76
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
77
|
|
|
*/ |
78
|
4 |
|
public function sum($model, string $column) |
79
|
|
|
{ |
80
|
4 |
|
return $this->aggregate('sum', $model, $column); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Calculate the `max` of the metric. |
85
|
|
|
* |
86
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
87
|
|
|
* @param string $column |
88
|
|
|
* |
89
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
90
|
|
|
*/ |
91
|
4 |
|
public function max($model, string $column) |
92
|
|
|
{ |
93
|
4 |
|
return $this->aggregate('max', $model, $column); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Calculate the `min` of the metric. |
98
|
|
|
* |
99
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
100
|
|
|
* @param string $column |
101
|
|
|
* |
102
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
103
|
|
|
*/ |
104
|
4 |
|
public function min($model, string $column) |
105
|
|
|
{ |
106
|
4 |
|
return $this->aggregate('min', $model, $column); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* ----------------------------------------------------------------- |
110
|
|
|
| Other Methods |
111
|
|
|
| ----------------------------------------------------------------- |
112
|
|
|
*/ |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Handle the aggregate calculation of the metric. |
116
|
|
|
* |
117
|
|
|
* @param string $method |
118
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
119
|
|
|
* @param string|null $column |
120
|
|
|
* |
121
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
122
|
|
|
*/ |
123
|
28 |
|
protected function aggregate(string $method, $model, string $column = null) |
124
|
|
|
{ |
125
|
28 |
|
$query = static::getQuery($model); |
126
|
28 |
|
$column = $column ?? $query->getModel()->getQualifiedKeyName(); |
|
|
|
|
127
|
|
|
|
128
|
28 |
|
$value = $query->{$method}($column); |
129
|
|
|
|
130
|
28 |
|
return $this->result($method === 'count' ? $value : $this->roundValue($value)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Make a new result instance. |
135
|
|
|
* |
136
|
|
|
* @param mixed|null $value |
137
|
|
|
* |
138
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\ValueResult|mixed |
139
|
|
|
*/ |
140
|
28 |
|
protected function result($value = null) |
141
|
|
|
{ |
142
|
28 |
|
return new ValueResult($value); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: