Test Failed
Push — master ( bdaab5...176ee2 )
by Vladimir
06:36
created

Kernel   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 16
lcom 2
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 4 1
A createInstance() 0 4 1
A getChannel() 0 4 1
A setChannel() 0 4 1
A getDriver() 0 4 1
A setDriver() 0 4 1
A getSession() 0 4 1
A setSession() 0 4 1
A loadSession() 0 4 1
A saveSession() 0 6 2
A closeSession() 0 7 2
A resolve() 0 4 1
A sessionManager() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Foundation;
6
7
use FondBot\Drivers\Driver;
8
use FondBot\Channels\Channel;
9
use League\Container\Container;
10
use FondBot\Conversation\Session;
11
use FondBot\Conversation\SessionManager;
12
13
class Kernel
14
{
15
    public const VERSION = '1.1.0';
16
17
    /** @var Kernel */
18
    private static $instance;
19
20
    private $container;
21
22
    private $driver;
23
    private $channel;
24
    private $session;
25
26
    private function __construct(Container $container)
27
    {
28
        $this->container = $container;
29
    }
30
31
    public static function getInstance(): Kernel
32
    {
33
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
34
    }
35
36
    public static function createInstance(Container $container): Kernel
37
    {
38
        return static::$instance = new static($container);
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
39
    }
40
41
    /**
42
     * Get current channel.
43
     *
44
     * @return Channel|null
45
     */
46
    public function getChannel(): ?Channel
47
    {
48
        return $this->channel;
49
    }
50
51
    /**
52
     * Set channel.
53
     *
54
     * @param Channel $channel
55
     */
56
    public function setChannel(Channel $channel): void
57
    {
58
        $this->channel = $channel;
59
    }
60
61
    /**
62
     * Get current driver.
63
     *
64
     * @return Driver|null
65
     */
66
    public function getDriver(): ?Driver
67
    {
68
        return $this->driver;
69
    }
70
71
    /**
72
     * Set driver.
73
     *
74
     * @param Driver $driver
75
     */
76
    public function setDriver(Driver $driver): void
77
    {
78
        $this->driver = $driver;
79
    }
80
81
    /**
82
     * Get session.
83
     *
84
     * @return Session|null
85
     */
86
    public function getSession(): ?Session
87
    {
88
        return $this->session;
89
    }
90
91
    /**
92
     * Set session.
93
     *
94
     * @param Session $session
95
     */
96
    public function setSession(Session $session): void
97
    {
98
        $this->session = $session;
99
    }
100
101
    /**
102
     * Load session.
103
     *
104
     * @param Channel $channel
105
     * @param Driver  $driver
106
     */
107
    public function loadSession(Channel $channel, Driver $driver): void
108
    {
109
        $this->session = $this->sessionManager()->load($channel, $driver);
110
    }
111
112
    /**
113
     * Save session.
114
     */
115
    public function saveSession(): void
116
    {
117
        if ($this->session !== null) {
118
            $this->sessionManager()->save($this->session);
119
        }
120
    }
121
122
    /**
123
     * Close session.
124
     *
125
     * @throws \Psr\Container\ContainerExceptionInterface
126
     */
127
    public function closeSession(): void
128
    {
129
        if ($this->session !== null) {
130
            $this->sessionManager()->close($this->session);
131
            $this->session = null;
132
        }
133
    }
134
135
    /**
136
     * Resolve an alias from container.
137
     *
138
     * @param string $alias
139
     * @param array  $args
140
     *
141
     * @return mixed
142
     *
143
     * @throws \Psr\Container\ContainerExceptionInterface
144
     */
145
    public function resolve(string $alias, array $args = [])
146
    {
147
        return $this->container->get($alias, $args);
148
    }
149
150
    /**
151
     * Get session manager.
152
     *
153
     * @return SessionManager
154
     *
155
     * @throws \Psr\Container\ContainerExceptionInterface
156
     */
157
    private function sessionManager(): SessionManager
158
    {
159
        return $this->resolve(SessionManager::class);
160
    }
161
}
162