Completed
Push — master ( e90d9e...f5d8ab )
by Vladimir
03:02
created

InteractsWithSession::context()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Traits;
6
7
use FondBot\Helpers\Arr;
8
use FondBot\Drivers\Chat;
9
use FondBot\Drivers\User;
10
use FondBot\Application\Kernel;
11
12
trait InteractsWithSession
13
{
14
    /** @var Kernel */
15
    protected $kernel;
16
17
    /**
18
     * Get the whole context or a single value.
19
     *
20
     * @param string|null $key
21
     *
22
     * @return array|mixed
23
     */
24 1
    protected function context(string $key = null)
25
    {
26 1
        $context = $this->kernel->getSession()->getContext();
27 1
        if ($key === null) {
28 1
            return $context;
29
        }
30
31 1
        return Arr::get($context, $key);
32
    }
33
34
    /**
35
     * Remember value in context.
36
     *
37
     * @param string $key
38
     * @param        $value
39
     */
40 2
    protected function remember(string $key, $value): void
41
    {
42 2
        $this->kernel->getSession()->setContextValue($key, $value);
43 2
    }
44
45
    /**
46
     * Get chat.
47
     *
48
     * @return Chat
49
     */
50 1
    protected function getChat(): Chat
51
    {
52 1
        return $this->kernel->getSession()->getChat();
53
    }
54
55
    /**
56
     * Get user.
57
     *
58
     * @return User
59
     */
60 2
    protected function getUser(): User
61
    {
62 2
        return $this->kernel->getSession()->getUser();
63
    }
64
}
65