Insight::setOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/**
4
 * AistInsight (http://mateuszsitek.com/projects/aist-insight)
5
 *
6
 * @link      http://github.com/ma-si/aist-insight for the canonical source repository
7
 * @copyright Copyright (c) 2006-2016 Aist Internet Technologies (http://aist.pl) All rights reserved.
8
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
9
 */
10
11
namespace AistInsight\View\Helper;
12
13
use Zend\View\Helper\AbstractHtmlElement;
14
15
class Insight extends AbstractHtmlElement
16
{
17
    /**
18
     * URL to Insight service
19
     */
20
    const INSIGHT_URL = 'http://insight.sensiolabs.com/projects';
21
22
    /**
23
     * Secure URL to Insight service
24
     */
25
    const INSIGHT_URL_SECURE = 'https://insight.sensiolabs.com/projects';
26
27
    /**
28
     * Default value whether img should be wrapped into html <a> tag
29
     */
30
    const LINKED = true;
31
32
    /**
33
     * Insight size
34
     */
35
    const SIZE_BIG = 'big';
36
    const SIZE_SMALL = 'small';
37
    const SIZE_MINI = 'mini';
38
39
    /**
40
     * Default options
41
     *
42
     * @var array
43
     */
44
    const OPTIONS = [
45
        'badge_size' => self::SIZE_BIG,
46
        'secure' => self::INSIGHT_URL_SECURE,
47
        'linked' => self::LINKED,
48
    ];
49
50
    /**
51
     * Insight project key
52
     *
53
     * @var string
54
     */
55
    protected $projectKey;
56
57
    /**
58
     * Options
59
     *
60
     * @var array
61
     */
62
    protected $options = self::OPTIONS;
63
64
    /**
65
     * Attributes for HTML image tag
66
     *
67
     * @var array
68
     */
69
    protected $attribs = [];
70
71
    /**
72
     * Returns an image from Insight's service.
73
     * $options may include the following:
74
     * - 'size' size of img to return
75
     * - 'secure' bool load from the SSL or Non-SSL location
76
     * - 'linked' bool whether img should be wrapped into html a tag
77
     *
78
     * @param  string|null $projectKey Project key..
79
     * @param  null|array $options Options
80
     * @param null|array $attribs
81
     * @return $this
82
     */
83
    public function __invoke($projectKey = '', $options = [], $attribs = [])
84
    {
85
        $this->setProjectKey($projectKey);
86
87
        if (empty($options)) {
88
            $options = self::OPTIONS;
89
        }
90
        if (empty($attribs)) {
91
            $attribs = [];
92
        }
93
94
        $this->setOptions($options);
95
        $this->setAttribs($attribs);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Return badge html
102
     *
103
     * @return string
104
     */
105
    public function __toString()
106
    {
107
        return (true === $this->options['linked']) ? $this->getATag() : $this->getImgTag();
108
    }
109
110
    /**
111
     * Configure state
112
     *
113
     * @param  array $options
114
     * @return $this
115
     */
116
    public function setOptions(array $options)
117
    {
118
        foreach ($options as $key => $value) {
119
            $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
120
            if (method_exists($this, $method)) {
121
                $this->{$method}($value);
122
            }
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get project url
130
     *
131
     * @return string
132
     */
133
    protected function getProjectUrl()
134
    {
135
        $src = $this->getInsightUrl() . '/' . $this->getProjectKey();
136
137
        return $src;
138
    }
139
140
    /**
141
     * Get img url
142
     *
143
     * @return string
144
     */
145
    protected function getImgUrl()
146
    {
147
        $src = $this->getProjectUrl() . '/' . $this->getBadgeSize() . '.png';
148
149
        return $src;
150
    }
151
152
    /**
153
     * Get URL to Insight's service
154
     *
155
     * @return string URL
156
     */
157
    protected function getInsightUrl()
158
    {
159
        return ($this->getSecure() === false) ? self::INSIGHT_URL : self::INSIGHT_URL_SECURE;
160
    }
161
162
    /**
163
     * Return valid image tag
164
     *
165
     * @return string
166
     */
167
    public function getImgTag()
168
    {
169
        $this->setSrcAttribForImg();
170
        $html = '<img'
171
            . $this->htmlAttribs($this->getAttribs())
172
            . $this->getClosingBracket();
173
174
        return $html;
175
    }
176
177
    /**
178
     * Return valid a tag
179
     *
180
     * @return string
181
     */
182
    public function getATag()
183
    {
184
        $html = '<a'
185
            . $this->htmlAttribs(['href' => $this->getProjectUrl(), 'target' => '_blank'])
186
            . $this->getClosingBracket()
187
            . $this->getImgTag()
188
            . '</a>'
189
        ;
190
191
        return $html;
192
    }
193
194
    /**
195
     * Set attribs for image tag
196
     *
197
     * Warning! You shouldn't set src attrib for image tag.
198
     * This attrib is overwritten in protected method setSrcAttribForImg().
199
     * This method(_setSrcAttribForImg) is called in public method getImgTag().
200
     *
201
     * @param  array $attribs
202
     * @return $this
203
     */
204
    public function setAttribs(array $attribs)
205
    {
206
        $this->attribs = $attribs;
207
208
        return $this;
209
    }
210
211
    /**
212
     * Get attribs of image
213
     *
214
     * Warning!
215
     * If you set src attrib, you get it, but this value will be overwritten in
216
     * protected method setSrcAttribForImg(). And finally your get other src
217
     * value!
218
     *
219
     * @return array
220
     */
221
    public function getAttribs()
222
    {
223
        return $this->attribs;
224
    }
225
226
    /**
227
     * Set project key
228
     *
229
     * @param  string $projectKey
230
     * @return $this
231
     */
232
    public function setProjectKey($projectKey)
233
    {
234
        $this->projectKey = $projectKey;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get project key
241
     *
242
     * @return string
243
     */
244
    public function getProjectKey()
245
    {
246
        return $this->projectKey;
247
    }
248
249
    /**
250
     * Set linked
251
     *
252
     * @param  string $linked Define whether img should be wrapped into html a tag
253
     * @return $this
254
     */
255
    public function setLinked($linked)
256
    {
257
        $this->options['linked'] = $linked;
258
259
        return $this;
260
    }
261
262
    /**
263
     * Set badge size
264
     *
265
     * @param  string $size Size of img must be one of the: big, small, mini
266
     * @return $this
267
     */
268
    public function setBadgeSize($size)
269
    {
270
        $this->options['badge_size'] = $size;
271
272
        return $this;
273
    }
274
275
    /**
276
     * Get badge size
277
     *
278
     * @return string The badge size
279
     */
280
    public function getBadgeSize()
281
    {
282
        return $this->options['badge_size'];
283
    }
284
285
    /**
286
     * Load from an SSL or No-SSL location?
287
     *
288
     * @param  bool $flag
289
     * @return $this
290
     */
291
    public function setSecure($flag)
292
    {
293
        $this->options['secure'] = ($flag === null) ? null : (bool) $flag;
294
295
        return $this;
296
    }
297
298
    /**
299
     * Get an SSL or a No-SSL location
300
     *
301
     * @return bool
302
     */
303
    public function getSecure()
304
    {
305
        if ($this->options['secure'] === null) {
306
            return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
307
        }
308
309
        return $this->options['secure'];
310
    }
311
312
    /**
313
     * Set src attrib for image
314
     *
315
     * You shouldn't set an own url value!
316
     * It sets value, uses protected method getImgUrl.
317
     *
318
     * If already exists, it will be overwritten.
319
     *
320
     * @return void
321
     */
322
    protected function setSrcAttribForImg()
323
    {
324
        $attribs = $this->getAttribs();
325
        $attribs['src'] = $this->getImgUrl();
326
        $this->setAttribs($attribs);
327
    }
328
}
329