-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathClusterService.php
More file actions
132 lines (110 loc) · 3.96 KB
/
ClusterService.php
File metadata and controls
132 lines (110 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace App\Services;
use App\DTO\ClusterDashboardData;
use App\Models\Cluster;
use App\Services\Interfaces\IBaseService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
/**
* @implements IBaseService<Cluster>
*/
class ClusterService implements IBaseService {
public function __construct(
private Cluster $cluster,
) {}
/**
* Creates a cluster dashboard data container with computed fields.
* This method does not mutate the cluster model.
*/
public function getClusterWithComputedData(
Cluster $cluster,
int $meterCount,
float $totalTransactionsAmount,
int $populationCount,
): ClusterDashboardData {
return new ClusterDashboardData(
cluster: $cluster,
meterCount: $meterCount,
revenue: $totalTransactionsAmount,
population: $populationCount,
);
}
public function getClusterCities(int $clusterId): ?Cluster {
return Cluster::query()->with(['cities', 'location'])->find($clusterId);
}
public function getClusterMiniGrids(int $clusterId): ?Cluster {
return Cluster::query()->with(['miniGrids', 'location'])->find($clusterId);
}
public function getGeoLocationById(int $clusterId): mixed {
$cluster = $this->cluster->newQuery()->with('location')->findOrFail($clusterId);
return $cluster->geo_json;
}
/**
* @return array<int, string>
*/
public function getDateRangeFromRequest(?string $startDate, ?string $endDate): array {
$dateRange = [];
if ($startDate !== null && $endDate !== null) {
$dateRange[0] = $startDate;
$dateRange[1] = $endDate;
} else {
$dateRange[0] = date('Y-m-d', strtotime('today - 31 days'));
$dateRange[1] = date('Y-m-d', strtotime('today - 1 days'));
}
return $dateRange;
}
public function getById(int $clusterId): Cluster {
return $this->cluster->newQuery()->with(['miniGrids.location', 'cities', 'location'])->find($clusterId);
}
/**
* @param array<string, mixed> $clusterData
*/
public function create(array $clusterData): Cluster {
return DB::connection('tenant')->transaction(function () use ($clusterData): Cluster {
$geoJson = $clusterData['geo_json'] ?? null;
// Keep legacy write only while clusters.geo_json exists.
if (! Schema::connection('tenant')->hasColumn('clusters', 'geo_json')) {
unset($clusterData['geo_json']);
}
$cluster = $this->cluster->newQuery()->create($clusterData);
if ($geoJson !== null) {
$cluster->location()->create([
'points' => '',
'geo_json' => $geoJson,
]);
}
return $cluster->fresh('location');
});
}
/**
* @return Collection<int, Cluster>|LengthAwarePaginator<int, Cluster>
*/
public function getAll(?int $limit = null): Collection|LengthAwarePaginator {
if ($limit !== null) {
return $this->cluster->newQuery()->with(['miniGrids', 'location'])->limit($limit)->get();
}
return $this->cluster->newQuery()->with(['miniGrids', 'location'])->get();
}
/**
* @param array<string, mixed> $data
*/
public function update($model, array $data): Cluster {
throw new \Exception('Method update() not yet implemented.');
}
public function delete($model): ?bool {
throw new \Exception('Method delete() not yet implemented.');
}
/**
* @return Collection<int, Cluster>
*/
public function getAllForExport(): Collection {
return $this->cluster->newQuery()->with([
'miniGrids',
'cities',
'manager',
'location',
])->get();
}
}