Completed
Push — master ( ab842b...870e7f )
by
unknown
02:49
created

AdminModule::createConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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 'charcoal-app'
10
use \Charcoal\App\Module\AbstractModule;
11
12
// Intra-module ('charcoal-admin') dependencies
13
use \Charcoal\Admin\Config as AdminConfig;
14
15
/**
16
 * Charcoal Administration Module
17
 */
18
class AdminModule extends AbstractModule
0 ignored issues
show
Bug introduced by
There is one abstract method createConfig in this class; you could implement it, or declare this class as abstract.
Loading history...
19
{
20
    /**
21
     * Charcoal Administration Setup.
22
     *
23
     * This module is bound to the `/admin` URL.
24
     *
25
     * ## Provides
26
     * - `charcoal/admin/module` An instance of this module
27
     *   - Exact type: `\Charcoal\Admin\AdminModule`
28
     *   - which implements `\Charcoal\Module\ModuleInterface`
29
     * - `charcoal/admin/config`
30
     * - `
31
     *
32
     * ## Dependencies
33
     * - `charcoal/config` Provided by \Charcoal\CharcoalModule
34
     *
35
     * @return AdminModule
36
     */
37
    public function setup()
38
    {
39
        // A session is necessary for the admin module
40
        if (session_id() === '') {
41
            session_start();
42
        }
43
44
        $container = $this->app()->getContainer();
45
46
        $module = $this;
47
        $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...
48
            return $module;
49
        };
50
51
        $container['charcoal/admin/config'] = function ($c) {
52
            $config = new AdminConfig();
53
54
            if ($c['config']->has('admin')) {
55
                $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...
56
            }
57
58
            return $config;
59
        };
60
61
        $config = $container['charcoal/admin/config'];
62
63
        $this->setConfig($config);
0 ignored issues
show
Documentation introduced by
$config is of type object<Closure>, but the function expects a object<Charcoal\Config\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...
64
65
        $group = '/'.trim($config['base_path'], '/');
66
67
        if (isset($config['routes']['default_view'])) {
68
            $this->app()->get(
69
                $group,
70
                function (RequestInterface $request, ResponseInterface $response) use ($group, $config) {
71
                    return $response->withRedirect(
72
                        $group.'/'.ltrim($config['routes']['default_view'], '/'),
73
                        303
74
                    );
75
                }
76
            );
77
        }
78
        $this->app()->group($group, 'charcoal/admin/module:setupRoutes');
79
80
        return $this;
81
    }
82
}
83