Completed
Push — master ( 5c498e...edddde )
by Hilmi Erdem
01:29
created

JetSmsXmlClient::sendShortMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Erdemkeren\JetSms\Http\Clients;
4
5
use Erdemkeren\JetSms\ShortMessage;
6
use Erdemkeren\JetSms\ShortMessageCollection;
7
use Erdemkeren\JetSms\Http\Responses\JetSmsXmlResponse;
8
use Erdemkeren\JetSms\Http\Responses\JetSmsResponseInterface;
9
10
/**
11
 * Class JetSmsXmlClient.
12
 */
13
class JetSmsXmlClient implements JetSmsClientInterface
14
{
15
    /**
16
     * The JetSms xml request url.
17
     *
18
     * @var string
19
     */
20
    private $url;
21
22
    /**
23
     * The auth username.
24
     *
25
     * @var string
26
     */
27
    private $username;
28
29
    /**
30
     * The auth password.
31
     *
32
     * @var string
33
     */
34
    private $password;
35
36
    /**
37
     * The outbox name.
38
     *
39
     * @var string
40
     */
41
    private $outboxName;
42
43
    /**
44
     * XmlJetSmsClient constructor.
45
     *
46
     * @param string $url
47
     * @param string $username
48
     * @param string $password
49
     * @param string $outboxName
50
     */
51 2 View Code Duplication
    public function __construct($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...
52
    {
53 2
        $this->url = $url;
54 2
        $this->username = $username;
55 2
        $this->password = $password;
56 2
        $this->outboxName = $outboxName;
57 2
    }
58
59
    /**
60
     * Send a short message using the JetSms services.
61
     *
62
     * @param  ShortMessage $shortMessage
63
     *
64
     * @return JetSmsResponseInterface
65
     */
66 1
    public function sendShortMessage(ShortMessage $shortMessage)
67
    {
68 1
        $payload = $this->generateSingleMessageBody($shortMessage);
69
70 1
        return new JetSmsXmlResponse($this->performCurlSession($payload));
71
    }
72
73
    /**
74
     * Send multiple short messages using the JetSms services.
75
     *
76
     * @param  ShortMessageCollection $shortMessageCollection
77
     *
78
     * @return JetSmsResponseInterface
79
     */
80 1
    public function sendShortMessages(ShortMessageCollection $shortMessageCollection)
81
    {
82 1
        $payload = $this->generateMultipleMessageBody($shortMessageCollection);
83
84 1
        return new JetSmsXmlResponse($this->performCurlSession($payload));
85
    }
86
87
    /**
88
     * Generate the xml http request body.
89
     *
90
     * @param  ShortMessage $shortMessage
91
     *
92
     * @return string
93
     */
94 1
    private function generateSingleMessageBody(ShortMessage $shortMessage)
95
    {
96 1
        $body = '<?xml version="1.0" encoding="iso-8859-9"?>';
97 1
        $body .= '<message-context type="smmgsd">';
98 1
        $body .= $this->generateAuthTags();
99 1
        $body .= $this->generateValidityRangeTags();
100 1
        $body .= $shortMessage->toSingleMessageXml();
101 1
        $body .= '</message-context>';
102
103 1
        return $body;
104
    }
105
106
    /**
107
     * Generate the xml http request body.
108
     *
109
     * @param  ShortMessageCollection $shortMessageCollection
110
     *
111
     * @return string
112
     */
113 1
    private function generateMultipleMessageBody(ShortMessageCollection $shortMessageCollection)
114
    {
115 1
        $body = '<?xml version="1.0" encoding="iso-8859-9"?>';
116 1
        $body .= '<message-context type="mmmgsd">';
117 1
        $body .= $this->generateAuthTags();
118 1
        $body .= $this->generateValidityRangeTags();
119 1
        $body .= $shortMessageCollection->toXml();
120 1
        $body .= '</message-context>';
121
122 1
        return $body;
123
    }
124
125 2
    private function generateValidityRangeTags()
126
    {
127 2
        return '<start-date></start-date><expire-date></expire-date>';
128
    }
129
130
    /**
131
     * Get the auth credentials as xml tags.
132
     *
133
     * @return string
134
     */
135 2
    private function generateAuthTags()
136
    {
137 2
        return "<username>{$this->username}</username>"
138 2
            . "<password>{$this->password}</password>"
139 2
            . "<outbox-name>{$this->outboxName}</outbox-name>";
140
    }
141
142
    /**
143
     * Perform the curl session.
144
     *
145
     * @param  string $payload
146
     *
147
     * @return string
148
     */
149 2
    private function performCurlSession($payload)
150
    {
151 2
        $perCurlConnection = curl_init();
152 2
        curl_setopt($perCurlConnection, CURLOPT_URL, $this->url);
153 2
        curl_setopt($perCurlConnection, CURLOPT_RETURNTRANSFER, 1);
154 2
        curl_setopt($perCurlConnection, CURLOPT_TIMEOUT, 10);
155 2
        curl_setopt($perCurlConnection, CURLOPT_POSTFIELDS, $payload);
156
157 2
        $result = curl_exec($perCurlConnection);
158
159 2
        return $result;
160
    }
161
}
162