Completed
Push — master ( 4c9412...ee119a )
by Hamoud
03:20 queued 01:51
created

MobilyWsApi::send()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 5
nop 1
crap 3.0123
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
    
12
    /**  @var string mobily.ws endpoint for sending sms */
13
    protected $endpoint = 'msgSend.php';
14
    
15
    /** @var MobilyWsConfig */
16
    private $config;
17
18
    /** @var HttpClient */
19
    private $http;
20
21
    /**
22
     * Create a new MobilyWs channel instance.
23
     *
24
     * @param MobilyWsConfig $config
25
     * @param HttpClient     $http
26
     */
27 9
    public function __construct(MobilyWsConfig $config, HttpClient $http)
28
    {
29 9
        $this->http = $http;
30 9
        $this->config = $config;
31 9
    }
32
    
33
    /**
34
     * Send request with string message
35
     *
36
     * @param $params
37
     *
38
     * @return array
39
     */
40 2
    public function sendString($params)
41
    {
42 1
        $payload = $this->preparePayload($params);
43 2
        return $this->send($payload);
44 1
    }
45
    
46
    /**
47
     * Send request with MobilyWsMessage instance
48
     *
49
     * @param MobilyWsMessage $message
50
     *
51
     * @param                 $number
52
     *
53
     * @return array
54
     * @internal param $params
55
     */
56 5
    public function sendMessage(MobilyWsMessage $message, $number)
57
    {
58
        $params = [
59 5
            'msg' => $message->text,
60 5
            'numbers' => $number,
61 5
            'dateSend' => $message->dateSend(),
62 5
            'timeSend' => $message->timeSend(),
63 5
        ];
64
        
65 5
        $payload = $this->preparePayload($params);
66 5
        return $this->send($payload);
67
    }
68
    
69
    /**
70
     * Send request to mobily.ws
71
     *
72
     * @param array $payload
73
     *
74
     * @return array
75
     * @throws \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification
76
     * @internal param array $params
77
     *
78
     */
79 8
    public function send(array $payload)
80
    {
81
        try {
82 8
            $response = $this->http->post($this->endpoint, $payload);
83
84 7
            if ($response->getStatusCode() == 200) {
85
                return [
86 7
                    'code' => $code = (string) $response->getBody(),
87 7
                    'message' => $this->msgSendResponse($code),
88 7
                ];
89
            }
90
            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...
91 1
        } catch (RequestException $exception) {
92 1
            throw CouldNotSendNotification::couldNotSendRequestToMobilyWs($exception);
93
        }
94
    }
95
96
    /**
97
     * Prepare payload for http request.
98
     *
99
     * @param $params
100
     *
101
     * @return array
102
     */
103 6
    protected function preparePayload($params)
104
    {
105 6
        $form = array_merge([
106 6
            '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...
107 6
            '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...
108 6
            '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...
109 6
        ], $params, $this->config->getCredentials());
110
111 6
        return array_merge(
112 6
            ['form_params' => $form],
113 6
            $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...
114 6
        );
115
    }
116
117
    /**
118
     * Parse the response body from mobily.ws.
119
     *
120
     * @param $code
121
     *
122
     * @return string
123
     */
124 7
    protected function msgSendResponse($code)
125
    {
126 7
        $arraySendMsg = [];
127 7
        $arraySendMsg[0] = 'لم يتم الاتصال بالخادم';
128 7
        $arraySendMsg[1] = 'تمت عملية الإرسال بنجاح';
129 7
        $arraySendMsg[2] = 'رصيدك 0 , الرجاء إعادة التعبئة حتى تتمكن من إرسال الرسائل';
130 7
        $arraySendMsg[3] = 'رصيدك غير كافي لإتمام عملية الإرسال';
131 7
        $arraySendMsg[4] = 'رقم الجوال (إسم المستخدم) غير صحيح';
132 7
        $arraySendMsg[5] = 'كلمة المرور الخاصة بالحساب غير صحيحة';
133 7
        $arraySendMsg[6] = 'صفحة الانترنت غير فعالة , حاول الارسال من جديد';
134 7
        $arraySendMsg[7] = 'نظام المدارس غير فعال';
135 7
        $arraySendMsg[8] = 'تكرار رمز المدرسة لنفس المستخدم';
136 7
        $arraySendMsg[9] = 'انتهاء الفترة التجريبية';
137 7
        $arraySendMsg[10] = 'عدد الارقام لا يساوي عدد الرسائل';
138 7
        $arraySendMsg[11] = 'اشتراكك لا يتيح لك ارسال رسائل لهذه المدرسة. يجب عليك تفعيل الاشتراك لهذه المدرسة';
139 7
        $arraySendMsg[12] = 'إصدار البوابة غير صحيح';
140 7
        $arraySendMsg[13] = 'الرقم المرسل به غير مفعل أو لا يوجد الرمز BS في نهاية الرسالة';
141 7
        $arraySendMsg[14] = 'غير مصرح لك بالإرسال بإستخدام هذا المرسل';
142 7
        $arraySendMsg[15] = 'الأرقام المرسل لها غير موجوده أو غير صحيحه';
143 7
        $arraySendMsg[16] = 'إسم المرسل فارغ، أو غير صحيح';
144 7
        $arraySendMsg[17] = 'نص الرسالة غير متوفر أو غير مشفر بشكل صحيح';
145 7
        $arraySendMsg[18] = 'تم ايقاف الارسال من المزود';
146 7
        $arraySendMsg[19] = 'لم يتم العثور على مفتاح نوع التطبيق';
147
148 7
        if (array_key_exists($code, $arraySendMsg)) {
149 7
            return $arraySendMsg[$code];
150
        }
151
        $message = "نتيجة العملية غير معرفه، الرجاء المحاول مجددا\n";
152
        $message .= 'الكود المرسل من الموقع: ';
153
        $message .= "{$code}";
154
155
        return $message;
156
    }
157
}
158