Completed
Push — master ( c675f1...96be6f )
by Hamoud
06:16 queued 10s
created

MobilyWsApi::sendMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
3
namespace NotificationChannels\MobilyWs;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\RequestException;
7
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification;
8
9
class MobilyWsApi
10
{
11
    /** @var MobilyWsConfig */
12
    private $config;
13
14
    /** @var HttpClient */
15
    private $http;
16
17
    /**
18
     * Create a new MobilyWs channel instance.
19
     *
20
     * @param MobilyWsConfig $config
21
     * @param HttpClient     $http
22
     */
23 6
    public function __construct(MobilyWsConfig $config, HttpClient $http)
24
    {
25 6
        $this->http = $http;
26 6
        $this->config = $config;
27 6
    }
28
    
29
    /**
30
     * Send request with string message
31
     *
32
     * @param $params
33
     *
34
     * @return array
35
     */
36 1
    public function sendString($params)
37
    {
38 1
        $payload = $this->preparePayload($params);
39 1
        return $this->send($payload);
40
    }
41
    
42
    /**
43
     * Send request with MobilyWsMessage instance
44
     *
45
     * @param MobilyWsMessage $message
46
     *
47
     * @param                 $number
48
     *
49
     * @return array
50
     * @internal param $params
51
     */
52 2
    public function sendMessage(MobilyWsMessage $message, $number)
53
    {
54
        $params = [
55 2
            'msg' => $message->text,
56 2
            'numbers' => $number,
57 2
            'dateSend' => $message->dateSend(),
58 2
            'timeSend' => $message->timeSend(),
59 2
        ];
60
        
61 2
        $payload = $this->preparePayload($params);
62 2
        return $this->send($payload);
63
    }
64
    
65
    /**
66
     * Send request to mobily.ws
67
     *
68
     * @param array $payload
69
     *
70
     * @return array
71
     * @throws \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification
72
     * @internal param array $params
73
     *
74
     */
75 5
    public function send(array $payload)
76
    {
77 5
        $endpoint = 'msgSend.php';
78
        try {
79 5
            $response = $this->http->post($endpoint, $payload);
80
81 4
            if ($response->getStatusCode() == 200) {
82
                return [
83 4
                    'code' => $code = (string) $response->getBody(),
84 4
                    'message' => $this->msgSendResponse($code),
85 4
                ];
86
            }
87
            throw CouldNotSendNotification::someErrorWhenSendingSms($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
88 1
        } catch (RequestException $exception) {
89 1
            throw CouldNotSendNotification::couldNotSendRequestToMobilyWs($exception);
90
        }
91
    }
92
93
    /**
94
     * Prepare payload for http request.
95
     *
96
     * @param $params
97
     *
98
     * @return array
99
     */
100 3
    protected function preparePayload($params)
101
    {
102 3
        $form = array_merge([
103 3
            'mobile' => $this->config->mobile,
0 ignored issues
show
Documentation introduced by
The property mobile does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104 3
            'password' => $this->config->password,
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105 3
            'applicationType' => $this->config->applicationType,
0 ignored issues
show
Documentation introduced by
The property applicationType does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106 3
            'lang' => $this->config->lang,
0 ignored issues
show
Documentation introduced by
The property lang does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107 3
            'sender' => $this->config->sender,
0 ignored issues
show
Documentation introduced by
The property sender does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108 3
        ], $params);
109
110 3
        return array_merge(
111 3
            ['form_params' => $form],
112 3
            $this->config->request
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113 3
        );
114
    }
115
116
    /**
117
     * Parse the response body from mobily.ws.
118
     *
119
     * @param $code
120
     *
121
     * @return string
122
     */
123 4
    protected function msgSendResponse($code)
124
    {
125 4
        $arraySendMsg = [];
126 4
        $arraySendMsg[0] = 'لم يتم الاتصال بالخادم';
127 4
        $arraySendMsg[1] = 'تمت عملية الإرسال بنجاح';
128 4
        $arraySendMsg[2] = 'رصيدك 0 , الرجاء إعادة التعبئة حتى تتمكن من إرسال الرسائل';
129 4
        $arraySendMsg[3] = 'رصيدك غير كافي لإتمام عملية الإرسال';
130 4
        $arraySendMsg[4] = 'رقم الجوال (إسم المستخدم) غير صحيح';
131 4
        $arraySendMsg[5] = 'كلمة المرور الخاصة بالحساب غير صحيحة';
132 4
        $arraySendMsg[6] = 'صفحة الانترنت غير فعالة , حاول الارسال من جديد';
133 4
        $arraySendMsg[7] = 'نظام المدارس غير فعال';
134 4
        $arraySendMsg[8] = 'تكرار رمز المدرسة لنفس المستخدم';
135 4
        $arraySendMsg[9] = 'انتهاء الفترة التجريبية';
136 4
        $arraySendMsg[10] = 'عدد الارقام لا يساوي عدد الرسائل';
137 4
        $arraySendMsg[11] = 'اشتراكك لا يتيح لك ارسال رسائل لهذه المدرسة. يجب عليك تفعيل الاشتراك لهذه المدرسة';
138 4
        $arraySendMsg[12] = 'إصدار البوابة غير صحيح';
139 4
        $arraySendMsg[13] = 'الرقم المرسل به غير مفعل أو لا يوجد الرمز BS في نهاية الرسالة';
140 4
        $arraySendMsg[14] = 'غير مصرح لك بالإرسال بإستخدام هذا المرسل';
141 4
        $arraySendMsg[15] = 'الأرقام المرسل لها غير موجوده أو غير صحيحه';
142 4
        $arraySendMsg[16] = 'إسم المرسل فارغ، أو غير صحيح';
143 4
        $arraySendMsg[17] = 'نص الرسالة غير متوفر أو غير مشفر بشكل صحيح';
144 4
        $arraySendMsg[18] = 'تم ايقاف الارسال من المزود';
145 4
        $arraySendMsg[19] = 'لم يتم العثور على مفتاح نوع التطبيق';
146
147 4
        if (array_key_exists($code, $arraySendMsg)) {
148 4
            return $arraySendMsg[$code];
149
        }
150
        $message = "نتيجة العملية غير معرفه، الرجاء المحاول مجددا\n";
151
        $message .= 'الكود المرسل من الموقع: ';
152
        $message .= "{$code}";
153
154
        return $message;
155
    }
156
}
157