Completed
Push — master ( afce1d...f8221d )
by Vladimir
09:37
created

SendsMessages   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 28.99 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
dl 20
loc 69
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMessage() 0 10 2
A sendAttachment() 10 10 2
A sendRequest() 10 10 2
getChat() 0 1 ?
getUser() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Template;
10
use FondBot\Templates\Attachment;
11
use FondBot\Drivers\Commands\SendMessage;
12
use FondBot\Drivers\Commands\SendRequest;
13
use FondBot\Drivers\Commands\SendAttachment;
14
15
trait SendsMessages
16
{
17
    /**
18
     * Send message to user.
19
     *
20
     * @param string|null   $text
21
     * @param Template|null $template
22
     * @param int           $delay
23
     */
24 3
    protected function sendMessage(string $text = null, Template $template = null, int $delay = 0): void
25
    {
26 3
        $command = new SendMessage($this->getChat(), $this->getUser(), $text, $template);
27
28 3
        if ($delay > 0) {
29 1
            $command->delay($delay);
30
        }
31
32 3
        kernel()->dispatch($command);
33 3
    }
34
35
    /**
36
     * Send attachment to user.
37
     *
38
     * @param Attachment $attachment
39
     * @param int        $delay
40
     */
41 2 View Code Duplication
    protected function sendAttachment(Attachment $attachment, int $delay = 0): 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...
42
    {
43 2
        $command = new SendAttachment($this->getChat(), $this->getUser(), $attachment);
44
45 2
        if ($delay > 0) {
46 1
            $command->delay($delay);
47
        }
48
49 2
        kernel()->dispatch($command);
50 2
    }
51
52
    /**
53
     * Send request to the messaging service.
54
     *
55
     * @param string $endpoint
56
     * @param array  $parameters
57
     * @param int    $delay
58
     */
59 1 View Code Duplication
    protected function sendRequest(string $endpoint, array $parameters = [], int $delay = 0): 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...
60
    {
61 1
        $command = new SendRequest($this->getChat(), $this->getUser(), $endpoint, $parameters);
62
63 1
        if ($delay > 0) {
64
            $command->delay($delay);
65
        }
66
67 1
        kernel()->dispatch($command);
68 1
    }
69
70
    /**
71
     * Get chat.
72
     *
73
     * @return Chat
74
     */
75
    abstract protected function getChat(): Chat;
76
77
    /**
78
     * Get user.
79
     *
80
     * @return User
81
     */
82
    abstract protected function getUser(): User;
83
}
84