Completed
Push — master ( 397d55...ea8bb0 )
by recca
35:35 queued 33:52
created

Client::send()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
rs 8.5806
cc 4
eloc 17
nc 2
nop 1
crap 4.0039
1
<?php
2
3
namespace Recca0120\Every8d;
4
5
use Carbon\Carbon;
6
use DomainException;
7
use Http\Client\HttpClient;
8
use Http\Message\MessageFactory;
9
use Http\Discovery\HttpClientDiscovery;
10
use Http\Discovery\MessageFactoryDiscovery;
11
12
class Client
13
{
14
    /**
15
     * $apiEndpoint.
16
     *
17
     * @var string
18
     */
19
    public $apiEndpoint = 'http://api.every8d.com/API21/HTTP';
20
21
    /**
22
     * $credit.
23
     *
24
     * @var float
25
     */
26
    public $credit = null;
27
28
    /**
29
     * __construct.
30
     *
31
     * @param string $userId
32
     * @param string $password
33
     * @param \Http\Client\HttpClient $httpClient
34
     * @param \Http\Message\MessageFactory $messageFactory
35
     */
36 2
    public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null)
37
    {
38 2
        $this->userId = $userId;
0 ignored issues
show
Bug introduced by
The property userId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39 2
        $this->password = $password;
0 ignored issues
show
Bug introduced by
The property password does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 2
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
0 ignored issues
show
Bug introduced by
The property httpClient does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41 2
        $this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
0 ignored issues
show
Bug introduced by
The property messageFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42 2
    }
43
44
    /**
45
     * setCredit.
46
     *
47
     * @param string $credit
48
     */
49 2
    protected function setCredit($credit)
50
    {
51 2
        $this->credit = (float) $credit;
52
53 2
        return $this;
54
    }
55
56
    /**
57
     * credit.
58
     *
59
     * @return float
60
     */
61 2
    public function credit()
62
    {
63 2
        if (is_null($this->credit) === false) {
64 1
            return $this->credit;
65
        }
66
67 1
        $response = $this->doRequest('getCredit.ashx', [
68 1
            'UID' => $this->userId,
69 1
            'PWD' => $this->password,
70
        ]);
71
72 1
        if ($this->isValidResponse($response) === false) {
73
            throw new DomainException($response);
74
        }
75
76 1
        return $this->setCredit($response)
77 1
            ->credit;
78
    }
79
80
    /**
81
     * send.
82
     *
83
     * @param array $params
84
     *
85
     * @return string
86
     */
87 1
    public function send($params)
88
    {
89 1
        $response = $this->doRequest('sendSMS.ashx', array_filter([
90 1
            'UID' => $this->userId,
91 1
            'PWD' => $this->password,
92 1
            'SB' => isset($params['subject']) ? $params['subject'] : null,
93 1
            'MSG' => $params['text'],
94 1
            'DEST' => $params['to'],
95 1
            'ST' => isset($params['ST']) ? Carbon::parse($params['ST'])->format('YmdHis') : null,
96
        ]));
97
98 1
        if ($this->isValidResponse($response) === false) {
99
            throw new DomainException($response, 500);
100
        }
101
102 1
        list($credit, $sended, $cost, $unsend, $batchId) = explode(',', $response);
103
104
        return [
105 1
            'credit' => $this->setCredit($credit)->credit,
106 1
            'sended' => (int) $sended,
107 1
            'cost' => (float) $cost,
108 1
            'unsend' => (int) $unsend,
109 1
            'batchId' => $batchId,
110
        ];
111
    }
112
113
    /**
114
     * isValidResponse.
115
     *
116
     * @param string $response
117
     *
118
     * @return bool
119
     */
120 2
    protected function isValidResponse($response)
121
    {
122 2
        return substr($response, 0, 1) !== '-';
123
    }
124
125
    /**
126
     * doRequest.
127
     *
128
     * @param string $uri
129
     * @param array $params
130
     *
131
     * @return string
132
     */
133 2
    protected function doRequest($uri, $params)
134
    {
135 2
        $request = $this->messageFactory->createRequest(
136 2
            'POST',
137 2
            rtrim($this->apiEndpoint, '/').'/'.$uri,
138 2
            ['Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'],
139
            http_build_query($params)
140
        );
141 2
        $response = $this->httpClient->sendRequest($request);
142
143 2
        return $response->getBody()->getContents();
144
    }
145
}
146