Completed
Push — master ( 4317fb...cd535a )
by Vladimir
02:31
created

Context::getChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Contracts\Channels\User;
8
use FondBot\Contracts\Core\Arrayable;
9
use FondBot\Contracts\Conversation\Intent;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, FondBot\Conversation\Intent.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use FondBot\Contracts\Channels\ReceivedMessage;
11
use FondBot\Contracts\Conversation\Interaction;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, FondBot\Conversation\Interaction.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
class Context implements Arrayable
14
{
15
    private $channel;
16
    private $user;
17
    private $message;
18
    private $intent;
19
    private $interaction;
20
    private $values;
21
22
    public function __construct(
23
        string $channel,
24
        User $user,
25
        ReceivedMessage $message,
26
        Intent $intent = null,
27
        Interaction $interaction = null,
28
        array $values = []
29
    ) {
30
        $this->channel = $channel;
31
        $this->user = $user;
32
        $this->message = $message;
33
        $this->intent = $intent;
34
        $this->interaction = $interaction;
35
        $this->values = $values;
36
    }
37
38
    /**
39
     * Get channel name.
40
     *
41
     * @return string
42
     */
43
    public function getChannel(): string
44
    {
45
        return $this->channel;
46
    }
47
48
    /**
49
     * Get user.
50
     *
51
     * @return User
52
     */
53
    public function getUser(): User
54
    {
55
        return $this->user;
56
    }
57
58
    /**
59
     * Get message received from user.
60
     *
61
     * @return ReceivedMessage
62
     */
63
    public function getMessage(): ReceivedMessage
64
    {
65
        return $this->message;
66
    }
67
68
    /**
69
     * Get current intent instance.
70
     *
71
     * @return Intent|null
72
     */
73
    public function getIntent(): ?Intent
74
    {
75
        return $this->intent;
76
    }
77
78
    /**
79
     * Set intent instance.
80
     *
81
     * @param Intent $intent
82
     */
83
    public function setIntent(Intent $intent): void
84
    {
85
        $this->intent = $intent;
86
    }
87
88
    /**
89
     * Get current interaction instance.
90
     *
91
     * @return Interaction|null
92
     */
93
    public function getInteraction(): ?Interaction
94
    {
95
        return $this->interaction;
96
    }
97
98
    /**
99
     * Set interaction instance.
100
     *
101
     * @param Interaction|null $interaction
102
     */
103
    public function setInteraction(?Interaction $interaction): void
104
    {
105
        $this->interaction = $interaction;
106
    }
107
108
    /**
109
     * Get stored values.
110
     *
111
     * @return array
112
     */
113
    public function getValues(): array
114
    {
115
        return $this->values;
116
    }
117
118
    /**
119
     * Set values to be stored.
120
     *
121
     * @param array $values
122
     */
123
    public function setValues(array $values): void
124
    {
125
        $this->values = $values;
126
    }
127
128
    /**
129
     * Store value.
130
     *
131
     * @param string $key
132
     * @param mixed  $value
133
     */
134
    public function setValue(string $key, $value): void
135
    {
136
        $this->values[$key] = $value;
137
    }
138
139
    /**
140
     * Get the instance as an array.
141
     *
142
     * @return array
143
     */
144
    public function toArray(): array
145
    {
146
        return [
147
            'intent' => $this->intent !== null ? get_class($this->intent) : null,
148
            'interaction' => $this->interaction !== null ? get_class($this->interaction) : null,
149
            'values' => $this->values,
150
        ];
151
    }
152
}
153