BaseMessage::getResponseMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Mremi\Flowdock library.
5
 *
6
 * (c) Rémi Marseille <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mremi\Flowdock\Api\Push;
13
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * Base push message class
18
 *
19
 * @author Rémi Marseille <[email protected]>
20
 */
21
abstract class BaseMessage implements BaseMessageInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $content;
27
28
    /**
29
     * @var array
30
     */
31
    protected $tags = array();
32
33
    /**
34
     * @var ResponseInterface
35
     */
36
    protected $response;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function create()
42
    {
43
        return new static;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function setContent($content)
50
    {
51
        $this->content = $content;
52
53
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getContent()
60
    {
61
        return $this->content;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function clearTags()
68
    {
69
        $this->tags = array();
70
71
        return $this;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function setTags(array $tags)
78
    {
79
        $this->clearTags();
80
81
        foreach ($tags as $tag) {
82
            $this->addTag($tag);
83
        }
84
85
        return $this;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function addTag($tag)
92
    {
93
        $this->tags[] = $tag;
94
95
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getTags()
102
    {
103
        return $this->tags;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function setResponse(ResponseInterface $response)
110
    {
111
        $this->response = $response;
112
113
        return $this;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getResponse()
120
    {
121
        return $this->response;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getData()
128
    {
129
        $array = $this->toArray();
130
131
        // to be consistent with the Flowdock API
132
        unset($array['response']);
133
134
        return $array;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function toArray()
141
    {
142
        $array = get_object_vars($this);
143
144
        $keys = array_map(array($this, 'underscore'), array_keys($array));
145
146
        return array_combine($keys, array_values($array));
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getResponseBody()
153
    {
154
        if (null === $this->response) {
155
            return array();
156
        }
157
158
        $body = json_decode($this->response->getBody(true), true);
0 ignored issues
show
Unused Code introduced by
The call to ResponseInterface::getBody() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
159
160
        return is_array($body) ? $body : array();
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getResponseMessage()
167
    {
168
        $body = $this->getResponseBody();
169
170
        return array_key_exists('message', $body) ? $body['message'] : null;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function getResponseErrors()
177
    {
178
        $body = $this->getResponseBody();
179
180
        return array_key_exists('errors', $body) ? $body['errors'] : array();
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function hasResponseErrors()
187
    {
188
        return count($this->getResponseErrors()) > 0;
189
    }
190
191
    /**
192
     * A string to underscore.
193
     *
194
     * This has been copied from the DependencyInjection Symfony component.
195
     *
196
     * @param string $string The string to underscore
197
     *
198
     * @return string The underscored string
199
     */
200
    private function underscore($string)
201
    {
202
        return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($string, '_', '.')));
203
    }
204
}
205