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

Melipayamak::__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 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
            $sms = $this->client->sms();
57
            $response = ['status' => true, 'data' =>[]];
58
            foreach ($this->recipients as $recipient) {
59
                $response = $sms->send(
60
                    $recipient,
61
                    $this->settings->from,
62
                    $this->body,
63
                    $this->settings->flash
64
                );
65
            }
66
        } catch(\Exception $e) {
67
            $response['status'][$recipient] = false;
0 ignored issues
show
Bug introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $recipient seems to be defined by a foreach iteration on line 58. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
68
            $response['data'][$recipient] = $e->getMessage();
69
        } finally {
70
            if (empty($data)) {
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
71
                $response['data'][$recipient] = $this->getSmsResponse(
72
                    json_decode($response, JSON_UNESCAPED_UNICODE)
73
                );
74
            }
75
        }
76
77
        $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...
78
79
        return (object) $response;
80
    }
81
82
    /**
83
     * Get the MeliPayamak Response.
84
     *
85
     * @param $sms
86
     * @return object
87
     */
88
    protected function getSmsResponse($sms)
89
    {
90
        $attributes = [
91
            'recId',
92
        ];
93
94
        $res = [];
95
        foreach ($attributes as $attribute) {
96
            $res[$attribute] = $sms->$attribute;
97
        }
98
99
        return (object) $res;
100
    }
101
}
102