Completed
Push — master ( c4745e...42612c )
by
unknown
02:52
created

AdminModule::setup()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 2
Metric Value
c 10
b 2
f 2
dl 0
loc 36
rs 8.8571
cc 3
eloc 18
nc 2
nop 0
1
<?php
2
3
namespace Charcoal\Admin;
4
5
// Dependencies from PSR-7 (HTTP Messaging)
6
use \Psr\Http\Message\RequestInterface;
7
use \Psr\Http\Message\ResponseInterface;
8
9
// Dependency from Pimple
10
use \Pimple\Container;
11
12
// Dependency from 'charcoal-app'
13
use \Charcoal\App\Handler\HandlerInterface;
14
use \Charcoal\App\Module\AbstractModule;
15
16
// Intra-module ('charcoal-admin') dependencies
17
use \Charcoal\Admin\Config as AdminConfig;
18
19
/**
20
 * Charcoal Administration Module
21
 */
22
class AdminModule extends AbstractModule
23
{
24
    /**
25
     * Charcoal Administration Setup.
26
     *
27
     * This module is bound to the `/admin` URL.
28
     *
29
     * ## Provides
30
     * - `charcoal/admin/module` An instance of this module
31
     *   - Exact type: `\Charcoal\Admin\AdminModule`
32
     *   - which implements `\Charcoal\Module\ModuleInterface`
33
     * - `charcoal/admin/config`
34
     * - `
35
     *
36
     * ## Dependencies
37
     * - `charcoal/config` Provided by \Charcoal\CharcoalModule
38
     *
39
     * @return AdminModule
40
     */
41
    public function setup()
42
    {
43
        // A session is necessary for the admin module
44
        if (session_id() === '') {
45
            session_start();
46
        }
47
48
        $container = $this->app()->getContainer();
49
50
        $module = $this;
51
        $container['charcoal/admin/module'] = function ($c) use ($module) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
            return $module;
53
        };
54
55
        $container['charcoal/admin/config'] = function ($c) {
56
            $config = new AdminConfig();
57
58
            if ($c['config']->has('admin')) {
59
                $config->merge($c['config']->get('admin'));
0 ignored issues
show
Unused Code introduced by
The call to the method Charcoal\Admin\Config::merge() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
60
            }
61
62
            return $config;
63
        };
64
65
        $config = $container['charcoal/admin/config'];
66
67
        $this->setConfig($config);
0 ignored issues
show
Documentation introduced by
$config is of type object<Closure>, but the function expects a object<Charcoal\App\Module\ConfigInterface>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
69
        $groupIdent = '/'.trim($config['base_path'], '/');
70
71
        // Add the route group
72
        $this->app()->group($groupIdent, 'charcoal/admin/module:setupRoutes')
73
                    ->add('charcoal/admin/module:setupHandlers');
74
75
        return $this;
76
    }
77
78
    /**
79
     * Set up the module's handlers, via group middleware.
80
     *
81
     * @param  RequestInterface  $request  A PSR7 request object.
82
     * @param  ResponseInterface $response A PSR7 response object.
83
     * @param  callable          $next     The next callable middleware.
84
     * @return ResponseInterface A PSR7 response object.
85
     */
86
    public function setupHandlers(RequestInterface $request, ResponseInterface $response, callable $next)
87
    {
88
        $config    = $this->config();
89
        $container = $this->app()->getContainer();
90
        $baseUrl   = $container['request']->getUri()->getBaseUrl();
91
        $adminUrl  = rtrim($baseUrl, '/\\').'/'.trim($config['base_path'], '/\\');
92
93
        if (isset($config['handlers'])) {
94
95
            /**
96
             * HTTP 404 (Not Found) handler.
97
             *
98
             * @param  object|HandlerInterface $handler   An error handler instance.
99
             * @param  Container               $container A container instance.
100
             * @return HandlerInterface
101
             */
102 View Code Duplication
            $container->extend('notFoundHandler', function ($handler, Container $container) use ($config, $adminUrl) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
                if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) {
104
                    $handler->config()->merge($config['handlers.defaults']);
105
                    $handler->setBaseUrl($adminUrl);
106
                }
107
108
                return $handler;
109
            });
110
111
            /**
112
             * HTTP 405 (Not Allowed) handler.
113
             *
114
             * @param  object|HandlerInterface $handler   An error handler instance.
115
             * @param  Container               $container A container instance.
116
             * @return HandlerInterface
117
             */
118 View Code Duplication
            $container->extend('notAllowedHandler', function ($handler, Container $container) use ($config, $adminUrl) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
                if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) {
120
                    $handler->config()->merge($config['handlers.defaults']);
121
                    $handler->setBaseUrl($adminUrl);
122
                }
123
124
                return $handler;
125
            });
126
127
            /**
128
             * HTTP 500 (Error) handler for PHP 7+ Throwables.
129
             *
130
             * @param  object|HandlerInterface $handler   An error handler instance.
131
             * @param  Container               $container A container instance.
132
             * @return HandlerInterface
133
             */
134 View Code Duplication
            $container->extend('phpErrorHandler', function ($handler, Container $container) use ($config, $adminUrl) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
                if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) {
136
                    $handler->config()->merge($config['handlers.defaults']);
137
                    $handler->setBaseUrl($adminUrl);
138
                }
139
140
                return $handler;
141
            });
142
143
            /**
144
             * HTTP 500 (Error) handler.
145
             *
146
             * @param  object|HandlerInterface $handler   An error handler instance.
147
             * @param  Container               $container A container instance.
148
             * @return HandlerInterface
149
             */
150 View Code Duplication
            $container->extend('errorHandler', function ($handler, Container $container) use ($config, $adminUrl) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
                if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) {
152
                    $handler->config()->merge($config['handlers.defaults']);
153
                    $handler->setBaseUrl($adminUrl);
154
                }
155
156
                return $handler;
157
            });
158
159
            /**
160
             * HTTP 503 (Service Unavailable) handler.
161
             *
162
             * This handler is not part of Slim.
163
             *
164
             * @param  object|HandlerInterface $handler   An error handler instance.
165
             * @param  Container               $container A container instance.
166
             * @return HandlerInterface
167
             */
168 View Code Duplication
            $container->extend('shutdownHandler', function ($handler, Container $container) use ($config, $adminUrl) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
                if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) {
170
                    $handler->config()->merge($config['handlers.defaults']);
171
                    $handler->setBaseUrl($adminUrl);
172
                }
173
174
                return $handler;
175
            });
176
        }
177
178
        return $next($request, $response);
179
    }
180
}
181