Test Failed
Push — master ( be437e...866f6c )
by Julien
07:25
created

Module   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 100
ccs 34
cts 44
cp 0.7727
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespace() 0 3 1
A getServices() 0 6 1
A registerServices() 0 21 1
A setServices() 0 6 1
A getDirname() 0 3 1
A registerAutoloaders() 0 7 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\Autoload\Loader;
15
use Phalcon\Cli\RouterInterface;
16
use Phalcon\Di\DiInterface;
17
use Phalcon\Mvc\ModuleDefinitionInterface;
18
use Zemit\Bootstrap\Config;
19
use Zemit\Support\Utils;
20
21
class Module implements ModuleDefinitionInterface
22
{
23
    public const NAME_CLI = 'cli';
24
    
25
    public string $name = self::NAME_CLI;
26
    
27
    public ?Config $config = null;
28
    
29
    public ?Dispatcher $dispatcher = null;
30
    
31
    public ?Loader $loader = null;
32
    
33
    public ?Router $router = null;
34
    
35
    /**
36
     * Registers an autoloader related to the frontend module
37
     */
38 1
    public function registerAutoloaders(DiInterface $container = null): void
39
    {
40 1
        $this->loader = $container['loader'] ?? new Loader();
41 1
        assert($this->loader instanceof Loader);
42
        
43 1
        $this->loader->setNamespaces($this->getNamespaces(), true);
44 1
        $this->loader->register();
45
    }
46
    
47
    /**
48
     * Registers services related to the module
49
     */
50 1
    public function registerServices(DiInterface $container): void
51
    {
52 1
        $this->getServices($container);
53
        
54 1
        assert($this->dispatcher instanceof DispatcherInterface);
55 1
        assert($this->router instanceof RouterInterface);
56
        
57
        // dispatcher settings
58
        $defaultNamespace = $this->getDefaultNamespace();
59
        $this->dispatcher->setDefaultNamespace($defaultNamespace);
60
        $this->dispatcher->setNamespaceName($defaultNamespace);
61
        
62
        // router settings
63
        $this->router->setDefaults([
0 ignored issues
show
Bug introduced by
The method setDefaults() 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

63
        $this->router->/** @scrutinizer ignore-call */ 
64
                       setDefaults([

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...
64
            'namespace' => $defaultNamespace,
65
            'module' => $this->name,
66
            'controller' => 'help',
67
            'action' => 'main',
68
        ]);
69
        
70
        $this->setServices($container);
71
    }
72
    
73 2
    public function getNamespaces(): array
74
    {
75 2
        $namespaces = [];
76
        
77
        // Caller namespace
78 2
        $namespace = $this->getNamespace();
79 2
        $dirname = $this->getDirname();
80
        
81
        // register the vendor module controllers
82 2
        $namespaces[$namespace . '\\Tasks'] = $dirname . '/Tasks/';
83 2
        $namespaces[$namespace . '\\Models'] = $dirname . '/Models/';
84
    
85
        // add zemit core models
86 2
        $corePath = dirname(__DIR__);
87 2
        $namespaces['Zemit\\Models'] = $corePath . '/Models/';
88
        
89 2
        return $namespaces;
90
    }
91
    
92 3
    public function getServices(DiInterface $container = null): void
93
    {
94 3
        $this->loader = $container['loader'] ?? new Loader();
95 3
        $this->config ??= $container['config'] ?? new Config();
96 3
        $this->router ??= $container['router'] ?? new Router();
97 3
        $this->dispatcher ??= $container['dispatcher'] ?? new Dispatcher();
98
    }
99
    
100 1
    public function setServices(DiInterface $container): void
101
    {
102 1
        $container->set('config', $this->config);
103 1
        $container->set('dispatcher', $this->dispatcher);
104 1
        $container->set('loader', $this->loader);
105 1
        $container->set('router', $this->router);
106
    }
107
    
108 1
    public function getDefaultNamespace(): string
109
    {
110 1
        return $this->getNamespace() . '\\Tasks';
111
    }
112
    
113 3
    public function getDirname(): string
114
    {
115 3
        return Utils::getDirname($this);
116
    }
117
    
118 4
    public function getNamespace(): string
119
    {
120 4
        return Utils::getNamespace($this);
121
    }
122
}
123