BoneUserPackage::addRoutes()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 4

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 4
eloc 40
c 5
b 0
f 2
nc 8
nop 2
dl 0
loc 54
ccs 43
cts 43
cp 1
crap 4
rs 9.28

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\User;
6
7
use Barnacle\Container;
8
use Barnacle\RegistrationInterface;
9
use Bone\Application;
10
use Bone\Console\Command;
11
use Bone\Console\CommandRegistrationInterface;
12
use Bone\Contracts\Container\AdminPanelProviderInterface;
13
use Bone\Contracts\Container\DefaultSettingsProviderInterface;
14
use Bone\Contracts\Container\DependentPackagesProviderInterface;
15
use Bone\Contracts\Container\FixtureProviderInterface;
16
use Bone\Controller\Init;
17
use Bone\I18n\I18nRegistrationInterface;
18
use Bone\Mail\Service\MailService;
19
use Bone\Paseto\PasetoService;
20
use Bone\Router\Router;
21
use Bone\Router\RouterConfigInterface;
22
use Bone\User\Controller\BoneUserApiController;
23
use Bone\User\Controller\BoneUserController;
24
use Bone\User\Fixtures\LoadUsers;
25
use Bone\User\Http\Controller\Admin\PersonAdminController;
26
use Bone\User\Http\Controller\Admin\UserAdminController;
27
use Bone\User\Http\Middleware\SessionAuth;
28
use Bone\User\Http\Middleware\SessionAuthRedirect;
29
use Bone\User\View\Helper\LoginWidget;
30
use Bone\View\Util\AdminLink;
31
use Bone\View\ViewRegistrationInterface;
32
use Del\Booty\AssetRegistrationInterface;
33
use Del\Console\CreateUserCommand;
34
use Del\Console\ResetPasswordCommand;
35
use Del\Service\UserService;
36
use Del\SessionManager;
37
use League\Route\RouteGroup;
38
use League\Route\Strategy\JsonStrategy;
39
use Laminas\Diactoros\ResponseFactory;
40
use Laminas\I18n\Translator\Translator;
41
use Symfony\Component\Console\Style\SymfonyStyle;
42
43
class BoneUserPackage implements RegistrationInterface, RouterConfigInterface, I18nRegistrationInterface,
44
                                 AssetRegistrationInterface, ViewRegistrationInterface, CommandRegistrationInterface,
45
                                 FixtureProviderInterface, AdminPanelProviderInterface, DefaultSettingsProviderInterface,
46
                                 DependentPackagesProviderInterface
47
{
48 3
    public function addToContainer(Container $c)
49
    {
50 3
        $c[BoneUserController::class] = $c->factory(function (Container $c) {
51
            /** @var MailService $mailService */
52 1
            $mailService = $c->get(MailService::class);
53
            /** @var UserService $userService */
54 1
            $userService = $c->get(UserService::class);
55 1
            $pasetoService = $c->get(PasetoService::class);
56 1
            $defaultLayout = $c->get('default_layout');
57 1
            $adminLayout = $c->has('admin_layout') ? $c->get('admin_layout') : $defaultLayout;
58 1
            $options = [];
59
60 1
            if ($c->has('bone-user')) {
61 1
                $options = $c->get('bone-user');
62
            }
63
64 1
            $loginRedirectRoute = $options['loginRedirectRoute'] ?? '/user/home';
65 1
            $registrationEnabled = $options['enableRegistration'] ?? true;
66 1
            $profileRequired = $options['requireProfile'] ?? false;
67 1
            $rememberMeCookie = $options['rememberMeCookie'] ?? false;
68 1
            $controller = new BoneUserController($userService, $mailService, $loginRedirectRoute, $adminLayout, $pasetoService, $registrationEnabled, $profileRequired, $rememberMeCookie);
69
70 1
            return  Init::controller($controller, $c);
71 3
        });
72
73 3
        $c[BoneUserApiController::class] = $c->factory(function (Container $c) {
74
            /** @var UserService $userService */
75 1
            $userService = $c->get(UserService::class);
76 1
            $dir = $c->get('uploads_dir');
77 1
            $img = $c->get('image_dir');
78 1
            $tmp = $c->get('temp_dir');
79 1
            $mailService = $c->get(MailService::class);
80
81 1
            return Init::controller(new BoneUserApiController($userService, $dir, $img, $tmp, $mailService), $c);
82 3
        });
83
84
85 3
        $c[SessionAuth::class] = $c->factory(function (Container $c) {
86
            /** @var SessionManager $session */
87 2
            $session = $c->get(SessionManager::class);
88
            /** @var UserService $userService */
89 2
            $userService = $c->get(UserService::class);
90
            /** @var PasetoService $pasetoService */
91 2
            $pasetoService = $c->get(PasetoService::class);
92
93 2
            return new SessionAuth($session, $userService, $pasetoService);
94 3
        });
95
96 3
        $c[SessionAuthRedirect::class] = $c->factory(function (Container $c) {
97
            /** @var SessionManager $session */
98 1
            $session = $c->get(SessionManager::class);
99
            /** @var UserService $userService */
100 1
            $userService = $c->get(UserService::class);
101
            /** @var PasetoService $pasetoService */
102 1
            $pasetoService = $c->get(PasetoService::class);
103
104 1
            return new SessionAuthRedirect($session, $userService, $pasetoService);
105 3
        });
106
    }
107
108 1
    public function getAssetFolders(): array
109
    {
110 1
        return [
111 1
            'bone-user' => dirname(__DIR__) . '/data/assets',
112 1
        ];
113
    }
114
115 1
    public function getTranslationsDirectory(): string
116
    {
117 1
        return dirname(__DIR__) . '/data/translations';
118
    }
119
120 1
    public function addViews(): array
121
    {
122 1
        return [
123 1
            'boneuser' => __DIR__ . '/View/BoneUser/',
124 1
            'email.user' => __DIR__ . '/View/email/',
125 1
        ];
126
    }
127
128 1
    public function addViewExtensions(Container $c): array
129
    {
130 1
        $userService = $c->get(UserService::class);
131 1
        $translator = $c->get(Translator::class);
132 1
        $sessionManager = $c->get(SessionManager::class);
133 1
        $uploadFolder = $c->get('uploads_dir');
134 1
        $loginWidget = new LoginWidget($userService, $translator, $sessionManager, $uploadFolder);
135
136 1
        return [$loginWidget];
137
    }
138
139 1
    public function addRoutes(Container $c, Router $router): Router
140
    {
141 1
        $router->group('/user', function (RouteGroup $route) {
142 1
            $route->map('GET', '/', [BoneUserController::class, 'indexAction']);
143 1
            $route->map('GET', '/lost-password/{email}', [BoneUserController::class, 'forgotPasswordAction']);
144 1
            $route->map('GET', '/login', [BoneUserController::class, 'loginAction']);
145 1
            $route->map('POST', '/login', [BoneUserController::class, 'loginFormAction']);
146 1
            $route->map('GET', '/logout', [BoneUserController::class, 'logoutAction']);
147 1
            $route->map('GET', '/activate/{email}/{token}', [BoneUserController::class, 'activateAction']);
148 1
            $route->map('GET', '/reset-email/{email}/{new-email}/{token}', [BoneUserController::class, 'resetEmailAction']);
149 1
            $route->map('GET', '/reset-password/{email}/{token}', [BoneUserController::class, 'resetPasswordAction']);
150 1
            $route->map('POST', '/reset-password/{email}/{token}', [BoneUserController::class, 'resetPasswordAction']);
151 1
            $route->map('GET', '/resend-activation-mail/{email}', [BoneUserController::class, 'resendActivationEmailAction']);
152 1
        });
153
154 1
        $canRegister = true;
155 1
        $admin = false;
156
157 1
        if ($c->has('bone-user')) {
158 1
            $config = $c->get('bone-user');
159 1
            $canRegister = $config['enableRegistration'] ?? true;
160 1
            $admin = $config['admin'] ?? false;
161
        }
162
163 1
        if ($canRegister) {
164 1
            $router->map('GET', '/user/register', [BoneUserController::class, 'registerAction']);
165 1
            $router->map('POST', '/user/register', [BoneUserController::class, 'registerAction']);
166
        }
167
168 1
        $auth = $c->get(SessionAuth::class);
169 1
        $router->map('GET', '/user/change-password', [BoneUserController::class, 'changePasswordAction'])->middleware($auth);
170 1
        $router->map('POST', '/user/change-password', [BoneUserController::class, 'changePasswordAction'])->middleware($auth);
171 1
        $router->map('GET', '/user/change-email', [BoneUserController::class, 'changeEmailAction'])->middleware($auth);
172 1
        $router->map('POST', '/user/change-email', [BoneUserController::class, 'changeEmailAction'])->middleware($auth);
173 1
        $router->map('GET', '/user/edit-profile', [BoneUserController::class, 'editProfileAction'])->middleware($auth);
174 1
        $router->map('POST', '/user/edit-profile', [BoneUserController::class, 'editProfileAction'])->middleware($auth);
175 1
        $router->map('GET', '/user/home', [BoneUserController::class, 'homePageAction'])->middleware($auth);
176 1
        $factory = new ResponseFactory();
177 1
        $strategy = new JsonStrategy($factory);
178 1
        $strategy->setContainer($c);
179
180 1
        $router->group('/api/user', function (RouteGroup $route) use ($auth) {
181 1
            $route->map('POST', '/choose-avatar', [BoneUserApiController::class, 'chooseAvatarAction'])->middleware($auth);
182 1
            $route->map('POST', '/upload-avatar', [BoneUserApiController::class, 'uploadAvatarAction'])->middleware($auth);
183 1
            $route->map('GET', '/avatar', [BoneUserApiController::class, 'avatar'])->middleware($auth);
184 1
        })
185 1
        ->setStrategy($strategy);
186
187 1
        if ($admin === true) {
188 1
            $router->adminResource('people', PersonAdminController::class, $c);
189 1
            $router->adminResource('users', UserAdminController::class, $c);
190
        }
191
192 1
        return $router;
193
    }
194
195 1
    public function registerConsoleCommands(Container $container): array
196
    {
197 1
        return [
198 1
            new ResetPasswordCommand($container->get(UserService::class)),
199 1
            new CreateUserCommand($container->get(UserService::class)),
200 1
        ];
201
    }
202
203
    public function getFixtures(): array
204
    {
205
        return [
206
            LoadUsers::class
207
        ];
208
    }
209
210
    public function getAdminLinks(): array
211
    {
212
        $c = Application::ahoy()->getContainer();
213
        $admin = false;
214
215
        if ($c->has('bone-user')) {
216
            $config = $c->get('bone-user');
217
            $admin = $config['admin'] ?? false;
218
        }
219
220
        if ($admin === true) {
221
            return [
222
                new AdminLink('People', '/admin/people', 'nav-icon fa fa-address-book'),
223
                new AdminLink('Users', '/admin/users', 'nav-icon fa fa-users'),
224
            ];
225
        }
226
227
        return [];
228
    }
229
230
    public function getRequiredPackages(): array
231
    {
232
        return [
233
            'Bone\Mail\MailPackage',
234
            'Bone\BoneDoctrine\BoneDoctrinePackage',
235
            'Bone\Paseto\PasetoPackage',
236
            'Del\Person\PersonPackage',
237
            'Del\UserPackage',
238
            'Del\Passport\PassportPackage',
239
            'Bone\Passport\PassportPackage',
240
            'Bone\User\BoneUserPackage',
241
        ];
242
    }
243
244
    public function getSettingsFileName(): string
245
    {
246
        return __DIR__ . '/../data/config/bone-user.php';
247
    }
248
}
249