1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Metrics; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Concerns\ConvertsToArray; |
8
|
|
|
use Arcanedev\LaravelMetrics\Contracts\Metric as MetricContract; |
9
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Concerns\HasCachedResults; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use Illuminate\Support\Traits\Macroable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Metric |
17
|
|
|
* |
18
|
|
|
* @package Arcanedev\LaravelMetrics\Metrics |
19
|
|
|
* @author ARCANEDEV <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class Metric implements MetricContract |
22
|
|
|
{ |
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Traits |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
use Macroable, |
29
|
|
|
ConvertsToArray, |
30
|
|
|
HasCachedResults; |
31
|
|
|
|
32
|
|
|
/* ----------------------------------------------------------------- |
33
|
|
|
| Properties |
34
|
|
|
| ----------------------------------------------------------------- |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The Http request instance. |
39
|
|
|
* |
40
|
|
|
* @var \Illuminate\Http\Request |
41
|
|
|
*/ |
42
|
|
|
protected $request; |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Getters & Setters |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the metric's title. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
6 |
|
public function title(): string |
55
|
|
|
{ |
56
|
6 |
|
$class = class_basename(static::class); |
57
|
|
|
|
58
|
6 |
|
return __(Str::title(Str::snake($class, ' '))); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the Http request instance. |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Http\Request |
65
|
|
|
*/ |
66
|
54 |
|
public function getRequest() |
67
|
|
|
{ |
68
|
54 |
|
return $this->request; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the Http request instance. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Http\Request $request |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
54 |
|
public function setRequest(Request $request) |
79
|
|
|
{ |
80
|
54 |
|
$this->request = $request; |
81
|
|
|
|
82
|
54 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/* ----------------------------------------------------------------- |
86
|
|
|
| Main Methods |
87
|
|
|
| ----------------------------------------------------------------- |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Resolve & calculate the metric. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Http\Request $request |
94
|
|
|
* |
95
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\Result|mixed |
96
|
|
|
*/ |
97
|
54 |
|
public function resolve(Request $request) |
98
|
|
|
{ |
99
|
54 |
|
$this->setRequest($request); |
100
|
|
|
|
101
|
|
|
$callback = function () use ($request) { |
102
|
50 |
|
return app()->call([$this, 'calculate'], [ |
103
|
50 |
|
'request' => $this->getRequest(), |
104
|
|
|
]); |
105
|
54 |
|
}; |
106
|
|
|
|
107
|
54 |
|
return ($cacheFor = $this->cacheFor()) |
108
|
4 |
|
? $this->cacheResult($cacheFor, $callback) |
109
|
54 |
|
: $callback(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Make a new result instance. |
114
|
|
|
* |
115
|
|
|
* @param mixed|null $value |
116
|
|
|
* |
117
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\Result |
118
|
|
|
*/ |
119
|
|
|
abstract protected function result($value = null); |
120
|
|
|
|
121
|
|
|
/* ----------------------------------------------------------------- |
122
|
|
|
| Other Methods |
123
|
|
|
| ----------------------------------------------------------------- |
124
|
|
|
*/ |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the instance as an array. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
6 |
|
public function toArray(): array |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
6 |
|
'class' => static::class, |
135
|
6 |
|
'type' => $this->type(), |
136
|
6 |
|
'title' => $this->title(), |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the query builder. |
142
|
|
|
* |
143
|
|
|
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|string $model |
144
|
|
|
* |
145
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
146
|
|
|
*/ |
147
|
50 |
|
protected static function getQuery($model): Builder |
148
|
|
|
{ |
149
|
50 |
|
if (is_string($model)) { |
150
|
50 |
|
$model = new $model; |
151
|
|
|
} |
152
|
|
|
|
153
|
50 |
|
if ($model instanceof Builder) { |
154
|
|
|
return $model; |
155
|
|
|
} |
156
|
|
|
|
157
|
50 |
|
return $model->newQuery(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|