Response   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 92
ccs 29
cts 29
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 3 1
A getMessage() 0 3 1
A getMessageIds() 0 3 1
A getMessageId() 0 3 1
A initializeByString() 0 12 3
A __toString() 0 16 2
1
<?php
2
/*
3
 * Copyright 2015 Topface, LLC <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace TopfaceLibrary\SmsOnline\Bulk;
19
20
use TopfaceLibrary\SmsOnline\InitializationInterface;
21
22
/**
23
 * Response class
24
 * @author alxmsl
25
 */
26
final class Response implements InitializationInterface {
27
    /**
28
     * Response status codes
29
     */
30
    const CODE_OK                   = 0, // the request was proceed successfully
31
          CODE_INCORRECT_DATA       = -1, // incorrect input data
32
          CODE_AUTHENTICATION_ERROR = -2, // authentication error
33
          CODE_REJECTED             = -3, // request processing is rejected
34
          CODE_TECHNICAL_ERROR      = -4, // temporary technical error
35
          CODE_LIMIT_REACHED        = -5; // you have no more sms on your account
36
37
    /**
38
     * @var int response code
39
     */
40
    private $code = 0;
41
42
    /**
43
     * @var string response message
44
     */
45
    private $message = '';
46
47
    /**
48
     * @var array message identifiers for delivery report
49
     */
50
    private $messageIds = [];
51
52
    /**
53
     * @return int response code
54
     */
55 1
    public function getCode() {
56 1
        return $this->code;
57
    }
58
59
    /**
60
     * @return string message identifiers for delivery report
61
     */
62 1
    public function getMessage() {
63 1
        return $this->message;
64
    }
65
66
    /**
67
     * @return array message identifiers for delivery report
68
     */
69 1
    public function getMessageIds() {
70 1
        return $this->messageIds;
71
    }
72
73
    /**
74
     * Method to get delivery report message id for phone
75
     * @param string $phone phone number
76
     * @return string message identifier
77
     */
78 1
    public function getMessageId($phone) {
79 1
        return $this->messageIds[$phone];
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 1
    public static function initializeByString($string) {
86 1
        $Object            = simplexml_load_string($string);
87 1
        $Response          = new Response();
88 1
        $Response->code    = (int) $Object->code;
89 1
        $Response->message = (string) $Object->tech_message;
90 1
        if (isset($Object->msg_id)) {
91 1
            foreach ($Object->msg_id as $node) {
92 1
                $Response->messageIds[(string) $node['phone']] = (string) $node;
93 1
            }
94 1
        }
95 1
        return $Response;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 1
    public function __toString() {
102
        $format = <<<'EOD'
103
    code:    %s
104
    message: %s
105
    ids:
106
%s
107 1
EOD;
108 1
        $ids = '';
109 1
        foreach ($this->getMessageIds() as $phone => $messageId) {
110 1
            $ids .= sprintf("\t%s: %s\n", $phone, $messageId);
111 1
        }
112 1
        return sprintf($format
113 1
            , $this->getCode()
114 1
            , $this->getMessage()
115 1
            , $ids);
116
    }
117
}
118