Passed
Push — 1.0 ( 12d132...dd1957 )
by Vladimir
06:10
created

SendsMessages::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Traits;
6
7
use FondBot\Drivers\Chat;
8
use FondBot\Drivers\User;
9
use FondBot\Contracts\Queue;
10
use FondBot\Application\Kernel;
11
use FondBot\Templates\Attachment;
12
use FondBot\Conversation\Template;
13
use FondBot\Drivers\Commands\SendMessage;
14
use FondBot\Drivers\Commands\SendRequest;
15
use FondBot\Drivers\Commands\SendAttachment;
16
17
trait SendsMessages
18
{
19
    /** @var Kernel */
20
    protected $kernel;
21
22
    /**
23
     * Send message to user.
24
     *
25
     * @param string        $text
26
     * @param Template|null $template
27
     */
28 2 View Code Duplication
    protected function sendMessage(string $text, Template $template = null): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
29
    {
30
        /** @var Queue $queue */
31 2
        $queue = $this->kernel->resolve(Queue::class);
32
33 2
        $command = new SendMessage($this->getChat(), $this->getUser(), $text, $template);
34 2
        $queue->push($this->kernel->getDriver(), $command);
35 2
    }
36
37
    /**
38
     * Send message to user with delay.
39
     *
40
     * @param int           $delay
41
     * @param string        $text
42
     * @param Template|null $template
43
     */
44 1 View Code Duplication
    protected function sendDelayedMessage(int $delay, string $text, Template $template = null): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
45
    {
46
        /** @var Queue $queue */
47 1
        $queue = $this->kernel->resolve(Queue::class);
48
49 1
        $command = new SendMessage($this->getChat(), $this->getUser(), $text, $template);
50 1
        $queue->later($this->kernel->getDriver(), $command, $delay);
51 1
    }
52
53
    /**
54
     * Send attachment to user.
55
     *
56
     * @param Attachment $attachment
57
     * @param int        $delay
58
     */
59 2
    protected function sendAttachment(Attachment $attachment, int $delay = 0): void
60
    {
61
        /** @var Queue $queue */
62 2
        $queue = $this->kernel->resolve(Queue::class);
63
64 2
        $command = new SendAttachment($this->getChat(), $this->getUser(), $attachment);
65
66 2
        if ($delay === 0) {
67 1
            $queue->push($this->kernel->getDriver(), $command);
68
        } else {
69 1
            $queue->later($this->kernel->getDriver(), $command, $delay);
70
        }
71 2
    }
72
73
    /**
74
     * Send request to the messaging service.
75
     *
76
     * @param string $endpoint
77
     * @param array  $parameters
78
     */
79 1 View Code Duplication
    protected function sendRequest(string $endpoint, array $parameters = []): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
80
    {
81
        /** @var Queue $queue */
82 1
        $queue = $this->kernel->resolve(Queue::class);
83
84 1
        $command = new SendRequest($this->getChat(), $this->getUser(), $endpoint, $parameters);
85 1
        $queue->push($this->kernel->getDriver(), $command);
86 1
    }
87
88
    /**
89
     * Get chat.
90
     *
91
     * @return Chat
92
     */
93
    abstract protected function getChat(): Chat;
94
95
    /**
96
     * Get user.
97
     *
98
     * @return User
99
     */
100
    abstract protected function getUser(): User;
101
}
102