BoneUserPackage::addToContainer()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 32
c 4
b 0
f 2
dl 0
loc 57
ccs 36
cts 36
cp 1
rs 9.408
cc 3
nc 1
nop 1
crap 3

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