Passed
Push — master ( 3e13b9...155993 )
by Vladimir
02:26
created

InteractsWithContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
ccs 9
cts 13
cp 0.6923
rs 10
wmc 5
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getChat() 0 4 1
A getUser() 0 4 1
A context() 0 10 2
A remember() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Traits;
6
7
use FondBot\Channels\Chat;
8
use FondBot\Channels\User;
9
10
trait InteractsWithContext
11
{
12
    /**
13
     * Get chat.
14
     *
15
     * @return Chat
16
     */
17
    protected function getChat(): Chat
18
    {
19
        return kernel()->getContext()->getChat();
20
    }
21
22
    /**
23
     * Get user.
24
     *
25
     * @return User
26
     */
27
    protected function getUser(): User
28
    {
29
        return kernel()->getContext()->getUser();
30
    }
31
32
    /**
33
     * Get the whole context or a single value.
34
     *
35
     * @param string|null $key
36
     * @param mixed       $default
37
     *
38
     * @return array|mixed
39
     */
40 2
    protected function context(string $key = null, $default = null)
41
    {
42 2
        $context = context();
43
44 2
        if ($key === null) {
45 1
            return $context;
46
        }
47
48 2
        return $context->get($key, $default);
49
    }
50
51
    /**
52
     * Remember value in context.
53
     *
54
     * @param string $key
55
     * @param mixed  $value
56
     */
57 1
    protected function remember(string $key, $value): void
58
    {
59 1
        $context = context();
60
61 1
        $context->set($key, $value);
62 1
    }
63
}
64