Completed
Push — master ( be3bdc...1e521f )
by Vladimir
06:50
created

Kernel::getDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
ccs 0
cts 2
cp 0
crap 2
rs 10
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 FondBot\Conversation\Context;
10
use FondBot\Conversation\Session;
11
use FondBot\Conversation\ContextManager;
12
use FondBot\Conversation\SessionManager;
13
use Illuminate\Contracts\Container\Container;
14
15
class Kernel
16
{
17
    public const VERSION = '2.0';
18
19
    private $container;
20
21
    /** @var Channel */
22
    private $channel;
23
24
    /** @var Driver */
25
    private $driver;
26
27
    private $session;
28
    private $context;
29
30 2
    public function __construct(Container $container)
31
    {
32 2
        $this->container = $container;
33 2
    }
34
35
    /**
36
     * Initialize kernel.
37
     *
38
     * @param Channel $channel
39
     */
40
    public function initialize(Channel $channel): void
41
    {
42
        // Set channel
43
        $this->channel = $channel;
44
45
        // Resolve channel driver and initialize it
46
        $this->driver = $this->container->make($channel->getDriver());
47
        $this->driver->initialize($channel->getParameters());
48
    }
49
50
    /**
51
     * Perform shutdown tasks.
52
     */
53
    public function terminate(): void
54
    {
55
        // Save session if exists
56
        if ($this->session !== null) {
57
            $this->sessionManager()->save($this->session);
58
        }
59
60
        // Save context if exists
61
        if ($this->context !== null) {
62
            $this->contextManager()->save($this->context);
63
        }
64
    }
65
66
    /**
67
     * Get current channel.
68
     *
69
     * @return Channel|null
70
     */
71
    public function getChannel(): ?Channel
72
    {
73
        return $this->channel;
74
    }
75
76
    /**
77
     * Get current driver.
78
     *
79
     * @return Driver|null
80
     */
81
    public function getDriver(): ?Driver
82
    {
83
        return $this->driver;
84
    }
85
86
    /**
87
     * Get session.
88
     *
89
     * @return Session|null
90
     */
91 1
    public function getSession(): ?Session
92
    {
93 1
        return $this->session;
94
    }
95
96
    /**
97
     * Set session.
98
     *
99
     * @param Session $session
100
     */
101 1
    public function setSession(Session $session): void
102
    {
103 1
        $this->session = $session;
104 1
    }
105
106
    /**
107
     * Close session.
108
     */
109 1
    public function closeSession(): void
110
    {
111 1
        if ($this->session !== null) {
112 1
            $this->sessionManager()->close($this->session);
113 1
            $this->session = null;
114
        }
115 1
    }
116
117
    /**
118
     * Get context.
119
     *
120
     * @return Context|null
121
     */
122 1
    public function getContext(): ?Context
123
    {
124 1
        return $this->context;
125
    }
126
127
    /**
128
     * Set context.
129
     *
130
     * @param Context $context
131
     */
132 1
    public function setContext(Context $context): void
133
    {
134 1
        $this->context = $context;
135 1
    }
136
137
    /**
138
     * Clear context.
139
     */
140 1
    public function clearContext(): void
141
    {
142 1
        if ($this->context !== null) {
143 1
            $this->contextManager()->clear($this->context);
144 1
            $this->context = null;
145
        }
146 1
    }
147
148
    /**
149
     * Get session manager.
150
     *
151
     * @return SessionManager
152
     */
153 1
    private function sessionManager(): SessionManager
154
    {
155 1
        return $this->container->make(SessionManager::class);
156
    }
157
158
    /**
159
     * Get context manager.
160
     *
161
     * @return ContextManager
162
     */
163 1
    private function contextManager(): ContextManager
164
    {
165 1
        return $this->container->make(ContextManager::class);
166
    }
167
}
168