22
33namespace DirectoryTree \Metrics \Jobs ;
44
5+ use DirectoryTree \Metrics \DatabaseMetricManager ;
56use DirectoryTree \Metrics \Measurable ;
67use DirectoryTree \Metrics \Metric ;
78use Illuminate \Bus \Queueable ;
@@ -19,7 +20,7 @@ class RecordMetric implements ShouldQueue
1920 */
2021 public function __construct (
2122 /** @var Collection<Measurable>|Measurable */
22- public Collection |Measurable $ metrics,
23+ public Collection |Measurable $ metrics
2324 ) {}
2425
2526 /**
@@ -38,9 +39,13 @@ public function handle(): void
3839 fn (Measurable $ metric ) => $ metric ->value ()
3940 );
4041
41- Metric::query ()->getConnection ()->transaction (
42- function (ConnectionInterface $ connection ) use ($ metric , $ value ) {
43- $ model = Metric::query ()->firstOrCreate ([
42+ /** @var \Illuminate\Database\Eloquent\Model $model */
43+ $ model = new DatabaseMetricManager::$ model ;
44+
45+ $ model ->getConnection ()->transaction (
46+ function (ConnectionInterface $ connection ) use ($ metric , $ value , $ model ) {
47+ $ instance = $ model ->newQuery ()->firstOrCreate ([
48+ ...$ metric ->additional (),
4449 'name ' => $ metric ->name (),
4550 'category ' => $ metric ->category (),
4651 'year ' => $ metric ->year (),
@@ -50,7 +55,7 @@ function (ConnectionInterface $connection) use ($metric, $value) {
5055 'measurable_id ' => $ metric ->measurable ()?->getKey(),
5156 ], ['value ' => 0 ]);
5257
53- Metric:: query ()->whereKey ($ model ->getKey ())->update ([
58+ $ model -> newQuery ()->whereKey ($ instance ->getKey ())->update ([
5459 'value ' => $ connection ->raw ('value + ' .$ value ),
5560 ]);
5661 }
0 commit comments