Gravatar::getRating()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Gravatar;
6
7
use Arcanedev\Gravatar\Contracts\Gravatar as GravatarContract;
8
use Arcanedev\Gravatar\Helpers\NumberChecker;
9
use Arcanedev\Html\Elements\Img;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class     Gravatar
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class Gravatar implements GravatarContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Traits
21
     | -----------------------------------------------------------------
22
     */
23
24 1
    use Concerns\HashEmail;
25
26
    /* -----------------------------------------------------------------
27
     |  Constants
28
     | -----------------------------------------------------------------
29
     */
30
31
    const BASE_URL   = 'http://www.gravatar.com/avatar/';
32
    const SECURE_URL = 'https://secure.gravatar.com/avatar/';
33
34
    /* -----------------------------------------------------------------
35
     |  Properties
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * The default image to use (gravatar-recognized type, url or false = default gravatar image)
41
     *
42
     * @var mixed
43
     */
44
    protected $defaultImage     = false;
45
46
    /**
47
     * The maximum rating to allow for the avatar.
48
     *
49
     * @var string
50
     */
51
    protected $rating           = 'g';
52
53
    /**
54
     * The avatar size.
55
     *
56
     * @var int
57
     */
58
    protected $size             = 80;
59
60
    /**
61
     * Should we use the secure (HTTPS) URL base ?
62
     *
63
     * @var bool
64
     */
65
    protected $secure           = false;
66
67
    /**
68
     * A temporary internal cache of the URL parameters.
69
     *
70
     * @var array|null
71
     */
72
    protected $cachedParams     = null;
73
74
    /**
75
     * Supported image defaults.
76
     *
77
     * @var array
78
     */
79
    private $supportedImages    = [
80
        '404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank',
81
    ];
82
83
    /**
84
     * Supported image ratings.
85
     *
86
     * @var array
87
     */
88
    private $supportedRatings   = [
89
        'g', 'pg', 'r', 'x'
90
    ];
91
92
    /* -----------------------------------------------------------------
93
     |  Constructor
94
     | -----------------------------------------------------------------
95
     */
96
97
    /**
98
     * Make a gravatar instance.
99
     *
100
     * @param  string  $default
101
     * @param  int     $size
102
     * @param  string  $rating
103
     */
104 76
    public function __construct($default = 'identicon', $size = 80, $rating = 'g')
105
    {
106 76
        $this->setDefaultImage($default);
107 76
        $this->setSize($size);
108 76
        $this->setRating($rating);
109 76
        $this->enableSecure();
110 76
    }
111
112
    /* -----------------------------------------------------------------
113
     |  Getters & Setters
114
     | -----------------------------------------------------------------
115
     */
116
117
    /**
118
     * Get the current default image setting.
119
     *
120
     * @return string|false
121
     */
122 36
    public function getDefaultImage()
123
    {
124 36
        return $this->defaultImage;
125
    }
126
127
    /**
128
     * Set the default image to use for avatars.
129
     *
130
     * @param  string|false  $image
131
     *
132
     * @return $this
133
     *
134
     * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException
135
     */
136 76
    public function setDefaultImage($image)
137
    {
138 76
        if ($image !== false) {
139 76
            $this->cachedParams = null;
140
141 76
            $image = in_array(strtolower($image), $this->supportedImages)
142 76
                ? strtolower($image)
143 76
                : $this->checkImageUrl($image);
144
        }
145
146 76
        $this->defaultImage = $image;
147
148 76
        return $this;
149
    }
150
151
    /**
152
     * Get the currently set avatar size.
153
     *
154
     * @return int
155
     */
156 40
    public function getSize()
157
    {
158 40
        return $this->size;
159
    }
160
161
    /**
162
     * Set the avatar size to use.
163
     *
164
     * @param  int  $size - The avatar size to use, must be less than 512 and greater than 0.
165
     *
166
     * @return $this
167
     *
168
     * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageSizeException
169
     */
170 76
    public function setSize($size)
171
    {
172 76
        $this->cachedParams = null;
173
174 76
        $this->checkSize($size);
175
176 76
        $this->size = $size;
177
178 76
        return $this;
179
    }
180
181
    /**
182
     * Get the current maximum allowed rating for avatars.
183
     *
184
     * @return string
185
     */
186 36
    public function getRating()
187
    {
188 36
        return $this->rating;
189
    }
190
191
    /**
192
     * Set the maximum allowed rating for avatars.
193
     *
194
     * @param  string  $rating
195
     *
196
     * @return $this
197
     *
198
     * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageRatingException
199
     */
200 76
    public function setRating($rating)
201
    {
202 76
        $this->cachedParams = null;
203
204 76
        $rating = strtolower($rating);
205
206 76
        if ( ! in_array($rating, $this->supportedRatings))
207 4
            throw Exceptions\InvalidImageRatingException::make($rating);
208
209 76
        $this->rating = $rating;
210
211 76
        return $this;
212
    }
213
214
    /**
215
     * Check if we are using the secure protocol for the image URLs.
216
     *
217
     * @return bool
218
     */
219 40
    public function isSecured(): bool
220
    {
221 40
        return $this->secure;
222
    }
223
224
    /* -----------------------------------------------------------------
225
     |  Main Methods
226
     | -----------------------------------------------------------------
227
     */
228
229
    /**
230
     * Get Gravatar image source.
231
     *
232
     * @param  string       $email
233
     * @param  int|null     $size
234
     * @param  string|null  $rating
235
     *
236
     * @return string
237
     */
238 12
    public function src($email, $size = null, $rating = null): string
239
    {
240 12
        if (is_null($size)) {
241 4
            $size = $this->getSize();
242
        }
243
244 12
        $size = max(1, min(512, $size));
245
246 12
        $this->setSize($size);
247
248 12
        if ( ! is_null($rating)) {
249 4
            $this->setRating($rating);
250
        }
251
252 12
        return $this->get($email);
253
    }
254
255
    /**
256
     * Get the avatar URL based on the provided email address.
257
     *
258
     * @param  string  $email
259
     * @param  bool    $hash
260
     *
261
     * @return string
262
     */
263 32
    public function get($email, $hash = true): string
264
    {
265 32
        $url  = $this->isSecured() ? static::SECURE_URL : static::BASE_URL;
266 32
        $url .= empty($email)
267 16
            ? str_repeat('0', 32)
268 26
            : ($hash ? static::hashEmail($email) : $email);
269
270 32
        $params = $this->getParams($email);
271
272 32
        return $url.'?'.http_build_query($params);
273
    }
274
275
    /**
276
     * Get Gravatar image tag.
277
     *
278
     * @param  string       $email
279
     * @param  string|null  $alt
280
     * @param  array        $attributes
281
     * @param  string|null  $rating
282
     *
283
     * @return \Arcanedev\Html\Elements\Img
284
     */
285 8
    public function image($email, $alt = null, array $attributes = [], $rating = null)
286
    {
287 8
        $dimensions = array_values(
288 8
            Arr::only($attributes, ['width', 'height'])
289
        );
290
291 8
        $size = count($dimensions)
292 4
            ? min(512, max($dimensions))
293 8
            : $this->getSize();
294
295 8
        return Img::make()
296 8
                  ->src($this->src($email, $size, $rating))
297 8
                  ->attributeIfNotNull($alt, 'alt', $alt)
298 8
                  ->attributes($attributes);
299
    }
300
301
    /**
302
     * Get profile's data.
303
     *
304
     * @param  string      $email
305
     * @param  mixed|null  $default
306
     *
307
     * @return array|mixed
308
     */
309 4
    public function profile($email, $default = null)
310
    {
311 4
        return (new Profile)->get($email, $default);
312
    }
313
314
    /**
315
     * Enable the use of the secure protocol for image URLs.
316
     *
317
     * @return $this
318
     */
319 76
    public function enableSecure()
320
    {
321 76
        $this->secure = true;
322
323 76
        return $this;
324
    }
325
326
    /**
327
     * Disable the use of the secure protocol for image URLs.
328
     *
329
     * @return $this
330
     */
331 4
    public function disableSecure()
332
    {
333 4
        $this->secure = false;
334
335 4
        return $this;
336
    }
337
338
    /**
339
     * Check if email has a gravatar.
340
     *
341
     * @param  string  $email
342
     *
343
     * @return bool
344
     */
345 4
    public function exists($email): bool
346
    {
347 4
        $this->setDefaultImage('404');
348
349 4
        $headers = get_headers($this->get($email), 1);
350
351 4
        return strpos($headers[0], '200') ? true : false;
352
    }
353
354
    /* -----------------------------------------------------------------
355
     |  Other Methods
356
     | -----------------------------------------------------------------
357
     */
358
359
    /**
360
     * Get params.
361
     *
362
     * @param  string  $email
363
     *
364
     * @return array
365
     */
366 32
    private function getParams(string $email): array
367
    {
368 32
        $params = $this->cachedParams;
369
370 32
        if (is_null($this->cachedParams)) {
371 32
            $params['s'] = $this->getSize();
372 32
            $params['r'] = $this->getRating();
373
374 32
            if ($this->getDefaultImage() !== false) {
375 28
                $params['d'] = $this->getDefaultImage();
376
            }
377
378 32
            $this->cachedParams = $params;
379
        }
380
381 32
        if (empty($email)) {
382 16
            $params['f'] = 'y';
383
        }
384
385 32
        return (array) $params;
386
    }
387
388
    /**
389
     * Check image url.
390
     *
391
     * @param  string  $image
392
     *
393
     * @return string
394
     *
395
     * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException
396
     */
397 12
    private function checkImageUrl(string $image): string
398
    {
399 12
        if ( ! filter_var($image, FILTER_VALIDATE_URL)) {
400 8
            throw Exceptions\InvalidImageUrlException::make($image);
401
        }
402
403 4
        return $image;
404
    }
405
406
    /**
407
     * Check the image size.
408
     *
409
     * @param  int  $size
410
     */
411 76
    private function checkSize(&$size): void
412
    {
413 76
        if ( ! NumberChecker::isIntValue($size)) {
414 4
            throw new Exceptions\InvalidImageSizeException(
415 4
                'Avatar size specified must be an integer.'
416
            );
417
        }
418
419 76
        $size = (int) $size;
420
421 76
        if ( ! NumberChecker::isIntBetween($size, 0, 512)) {
422 4
            throw new Exceptions\InvalidImageSizeException(
423 4
                'Avatar size must be within 0 pixels and 512 pixels.'
424
            );
425
        }
426 76
    }
427
}
428