Completed
Pull Request — master (#13)
by
unknown
02:29
created

Kavenegar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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|Client
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Kavenegar\Kavenegar...this->settings->apiKey) of type object<Kavenegar\KavenegarApi> is incompatible with the declared type null|object<Tzsk\Sms\Drivers\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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