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

Melipayamak::getSmsResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Tzsk\Sms\Drivers;
3
4
use Melipayamak\MelipayamakApi;
5
use Tzsk\Sms\Abstracts\Driver;
6
7
class Melipayamak extends Driver
8
{
9
    /**
10
     * Melipayamak Settings.
11
     *
12
     * @var null|object
13
     */
14
    protected $settings = null;
15
16
    /**
17
     * Melipayamak 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 MelipayamakApi($this->settings->username, $this->settings->password);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Melipayamak\Melipay...is->settings->password) of type object<Melipayamak\MelipayamakApi> 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
     * Determine if the sms must be a flash message or not.
37
     *
38
     * @param bool $flash
39
     * @return $this
40
     */
41
    public function asFlash($flash=true)
42
    {
43
        $this->settings->flash = $flash;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Send text message and return response.
50
     *
51
     * @return object
52
     */
53
    public function send()
54
    {
55
        try {
56
            $response = ['status' => true, 'data' =>[]];
57 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...
58
                $sms = $this->client->sms()->send(
59
                    $recipient,
60
                    $this->settings->from,
61
                    $this->body,
62
                    $this->settings->flash
63
                );
64
                $response['data'][$recipient] = $this->getSmsResponse(
65
                    json_decode($sms, true)
66
                );
67
            }
68
        } catch (\Exception $e) {
69
            $response['status'][$recipient] = false;
70
            $response['data'][$recipient] = $e->getMessage();
71
        }
72
73
        $this->flash(false);
0 ignored issues
show
Bug introduced by
The method flash() does not seem to exist on object<Tzsk\Sms\Drivers\Melipayamak>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
75
        return (object) $response;
76
    }
77
78
    /**
79
     * Get the Melipayamak Response.
80
     *
81
     * @param $sms
82
     * @return object
83
     */
84
    protected function getSmsResponse($sms)
85
    {
86
        $attributes = [
87
            'recId',
88
        ];
89
90
        $res = [];
91
        foreach ($attributes as $attribute) {
92
            $res[$attribute] = $sms->$attribute;
93
        }
94
95
        return (object) $res;
96
    }
97
}
98