Test Failed
Push — master ( 2a75d8...e4b8e4 )
by Julien
12:49
created

Module   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 95
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespace() 0 3 1
A getServices() 0 6 1
A registerServices() 0 18 1
A setServices() 0 6 1
A getDirname() 0 3 1
A registerAutoloaders() 0 5 1
A getDefaultNamespace() 0 3 1
A getNamespaces() 0 17 1
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Cli;
13
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Loader;
16
use Phalcon\Mvc\ModuleDefinitionInterface;
17
use Zemit\Bootstrap\Config;
18
use Zemit\Utils;
19
20
class Module implements ModuleDefinitionInterface
21
{
22
    public const NAME_CLI = 'cli';
23
    
24
    public string $name;
25
    
26
    public Config $config;
27
    
28
    public Dispatcher $dispatcher;
29
    
30
    public Loader $loader;
31
    
32
    public Router $router;
33
    
34
    /**
35
     * Registers an autoloader related to the frontend module
36
     */
37
    public function registerAutoloaders(DiInterface $container = null): void
38
    {
39
        $this->getServices($container);
40
        $this->loader->registerNamespaces($this->getNamespaces(), true);
41
        $this->loader->register();
42
    }
43
    
44
    /**
45
     * Registers services related to the module
46
     */
47
    public function registerServices(DiInterface $container): void
48
    {
49
        $this->getServices($container);
50
        
51
        // dispatcher settings
52
        $defaultNamespace = $this->getDefaultNamespace();
53
        $this->dispatcher->setDefaultNamespace($defaultNamespace);
54
        $this->dispatcher->setNamespaceName($defaultNamespace);
55
        
56
        // router settings
57
        $this->router->setDefaults([
58
            'namespace' => $defaultNamespace,
59
            'module' => $this->name,
60
            'controller' => 'help',
61
            'action' => 'main',
62
        ]);
63
        
64
        $this->setServices($container);
65
    }
66
    
67
    public function getNamespaces(): array
68
    {
69
        $namespaces = [];
70
        
71
        // Caller namespace
72
        $namespace = $this->getNamespace();
73
        $dirname = $this->getDirname();
74
        
75
        // register the vendor module controllers
76
        $namespaces[$namespace . '\\Tasks'] = $dirname . '/Tasks/';
77
        $namespaces[$namespace . '\\Models'] = $dirname . '/Models/';
78
    
79
        // add zemit core models
80
        $corePath = dirname(__DIR__);
81
        $namespaces['Zemit\\Models'] = $corePath . '/Models/';
82
        
83
        return $namespaces;
84
    }
85
    
86
    public function getServices(DiInterface $container = null): void
87
    {
88
        $this->config ??= $container['config'] ??= new Config();
89
        $this->loader ??= $container['loader'] ??= new Loader();
90
        $this->router ??= $container['router'] ??= new Router();
91
        $this->dispatcher ??= $container['dispatcher'] ??= new Dispatcher();
92
    }
93
    
94
    public function setServices(DiInterface $container = null): void
95
    {
96
        $container->set('config', $this->config);
0 ignored issues
show
Bug introduced by
The method set() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $container->/** @scrutinizer ignore-call */ 
97
                    set('config', $this->config);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
        $container->set('dispatcher', $this->dispatcher);
98
        $container->set('loader', $this->loader);
99
        $container->set('router', $this->router);
100
    }
101
    
102
    public function getDefaultNamespace(): string
103
    {
104
        return $this->getNamespace() . '\\Tasks';
105
    }
106
    
107
    public function getDirname(): string
108
    {
109
        return Utils::getDirname($this);
110
    }
111
    
112
    public function getNamespace(): string
113
    {
114
        return Utils::getNamespace($this);
115
    }
116
}
117