Passed
Push — master ( 2d3be3...4d877f )
by Derek Stephen
07:12
created

BoneUserPackage::addRoutes()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 61
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 5.002

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 45
c 3
b 0
f 0
nc 16
nop 2
dl 0
loc 61
rs 8.8888
ccs 44
cts 46
cp 0.9565
crap 5.002

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\Console\CommandRegistrationInterface;
10
use Bone\Contracts\Container\FixtureProviderInterface;
11
use Bone\Controller\Init;
12
use Bone\Http\Middleware\HalEntity;
13
use Bone\Http\Middleware\JsonParse;
14
use Bone\Http\Middleware\Stack;
15
use Bone\I18n\I18nRegistrationInterface;
16
use Bone\Mail\Service\MailService;
17
use Bone\OAuth2\Http\Middleware\ResourceServerMiddleware;
0 ignored issues
show
Bug introduced by
The type Bone\OAuth2\Http\Middlew...esourceServerMiddleware was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Bone\OAuth2\Http\Middleware\ScopeCheck;
0 ignored issues
show
Bug introduced by
The type Bone\OAuth2\Http\Middleware\ScopeCheck was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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