Skip to content

Commit a6c56ec

Browse files
committed
Add metric helper
1 parent 94dc924 commit a6c56ec

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/PendingMetric.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace DirectoryTree\Metrics;
4+
5+
use BackedEnum;
6+
use Carbon\CarbonInterface;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class PendingMetric
10+
{
11+
/**
12+
* The name of the metric.
13+
*/
14+
protected BackedEnum|string $name;
15+
16+
/**
17+
* The category of the metric.
18+
*/
19+
protected BackedEnum|string|null $category = null;
20+
21+
/**
22+
* The date of the metric.
23+
*/
24+
protected ?CarbonInterface $date = null;
25+
26+
/**
27+
* The measurable model of the metric.
28+
*/
29+
protected ?Model $measurable = null;
30+
31+
/**
32+
* Constructor.
33+
*/
34+
public function __construct(BackedEnum|string $name)
35+
{
36+
$this->name = $name;
37+
}
38+
39+
/**
40+
* Set the category of the metric.
41+
*/
42+
public function category(BackedEnum|string|null $category): self
43+
{
44+
$this->category = $category;
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* Set the date of the metric.
51+
*/
52+
public function date(CarbonInterface $date): self
53+
{
54+
$this->date = $date;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* Set the measurable model of the metric.
61+
*/
62+
public function measurable(Model $measurable): self
63+
{
64+
$this->measurable = $measurable;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Record the metric.
71+
*/
72+
public function record(int $value = 1): void
73+
{
74+
app(MetricManager::class)->record(
75+
new MetricData(
76+
$this->name,
77+
$this->category,
78+
$value,
79+
$this->date,
80+
$this->measurable
81+
)
82+
);
83+
}
84+
}

src/helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace DirectoryTree\Metrics;
4+
5+
use BackedEnum;
6+
7+
if (! function_exists('metric')) {
8+
/**
9+
* Create a new pending metric.
10+
*/
11+
function metric(BackedEnum|string $name): PendingMetric
12+
{
13+
return new PendingMetric($name);
14+
}
15+
}

0 commit comments

Comments
 (0)