Skip to content

Commit 7ea0dca

Browse files
Severinov1r0x
andauthored
[0.12] Moved the AccesPointUtils into a Service. (#590)
* Moved AccessPointLogic into a Service --------- Co-authored-by: Vinzenz Rosenkranz <vinzenz.rosenkranz@uni-tuebingen.de>
1 parent a393b78 commit 7ea0dca

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

app/Http/Controllers/HomeController.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Attribute;
66
use App\Registries\AttributeRegistry;
7+
use App\Services\AccessPointsService;
78
use App\Bibliography;
89
use App\Entity;
910
use App\EntityType;
@@ -16,7 +17,6 @@
1617
use App\ThConcept;
1718
use App\User;
1819

19-
use App\Utils\AccesspointUtils;
2020
use Illuminate\Http\Request;
2121
use Illuminate\Support\Facades\App;
2222
use Illuminate\Support\Str;
@@ -28,7 +28,7 @@ class HomeController extends Controller
2828
*
2929
* @return void
3030
*/
31-
public function __construct()
31+
public function __construct(private readonly AccessPointsService $accessPointsService)
3232
{
3333
parent::__construct();
3434
if(!Preference::hasPublicAccess()) {
@@ -39,16 +39,14 @@ public function __construct()
3939

4040
public function checkAccesspointAccess(Request $request) {
4141
$user = auth()->user();
42-
4342
$accessPath = Str::finish($request->get('endpoint', '/'), '/');
4443

4544
if(!isset($user->accesspoints)) {
4645
// do not redirect if user has no access points defined (aka access to everything)
4746
return response()->json(null, 204);
4847
}
4948

50-
$availableAccesspoints = AccesspointUtils::get();
51-
49+
$availableAccesspoints = $this->accessPointsService->get();
5250
foreach($user->accesspoints as $accesspointId) {
5351
if(array_key_exists($accesspointId, $availableAccesspoints)) {
5452
if($availableAccesspoints[$accesspointId]['path'] == $accessPath) {
@@ -108,7 +106,7 @@ public function getGlobalData() {
108106
}
109107
}
110108

111-
$accesspoints = AccesspointUtils::get();
109+
$accesspoints = $this->accessPointsService->get();
112110

113111
// TODO handle layer relation in Map Plugin
114112
// $entityTypes = EntityType::with(['sub_entity_types', 'layer', 'attributes'])

app/Plugin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App;
44

55
use App\File\Directory;
6+
use App\Services\AccessPointsService;
67
use Carbon\Carbon;
78
use Illuminate\Database\Eloquent\Collection;
89
use Illuminate\Database\Eloquent\Model;
@@ -353,6 +354,10 @@ public function updateUpdateState($fromInfoVersion): void {
353354

354355
public function clearCache(): void {
355356
Cache::forget($this->getScopeCacheKey());
357+
358+
// TODO: Models are meant for the data layer only,
359+
// we should restructure the code into service classes (e.g. PluginMigrationService)
360+
app(AccessPointsService::class)->clearCache();
356361
}
357362

358363
public function handleInstallation(bool $isUpdate = false): void {
@@ -484,7 +489,7 @@ private function publishScript(): void {
484489

485490
self::getDirectory()->store(
486491
$this->publicName(false),
487-
$scriptPath
492+
$filehandle
488493
);
489494
fclose($filehandle);
490495
} else {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Plugin;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
class AccessPointsService {
9+
private const CACHE_KEY = 'access_points';
10+
11+
public const /*array*/ CORE_ACCESSPOINTS = [
12+
"Default" => [
13+
"label" => "main.user.accesspoints.default",
14+
"path" => "/",
15+
],
16+
];
17+
18+
public function clearCache(): void {
19+
Cache::forget(self::CACHE_KEY);
20+
}
21+
22+
public function get(): array {
23+
return Cache::rememberForever(self::CACHE_KEY, function() {
24+
$accesspoints = self::CORE_ACCESSPOINTS;
25+
$accesspoints = array_merge($accesspoints, $this->loadAccessPointsFromPlugins());
26+
return $accesspoints;
27+
});
28+
}
29+
30+
private function loadAccessPointsFromPlugins(): array {
31+
$installedPlugins = Plugin::getInstalled();
32+
$accesspoints = [];
33+
foreach($installedPlugins as $plugin) {
34+
$accesspoints = array_merge($accesspoints, $plugin->getAccessPoints());
35+
}
36+
return $accesspoints;
37+
}
38+
}

app/Utils/AccesspointUtils.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)