Ssl::getValidationRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Sarahman\SmsService\Providers;
4
5
use Exception;
6
use Illuminate\Support\Facades\Log;
7
use Sarahman\SmsService\Response;
8
use SimpleXMLElement;
9
10
class Ssl extends BaseProvider
11
{
12
    public function getUrl()
13
    {
14
        return sprintf('%s/%s/server.php', parent::getUrl(), $this->config['sid']);
15
    }
16
17
    public function getUsername()
18
    {
19
        return $this->config['user'];
20
    }
21
22
    public function mapParams($recipient, $message, array $params = [])
23
    {
24
        if (!preg_match($this->recipientPattern, $recipient, $matches)) {
25
            return [];
26
        }
27
28
        $recipient = '880'.$matches[3];
29
        $clientRefId = !array_key_exists('id', $params) ? uniqid() : $params['id'];
30
31
        return [
32
            'sms' => [
33
                [
34
                    0 => $recipient,
35
                    1 => preg_replace(['/\'/', '/^\&/', '/(\.\s?)(\&)/', '/\&/'], [' ', 'And', '$1And', 'and'], $message),
36
                    2 => $clientRefId,
37
                ],
38
            ],
39
        ];
40
    }
41
42
    public function getValidationRules()
43
    {
44
        return [
45
            'user'    => 'required',
46
            'pass'    => 'required',
47
            'sid'     => 'required',
48
            'sms.0.0' => 'required|regex:/^8801[3456789]\d{8}$/',
49
            'sms.0.1' => 'required',
50
            'sms.0.2' => 'required',
51
        ];
52
    }
53
54
    public function parseResponse($response)
55
    {
56
        try {
57
            $xmlElement = new SimpleXMLElement($response);
58
59
            return new Response('SUCESSFULL' === strtoupper((string) $xmlElement->LOGIN), $response);
60
        } catch (Exception $exception) {
61
            Log::error($exception);
62
63
            return new Response(false, 'SSL did not respond!');
64
        }
65
    }
66
}
67