Completed
Push — master ( 610cde...3ccd61 )
by Mathieu
08:26
created

ScriptServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 1
A registerScriptFactory() 0 26 1
A registerClimate() 0 19 1
1
<?php
2
3
namespace Charcoal\App\ServiceProvider;
4
5
// From Pimple
6
use Pimple\ServiceProviderInterface;
7
use Pimple\Container;
8
9
// From 'league/climate'
10
use League\CLImate\CLImate;
11
12
// From 'charcoal-factory'
13
use Charcoal\Factory\GenericFactory as Factory;
14
15
use Charcoal\App\Route\ScriptRoute;
16
use Charcoal\App\Script\ScriptInterface;
17
18
/**
19
 * Script Service Provider
20
 */
21
class ScriptServiceProvider implements ServiceProviderInterface
22
{
23
    /**
24
     * Registers services on the given container.
25
     *
26
     * This method should only be used to configure services and parameters.
27
     * It should not get services.
28
     *
29
     * @param Container $container A container instance.
30
     * @return void
31
     */
32
    public function register(Container $container)
33
    {
34
        /** @var string The default route controller for templates. */
35
        $container['route/controller/script/class'] = ScriptRoute::class;
36
37
        $this->registerScriptFactory($container);
0 ignored issues
show
Unused Code introduced by
The call to the method Charcoal\App\ServiceProv...registerScriptFactory() 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...
38
        $this->registerClimate($container);
0 ignored issues
show
Unused Code introduced by
The call to the method Charcoal\App\ServiceProv...ider::registerClimate() 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...
39
    }
40
41
    /**
42
     * @param Container $container The DI container.
43
     * @return void
44
     */
45
    private function registerScriptFactory(Container $container)
46
    {
47
        /**
48
         * The Script Factory service is used to instantiate new scripts.
49
         *
50
         * - Scripts are `ScriptInterface` and must be suffixed with `Script`.
51
         * - The container is passed to the created script constructor, which will call `setDependencies()`.
52
         *
53
         * @param Container $container A container instance.
54
         * @return \Charcoal\Factory\FactoryInterface
55
         */
56
        $container['script/factory'] = function (Container $container) {
57
            return new Factory([
58
                'base_class'       => ScriptInterface::class,
59
                'resolver_options' => [
60
                    'suffix' => 'Script'
61
                ],
62
                'arguments' => [[
63
                    'container'      => $container,
64
                    'logger'         => $container['logger'],
65
                    'climate'        => $container['script/climate'],
66
                    'climate_reader' => $container['script/climate/reader']
67
                ]]
68
            ]);
69
        };
70
    }
71
72
    /**
73
     * @param Container $container A container instance.
74
     * @return void
75
     */
76
    private function registerClimate(Container $container)
77
    {
78
        /**
79
         * @param Container $container A container instance.
80
         * @return null|\League\CLImate\Util\Reader\ReaderInterface
81
         */
82
        $container['script/climate/reader'] = function () {
83
            return null;
84
        };
85
86
        /**
87
         * @param Container $container A container instance.
88
         * @return CLImate
89
         */
90
        $container['script/climate'] = function () {
91
            $climate = new CLImate();
92
            return $climate;
93
        };
94
    }
95
}
96