Completed
Pull Request — master (#13)
by
unknown
01:38
created

Kavenegar::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 10
Ratio 47.62 %

Importance

Changes 0
Metric Value
dl 10
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
namespace Tzsk\Sms\Drivers;
3
4
use Kavenegar\KavenegarApi;
5
use Tzsk\Sms\Abstracts\Driver;
6
7
class Kavenegar extends Driver
8
{
9
    /**
10
     * Kavenegar Settings.
11
     *
12
     * @var null|object
13
     */
14
    protected $settings = null;
15
16
    /**
17
     * Kavenegar Client.
18
     *
19
     * @var null|KavenegarApi
20
     */
21
    protected $client = null;
22
23
    /**
24
     * Construct the class with the relevant settings.
25
     *
26
     * SendSmsInterface constructor.
27
     * @param $settings object
28
     */
29
    public function __construct($settings)
30
    {
31
        $this->settings = (object) $settings;
32
        $this->client = new KavenegarApi($this->settings->apiKey);
33
    }
34
35
    /**
36
     * Send text message and return response.
37
     *
38
     * @return object
39
     */
40
    public function send()
41
    {
42
        try {
43
            $response = ['status' => true, 'data' =>[]];
44 View Code Duplication
            foreach ($this->recipients as $recipient) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
                $sms = $this->client->Send(
46
                    $this->settings->from,
47
                    $recipient,
48
                    $this->body
49
                );
50
                $response['data'][$recipient] = $this->getSmsResponse(
51
                    json_decode($sms, true)
52
                );
53
            }
54
        } catch (\Exception $e) {
55
            $response['status'][$recipient] = false;
56
            $response['data'][$recipient] = $e->getMessage();
57
        }
58
59
        return (object) $response;
60
    }
61
62
    /**
63
     * Get the Kavenegar Response.
64
     *
65
     * @param $sms
66
     * @return object
67
     */
68
    protected function getSmsResponse($sms)
69
    {
70
        $attributes = [
71
            'messageid', 'message', 'status', 'statustext',
72
            'sender', 'receptor', 'date', 'cost',
73
        ];
74
75
        $res = [];
76
        foreach ($attributes as $attribute) {
77
            $res[$attribute] = $sms->$attribute;
78
        }
79
80
        return (object) $res;
81
    }
82
}
83