Completed
Push — master ( 10533f...9c60cb )
by Vladimir
02:25
created

Kernel::sessionManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
46
    /**
47
     * Perform shutdown tasks.
48
     */
49
    public function terminate(): void
50
    {
51
        // Save session if exists
52
        if ($this->session !== null) {
53
            $this->sessionManager()->save($this->session);
54
        }
55
56
        // Save context if exists
57
        if ($this->context !== null) {
58
            $this->contextManager()->save($this->context);
59
        }
60
    }
61
62
    /**
63
     * Get current channel.
64
     *
65
     * @return Channel|null
66
     */
67
    public function getChannel(): ?Channel
68
    {
69
        return $this->channel;
70
    }
71
72
    /**
73
     * Get current driver.
74
     *
75
     * @return Driver|null
76
     */
77
    public function getDriver(): ?Driver
78
    {
79
        return $this->driver;
80
    }
81
82
    /**
83
     * Get session.
84
     *
85
     * @return Session|null
86
     */
87 1
    public function getSession(): ?Session
88
    {
89 1
        return $this->session;
90
    }
91
92
    /**
93
     * Set session.
94
     *
95
     * @param Session $session
96
     */
97 1
    public function setSession(Session $session): void
98
    {
99 1
        $this->session = $session;
100 1
    }
101
102
    /**
103
     * Close session.
104
     */
105 1
    public function closeSession(): void
106
    {
107 1
        if ($this->session !== null) {
108 1
            $this->sessionManager()->close($this->session);
109 1
            $this->session = null;
110
        }
111 1
    }
112
113
    /**
114
     * Get context.
115
     *
116
     * @return Context|null
117
     */
118 1
    public function getContext(): ?Context
119
    {
120 1
        return $this->context;
121
    }
122
123
    /**
124
     * Set context.
125
     *
126
     * @param Context $context
127
     */
128 1
    public function setContext(Context $context): void
129
    {
130 1
        $this->context = $context;
131 1
    }
132
133
    /**
134
     * Clear context.
135
     */
136 1
    public function clearContext(): void
137
    {
138 1
        if ($this->context !== null) {
139 1
            $this->contextManager()->clear($this->context);
140 1
            $this->context = null;
141
        }
142 1
    }
143
144
    /**
145
     * Get session manager.
146
     *
147
     * @return SessionManager
148
     */
149 1
    private function sessionManager(): SessionManager
150
    {
151 1
        return $this->container->make(SessionManager::class);
152
    }
153
154
    /**
155
     * Get context manager.
156
     *
157
     * @return ContextManager
158
     */
159 1
    private function contextManager(): ContextManager
160
    {
161 1
        return $this->container->make(ContextManager::class);
162
    }
163
}
164