Passed
Branch main (b6a268)
by Iain
04:11
created

AthenaRouteLoader::addReadRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Athena\Routing;
16
17
use Parthenon\Athena\Controller\AthenaController;
18
use Parthenon\Athena\Controller\NotificationController;
19
use Parthenon\Athena\SectionManager;
20
use Symfony\Component\Config\Loader\Loader;
21
use Symfony\Component\Routing\Route;
22
use Symfony\Component\Routing\RouteCollection;
23
24
final class AthenaRouteLoader extends Loader
25
{
26
    private $isLoaded = false;
27
28
    private SectionManager $sectionManager;
29
    private ?string $host;
30
31
    public function __construct(SectionManager $sectionManager, ?string $host)
32
    {
33
        $this->sectionManager = $sectionManager;
34
        $this->host = $host;
35
    }
36
37
    public function load($resource, string $type = null): mixed
38
    {
39
        if (true === $this->isLoaded) {
40
            throw new \RuntimeException('Do not add the "athena" loader twice');
41
        }
42
43
        $routes = new RouteCollection();
44
45
        foreach ($this->sectionManager->getSections() as $section) {
46
            $urlTag = $section->getUrlTag();
47
            $serviceName = $this->getServiceName(get_class($section));
48
49
            $this->addListRoute($urlTag, $serviceName, $routes);
50
            $this->addCreateRoute($urlTag, $serviceName, $routes);
51
            $this->addEditRoute($urlTag, $serviceName, $routes);
52
            $this->addReadRoute($urlTag, $serviceName, $routes);
53
            $this->addDeleteRoute($urlTag, $serviceName, $routes);
54
            $this->addUndeleteRoute($urlTag, $serviceName, $routes);
55
        }
56
57
        $this->addDefaultRoutes($routes);
58
59
        $this->isLoaded = true;
60
61
        return $routes;
62
    }
63
64
    public function supports($resource, string $type = null): bool
65
    {
66
        return 'athena' === $type;
67
    }
68
69
    protected function addDefaultRoutes(RouteCollection $routes): void
70
    {
71
        $route = $this->getRoute('/athena/index', [
72
            '_controller' => AthenaController::class.'::index',
73
        ]);
74
        $routes->add('parthenon_athena_index', $route);
75
        $route = $this->getRoute('/athena/notification/index', [
76
            '_controller' => NotificationController::class.'::viewAll',
77
        ]);
78
        $routes->add('parthenon_athena_notification_list', $route);
79
        $route = $this->getRoute('/athena/notification/{id}/read', [
80
            '_controller' => NotificationController::class.'::markAsRead',
81
        ]);
82
        $routes->add('parthenon_athena_notification_read', $route);
83
84
        if (!is_null($this->host)) {
85
            $route = $this->getRoute('/', [
86
                '_controller' => AthenaController::class.'::login',
87
            ]);
88
            $routes->add('parthenon_athena_landing', $route);
89
            $route = $this->getRoute('/login', [
90
                '_controller' => AthenaController::class.'::login',
91
            ]);
92
            $routes->add('parthenon_athena_login', $route);
93
        }
94
    }
95
96
    protected function addListRoute(string $urlTag, string $serviceName, RouteCollection $routes): void
97
    {
98
        $path = '/athena/'.$urlTag.'/list';
99
        $defaults = [
100
            '_controller' => 'athena_controller_'.$serviceName.'::showList',
101
        ];
102
        $route = $this->getRoute($path, $defaults);
103
        // add the new route to the route collection
104
        $routeName = 'parthenon_athena_crud_'.$urlTag.'_list';
105
        $routes->add($routeName, $route);
106
    }
107
108
    protected function addReadRoute(string $urlTag, string $serviceName, RouteCollection $routes): void
109
    {
110
        $path = '/athena/'.$urlTag.'/{id}/read';
111
        $defaults = [
112
            '_controller' => 'athena_controller_'.$serviceName.'::showRead',
113
        ];
114
        $route = $this->getRoute($path, $defaults);
115
        // add the new route to the route collection
116
        $routeName = 'parthenon_athena_crud_'.$urlTag.'_read';
117
        $routes->add($routeName, $route);
118
    }
119
120
    protected function addDeleteRoute(string $urlTag, string $serviceName, RouteCollection $routes): void
121
    {
122
        $path = '/athena/'.$urlTag.'/{id}/delete';
123
        $defaults = [
124
            '_controller' => 'athena_controller_'.$serviceName.'::delete',
125
        ];
126
        $route = $this->getRoute($path, $defaults);
127
        // add the new route to the route collection
128
        $routeName = 'parthenon_athena_crud_'.$urlTag.'_delete';
129
        $routes->add($routeName, $route);
130
    }
131
132
    protected function addUndeleteRoute(string $urlTag, string $serviceName, RouteCollection $routes): void
133
    {
134
        $path = '/athena/'.$urlTag.'/{id}/undelete';
135
        $defaults = [
136
            '_controller' => 'athena_controller_'.$serviceName.'::undelete',
137
        ];
138
        $route = $this->getRoute($path, $defaults);
139
        // add the new route to the route collection
140
        $routeName = 'parthenon_athena_crud_'.$urlTag.'_undelete';
141
        $routes->add($routeName, $route);
142
    }
143
144
    protected function addEditRoute(string $urlTag, string $serviceName, RouteCollection $routes): void
145
    {
146
        $path = '/athena/'.$urlTag.'/{id}/edit';
147
        $defaults = [
148
            '_controller' => 'athena_controller_'.$serviceName.'::edit',
149
        ];
150
        $route = $this->getRoute($path, $defaults);
151
        // add the new route to the route collection
152
        $routeName = 'parthenon_athena_crud_'.$urlTag.'_edit';
153
        $routes->add($routeName, $route);
154
    }
155
156
    protected function addCreateRoute(string $urlTag, string $serviceName, RouteCollection $routes): void
157
    {
158
        $path = '/athena/'.$urlTag.'/create';
159
        $defaults = [
160
            '_controller' => 'athena_controller_'.$serviceName.'::create',
161
        ];
162
        $route = $this->getRoute($path, $defaults);
163
        // add the new route to the route collection
164
        $routeName = 'parthenon_athena_crud_'.$urlTag.'_create';
165
        $routes->add($routeName, $route);
166
    }
167
168
    protected function getRoute(string $path, array $defaults): Route
169
    {
170
        $route = new Route($path, $defaults, [], [], $this->host);
171
172
        return $route;
173
    }
174
175
    private function getServiceName(string $className): string
176
    {
177
        $parts = explode('\\', $className);
178
        $className = end($parts);
179
180
        return strtolower($className);
181
    }
182
}
183