Completed
Push — master ( edddde...b38748 )
by Hilmi Erdem
01:28
created

JetSmsHttpClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 121
Duplicated Lines 26.45 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 32
loc 121
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A sendShortMessage() 12 12 1
A sendShortMessages() 12 12 1
A getSendDate() 0 6 1
A getCredentials() 0 8 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
namespace Erdemkeren\JetSms\Http\Clients;
4
5
use GuzzleHttp\Client;
6
use Erdemkeren\JetSms\ShortMessage;
7
use Erdemkeren\JetSms\ShortMessageCollection;
8
use Erdemkeren\JetSms\Http\Responses\JetSmsHttpResponse;
9
use Erdemkeren\JetSms\Http\Responses\JetSmsResponseInterface;
10
11
/**
12
 * Class JetSmsHttpClient.
13
 */
14
class JetSmsHttpClient implements JetSmsClientInterface
15
{
16
    /**
17
     * The Http client.
18
     *
19
     * @var Client
20
     */
21
    private $httpClient;
22
23
    /**
24
     * The JetSms xml request url.
25
     *
26
     * @var string
27
     */
28
    private $url;
29
30
    /**
31
     * The auth username.
32
     *
33
     * @var string
34
     */
35
    private $username;
36
37
    /**
38
     * The auth password.
39
     *
40
     * @var string
41
     */
42
    private $password;
43
44
    /**
45
     * The outbox name.
46
     *
47
     * @var string
48
     */
49
    private $outboxName;
50
51
    /**
52
     * XmlJetSmsClient constructor.
53
     *
54
     * @param Client $client
55
     * @param string $url
56
     * @param string $username
57
     * @param string $password
58
     * @param string $outboxName
59
     */
60 View Code Duplication
    public function __construct(Client $client, $url, $username, $password, $outboxName)
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...
61
    {
62
        $this->httpClient = $client;
63
        $this->url = $url;
64
        $this->username = $username;
65
        $this->password = $password;
66
        $this->outboxName = $outboxName;
67
    }
68
69
    /**
70
     * Send a short message using the JetSms services.
71
     *
72
     * @param  ShortMessage $shortMessage
73
     *
74
     * @return JetSmsResponseInterface
75
     */
76 View Code Duplication
    public function sendShortMessage(ShortMessage $shortMessage)
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...
77
    {
78
        $guzzleResponse = $this->httpClient->request('POST', $this->url, [
79
            'form_params' => array_merge(
80
                $shortMessage->toArray(),
81
                $this->getSendDate(),
82
                $this->getCredentials()
83
            ),
84
        ]);
85
86
        return new JetSmsHttpResponse((string) $guzzleResponse->getBody());
87
    }
88
89
    /**
90
     * Send multiple short messages using the JetSms services.
91
     *
92
     * @param  ShortMessageCollection $shortMessageCollection
93
     *
94
     * @return JetSmsResponseInterface
95
     */
96 View Code Duplication
    public function sendShortMessages(ShortMessageCollection $shortMessageCollection)
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...
97
    {
98
        $guzzleResponse = $this->httpClient->request('POST', $this->url, [
99
            'form_params' => array_merge(
100
                $shortMessageCollection->toArray(),
101
                $this->getSendDate(),
102
                $this->getCredentials()
103
            ),
104
        ]);
105
106
        return new JetSmsHttpResponse((string) $guzzleResponse->getBody());
107
    }
108
109
    /**
110
     * Get the send date of the contents.
111
     *
112
     * @return array
113
     */
114
    private function getSendDate()
115
    {
116
        return [
117
            'SendDate' => null,
118
        ];
119
    }
120
121
    /**
122
     * Get the auth credentials array.
123
     *
124
     * @return array
125
     */
126
    private function getCredentials()
127
    {
128
        return [
129
            'Username'       => $this->username,
130
            'Password'       => $this->password,
131
            'TransmissionID' => $this->outboxName,
132
        ];
133
    }
134
}
135