Completed
Pull Request — master (#171)
by
unknown
02:33
created

ConversationBus::handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Telegram\Bot\Conversations;
4
5
use Telegram\Bot\Answers\AnswerBus;
6
use Telegram\Bot\Api;
7
use Telegram\Bot\Exceptions\TelegramSDKException;
8
use Telegram\Bot\Objects\Update;
9
10
/**
11
 * Class ConversationBus
12
 */
13
class ConversationBus extends AnswerBus
14
{
15
    /**
16
     * @var Conversation[]
17
     */
18
    private $conversations;
19
20
    /**
21
     * ConversationBus constructor.
22
     *
23
     * @param Api $telegram
24
     */
25 68
    public function __construct(Api $telegram)
26
    {
27 68
        $this->telegram = $telegram;
28 68
    }
29
30
    /**
31
     * @return Conversation[]
32
     */
33
    public function getConversations()
34
    {
35
        return $this->conversations;
36
    }
37
38
    /**
39
     * @param array $conversations
40
     * @return $this
41
     * @throws TelegramSDKException
42
     */
43
    public function addConversations(array $conversations)
44
    {
45
        foreach ($conversations as $conversation) {
46
            $this->addConversation($conversation);
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param $conversation
54
     * @return $this
55
     * @throws TelegramSDKException
56
     */
57
    public function addConversation($conversation)
58
    {
59 View Code Duplication
        if (!is_object($conversation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            if (!class_exists($conversation)) {
61
                throw new TelegramSDKException(
62
                    sprintf(
63
                        'Conversation class "%s" not found! Please make sure the class exists.',
64
                        $conversation
65
                    )
66
                );
67
            }
68
69
            if ($this->telegram->hasContainer()) {
70
                $conversation = $this->buildDependencyInjectedAnswer($conversation);
71
            } else {
72
                $conversation = new $conversation();
73
            }
74
        }
75
76
        if ($conversation instanceof ConversationInterface) {
77
            /*
78
             * At this stage we definitely have a proper conversation to use.
79
             *
80
             * @var Conversation $conversation
81
            */
82
            $this->conversations[get_class($conversation)] = $conversation;
83
84
            return $this;
85
        }
86
87
        throw new TelegramSDKException(
88
            sprintf(
89
                'Conversation class "%s" should be an instance of "Telegram\Bot\Conversations\ConversationInterface"',
90
                get_class($conversation)
91
            )
92
        );
93
    }
94
95
    /**
96
     * @param array $names
97
     * @return $this
98
     */
99
    public function removeConversations(array $names)
100
    {
101
        foreach ($names as $name) {
102
            $this->removeConversation($name);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param $name
110
     * @return $this
111
     */
112
    public function removeConversation($name)
113
    {
114
        unset($this->conversations[$name]);
115
116
        return $this;
117
    }
118
119
    /**
120
     * Handles Inbound Messages and Executes Appropriate Command.
121
     *
122
     * @param $message
123
     * @param $update
124
     *
125
     * @throws TelegramSDKException
126
     *
127
     * @return Update
128
     */
129
    protected function handler($message, Update $update)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        $this->execute($update->getMessage()->getFrom()->getCurrentConversation(), $update);
132
133
        return $update;
134
    }
135
136
    /**
137
     * Execute the command.
138
     *
139
     * @param $name
140
     * @param $message
141
     *
142
     * @return mixed
143
     */
144
    protected function execute($name, $update)
145
    {
146
        if (array_key_exists($name, $this->conversations)) {
147
            return $this->conversations[$name]->make($this->telegram, $update);
148
        }
149
150
        return 'Ok';
151
    }
152
}
153