Completed
Push — master ( ebdf7a...2dba39 )
by Vladimir
05:12
created

Context::load()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 3
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Chat;
8
use FondBot\User;
9
use FondBot\Channel;
10
use FondBot\FondBot;
11
use FondBot\Conversation;
12
use Illuminate\Support\Facades\Cache;
13
use Illuminate\Contracts\Support\Arrayable;
14
15
class Context implements Arrayable
16
{
17
    private $channel;
18
    private $chat;
19
    private $user;
20
    private $intent;
21
    private $interaction;
22 9
    private $items;
23
    private $attempts = 0;
24 9
25 9
    public function __construct(Channel $channel, Chat $chat, User $user, array $items = [])
26 9
    {
27 9
        $this->channel = $channel;
28 9
        $this->chat = $chat;
29
        $this->user = $user;
30 4
        $this->items = collect($items);
31
    }
32 4
33
    public static function load(Channel $channel, Chat $chat, User $user)
34
    {
35 4
        $value = Cache::get(static::getCacheKey($channel, $chat, $user), [
36
            'chat' => $chat,
37 4
            'user' => $user,
38
            'intent' => null,
39
            'interaction' => null,
40 4
            'items' => [],
41
        ]);
42 4
43
        $context = new static($channel, $chat, $user, $value['items'] ?? []);
44
45 2
        if (isset($value['intent'])) {
46
            $context->setIntent(resolve($value['intent']));
0 ignored issues
show
Bug introduced by
It seems like resolve($value['intent']) can also be of type Illuminate\Foundation\Application; however, parameter $intent of FondBot\Conversation\Context::setIntent() does only seem to accept FondBot\Conversation\Intent, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $context->setIntent(/** @scrutinizer ignore-type */ resolve($value['intent']));
Loading history...
47 2
        }
48
49
        if (isset($value['interaction'])) {
50 2
            $context->setInteraction(resolve($value['interaction']));
0 ignored issues
show
Bug introduced by
It seems like resolve($value['interaction']) can also be of type Illuminate\Foundation\Application; however, parameter $interaction of FondBot\Conversation\Context::setInteraction() does only seem to accept null|FondBot\Conversation\Interaction, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            $context->setInteraction(/** @scrutinizer ignore-type */ resolve($value['interaction']));
Loading history...
51
        }
52 2
53
        return $context;
54 2
    }
55
56
    public function save(): void
57 2
    {
58
        Cache::put(static::getCacheKey($this->channel, $this->chat, $this->user), $this->toArray(), FondBot::$contextTtl);
0 ignored issues
show
Bug introduced by
The property contextTtl is declared private in FondBot\FondBot and cannot be accessed from this context.
Loading history...
59 2
    }
60
61
    public function delete(): void
62 2
    {
63
        Cache::forget(static::getCacheKey($this->channel, $this->chat, $this->user));
64 2
    }
65
66 2
    public function getChannel(): Channel
67
    {
68
        return $this->channel;
69 5
    }
70
71 5
    public function getChat(): Chat
72
    {
73
        return $this->chat;
74 3
    }
75
76 3
    public function getUser(): User
77
    {
78 3
        return $this->user;
79
    }
80
81
    public function getIntent(): ?Intent
82
    {
83
        return $this->intent;
84
    }
85
86
    public function setIntent(Intent $intent): Context
87
    {
88
        $this->intent = $intent;
89
90
        return $this;
91
    }
92
93 2
    public function getInteraction(): ?Interaction
94
    {
95
        return $this->interaction;
96 2
    }
97 2
98 2
    public function setInteraction(?Interaction $interaction): Context
99 2
    {
100
        $this->interaction = $interaction;
101
102
        return $this;
103
    }
104
105
    public function getItem(string $key, $default = null)
106
    {
107
        return $this->items->get($key, $default);
108
    }
109
110
    public function setItem(string $key, $value): Context
111
    {
112
        $this->items->put($key, $value);
113
114
        return $this;
115
    }
116
117
    public function incrementAttempts(): Context
118
    {
119
        $this->attempts++;
120
121
        return $this;
122
    }
123
124
    public function attempts(): int
125
    {
126
        return $this->attempts;
127
    }
128
129
    public function toArray(): array
130
    {
131
        return [
132
            'intent' => $this->intent ? get_class($this->intent) : null,
133
            'interaction' => $this->interaction ? get_class($this->interaction) : null,
134
            'items' => $this->items->toArray(),
135
            'attempts' => $this->attempts,
136
        ];
137
    }
138
139
    public function __destruct()
140
    {
141
        if (Conversation::instance()->transitioned()) {
142
            $this->save();
143
        } else {
144
            $this->delete();
145
        }
146
    }
147
148
    private static function getCacheKey(Channel $channel, Chat $chat, User $user): string
149
    {
150
        return implode('.', ['context', $channel->getName(), $chat->getId(), $user->getId()]);
151
    }
152
}
153