Core   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 9
Bugs 2 Features 0
Metric Value
eloc 16
dl 0
loc 56
rs 10
c 9
b 2
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentContext() 0 3 1
A get() 0 3 1
A start() 0 18 3
A __construct() 0 3 1
A getContext() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Cervo package.
5
 *
6
 * Copyright (c) 2010-2019 Nevraxe inc. & Marc André Audet <[email protected]>.
7
 *
8
 * @package   Cervo
9
 * @author    Marc André Audet <[email protected]>
10
 * @copyright 2010 - 2019 Nevraxe inc. & Marc André Audet
11
 * @license   See LICENSE.md  MIT
12
 * @link      https://github.com/Nevraxe/Cervo
13
 * @since     5.0.0
14
 */
15
16
declare(strict_types=1);
17
18
namespace Cervo;
19
20
use Cervo\Config\BaseConfig;
21
use Cervo\Exceptions\ControllerReflection\AlreadyInitialisedException;
22
use Cervo\Interfaces\SingletonInterface;
23
24
25
/**
26
 * Core class for Cervo.
27
 *
28
 * @author Marc André Audet <[email protected]>
29
 */
30
final class Core
31
{
32
    /** @var bool */
33
    private $isInit = false;
34
35
    /** @var Context */
36
    private $context = null;
37
38
    /** @var Context|null */
39
    private static $global_context = null;
40
41
    public function __construct(?BaseConfig $config = null)
42
    {
43
        $this->context = new Context($config);
44
    }
45
46
    public static function getCurrentContext(): ?Context
47
    {
48
        return self::$global_context;
49
    }
50
51
    /**
52
     * Fetch an object from the Singletons registry, or instantialise it.
53
     *
54
     * @param string $className The name of the class to get as Singleton
55
     *
56
     * @return SingletonInterface
57
     */
58
    public static function get(string $className): SingletonInterface
59
    {
60
        return self::$global_context->getSingletons()->get($className);
0 ignored issues
show
Bug introduced by
The method getSingletons() 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

60
        return self::$global_context->/** @scrutinizer ignore-call */ getSingletons()->get($className);

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...
61
    }
62
63
    public function start()
64
    {
65
        if ($this->isInit === true) {
66
            throw new AlreadyInitialisedException;
67
        }
68
69
        $this->isInit = true;
70
71
        self::$global_context = $this->context;
72
73
        /** @var Router $router */
74
        $router = $this->getContext()->getSingletons()->get(Router::class);
75
76
        foreach ($this->getContext()->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) {
77
            $router->loadPath($path);
78
        }
79
80
        (new ControllerReflection($this->getContext(), $router->dispatch()))();
81
    }
82
83
    public function getContext(): Context
84
    {
85
        return $this->context;
86
    }
87
}
88