Completed
Pull Request — master (#50)
by Vladimir
04:58 queued 02:00
created

Kernel   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 209
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0
wmc 24
lcom 2
cbo 3

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 4 1
A getInstance() 0 4 1
A createInstance() 0 4 1
A boot() 0 5 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 closeSession() 0 7 2
A getContext() 0 4 1
A setContext() 0 4 1
A resolve() 0 4 1
A sessionManager() 0 4 1
A contextManager() 0 4 1
A terminate() 0 16 4
A clearContext() 0 7 2
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\Context;
11
use FondBot\Conversation\Session;
12
use FondBot\Conversation\ContextManager;
13
use FondBot\Conversation\SessionManager;
14
15
class Kernel
16
{
17
    public const VERSION = '2.0.0';
18
19
    /** @var Kernel */
20
    private static $instance;
21
22
    private $container;
23
    private $terminable;
24
25
    private $driver;
26
    private $channel;
27
    private $session;
28
    private $context;
29
30 170
    private function __construct(Container $container, bool $terminable = true)
31
    {
32 170
        $this->container = $container;
33 170
        $this->terminable = $terminable;
34 170
    }
35
36 5
    public function __destruct()
37
    {
38 5
        $this->terminate();
39 5
    }
40
41 34
    public static function getInstance(): Kernel
42
    {
43 34
        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...
44
    }
45
46 170
    public static function createInstance(Container $container, bool $terminable = true): Kernel
47
    {
48 170
        return static::$instance = new static($container, $terminable);
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...
49
    }
50
51
    /**
52
     * Boot kernel.
53
     *
54
     * @param Channel $channel
55
     * @param Driver  $driver
56
     */
57 1
    public function boot(Channel $channel, Driver $driver): void
58
    {
59 1
        $this->session = $this->sessionManager()->load($channel, $driver);
60 1
        $this->context = $this->contextManager()->load($channel, $driver);
61 1
    }
62
63
    /**
64
     * Perform shutdown tasks.
65
     */
66 7
    public function terminate(): void
67
    {
68 7
        if (!$this->terminable) {
69 1
            return;
70
        }
71
72
        // Save session if exists
73 6
        if ($this->session !== null) {
74 2
            $this->sessionManager()->save($this->session);
75
        }
76
77
        // Save context if exists
78 6
        if ($this->context !== null) {
79 2
            $this->contextManager()->save($this->context);
80
        }
81 6
    }
82
83
    /**
84
     * Get current channel.
85
     *
86
     * @return Channel|null
87
     */
88 7
    public function getChannel(): ?Channel
89
    {
90 7
        return $this->channel;
91
    }
92
93
    /**
94
     * Set channel.
95
     *
96
     * @param Channel $channel
97
     */
98 7
    public function setChannel(Channel $channel): void
99
    {
100 7
        $this->channel = $channel;
101 7
    }
102
103
    /**
104
     * Get current driver.
105
     *
106
     * @return Driver|null
107
     */
108 7
    public function getDriver(): ?Driver
109
    {
110 7
        return $this->driver;
111
    }
112
113
    /**
114
     * Set driver.
115
     *
116
     * @param Driver $driver
117
     */
118 7
    public function setDriver(Driver $driver): void
119
    {
120 7
        $this->driver = $driver;
121 7
    }
122
123
    /**
124
     * Get session.
125
     *
126
     * @return Session|null
127
     */
128 14
    public function getSession(): ?Session
129
    {
130 14
        return $this->session;
131
    }
132
133
    /**
134
     * Set session.
135
     *
136
     * @param Session $session
137
     */
138 14
    public function setSession(Session $session): void
139
    {
140 14
        $this->session = $session;
141 14
    }
142
143
    /**
144
     * Close session.
145
     *
146
     * @throws \Psr\Container\ContainerExceptionInterface
147
     */
148 3
    public function closeSession(): void
149
    {
150 3
        if ($this->session !== null) {
151 3
            $this->sessionManager()->close($this->session);
152 3
            $this->session = null;
153
        }
154 3
    }
155
156
    /**
157
     * Get context.
158
     *
159
     * @return Context|null
160
     */
161 3
    public function getContext(): ?Context
162
    {
163 3
        return $this->context;
164
    }
165
166
    /**
167
     * Set context.
168
     *
169
     * @param Context $context
170
     */
171 6
    public function setContext(Context $context): void
172
    {
173 6
        $this->context = $context;
174 6
    }
175
176
    /**
177
     * Clear context.
178
     */
179 3
    public function clearContext(): void
180
    {
181 3
        if ($this->context !== null) {
182 1
            $this->contextManager()->clear($this->context);
183 1
            $this->context = null;
184
        }
185 3
    }
186
187
    /**
188
     * Resolve an alias from container.
189
     *
190
     * @param string $alias
191
     * @param array  $args
192
     *
193
     * @return mixed
194
     *
195
     * @throws \Psr\Container\ContainerExceptionInterface
196
     */
197 38
    public function resolve(string $alias, array $args = [])
198
    {
199 38
        return $this->container->get($alias, $args);
200
    }
201
202
    /**
203
     * Get session manager.
204
     *
205
     * @return SessionManager
206
     *
207
     * @throws \Psr\Container\ContainerExceptionInterface
208
     */
209 6
    private function sessionManager(): SessionManager
210
    {
211 6
        return $this->resolve(SessionManager::class);
212
    }
213
214
    /**
215
     * Get context manager.
216
     *
217
     * @return ContextManager
218
     */
219 4
    private function contextManager(): ContextManager
220
    {
221 4
        return $this->resolve(ContextManager::class);
222
    }
223
}
224