FormDriver::doRealRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Pavel
5
 * Date: 2015-05-12
6
 * Time: 22:43
7
 */
8
9
namespace ScayTrase\WebSMS\Driver;
10
11
use Buzz\Client\FileGetContents;
12
use Buzz\Message\Form\FormRequest;
13
use Buzz\Message\Request;
14
use Buzz\Message\Response;
15
use ScayTrase\WebSMS\Exception\UnexpectedResponseException;
16
17
class FormDriver implements DriverInterface
18
{
19
    const HTTP_AccessPoint_Send    = 'http://cab.websms.ru/http_in5.asp';
20
    const HTTP_AccessPoint_Balance = 'http://cab.websms.ru/http_credit.asp';
21
22
    const Error_Message = 'error_num';
23
    const Error_Code    = 'error_code';
24
25
    const Section_Common  = 'Common';
26
    const Section_Summary = 'Summary';
27
28
    public function doSendRequest(array $options)
29
    {
30
        $data = $this->doRealRequest(self::HTTP_AccessPoint_Send, $options);
31
32
        $status = parse_ini_string($data, true);
33
34
        if (!isset($status[self::Section_Common][self::Error_Message])) {
35
            throw new UnexpectedResponseException('Response either does not contain common section or error message');
36
        }
37
38
        $normalized_status = array();
39
        $normalized_status = array_merge_recursive($normalized_status, $status[self::Section_Common]);
40
        if (isset($status[self::Section_Summary])) {
41
            $normalized_status = array_merge_recursive($normalized_status, $status[self::Section_Summary]);
42
        }
43
44
        foreach ($status as $key => $section) {
45
            if (is_int($key)) {
46
                $normalized_status['sms'][] = $section;
47
            }
48
        }
49
50
        $normalized_status[self::NORMALIZED_MESSAGE] = $status[self::Section_Common][self::Error_Message];
51
        $normalized_status[self::NORMALIZED_CODE]    =
52
            isset($status[self::Section_Common][self::Error_Code]) ?
53
                $status[self::Section_Common][self::Error_Code] :
54
                self::STATUS_OK;
55
56
        return $normalized_status;
57
    }
58
59
    public function doBalanceRequest(array $options)
60
    {
61
        $data = $this->doRealRequest(self::HTTP_AccessPoint_Balance, $options);
62
63
        return $data;
64
    }
65
66
    private function doRealRequest($url, array $options)
67
    {
68
        $request = new FormRequest(Request::METHOD_POST);
69
        $request->fromUrl($url);
70
        $request->setFields($options);
71
72
        $response = new Response();
73
        $client   = new FileGetContents();
74
75
        $client->send($request, $response);
0 ignored issues
show
Deprecated Code introduced by
The method Buzz\Client\FileGetContents::send() has been deprecated with message: Will be removed in 1.0. Use sendRequest instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76
77
        return $response->getContent();
78
    }
79
}
80