Passed
Push — master ( 8bf068...43e885 )
by Paul
08:48
created

Avatar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
eloc 29
c 1
b 1
f 0
dl 0
loc 76
ccs 10
cts 30
cp 0.3333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 6 2
A generate() 0 4 1
A fallback() 0 7 2
A size() 0 7 2
A img() 0 17 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Modules\Html\Builder;
8
use GeminiLabs\SiteReviews\Review;
9
10
class Avatar
11
{
12
    const FALLBACK = 'https://gravatar.com/avatar/?d=mm&s=128';
13
    const FALLBACK_SIZE = 40;
14
15
    /**
16
     * @param int $size
17
     * @return string
18
     */
19
    public function fallback($size = null)
20
    {
21
        $fallbackUrl = static::FALLBACK;
22
        if ($size = $this->size($size)) {
23
            $fallbackUrl = add_query_arg('s', $size, remove_query_arg('s', $fallbackUrl));
24
        }
25
        return glsr()->filterString('avatar/fallback', $fallbackUrl, $size);
26
    }
27
28
    /**
29
     * @param \WP_User|int|string $userField
30
     * @param int $size
31
     * @return string
32
     */
33 15
    public function generate($userField, $size = null)
34
    {
35 15
        $size = $this->size($size);
36 15
        return $this->url(get_avatar_url($userField, ['size' => $size]), $size);
37
    }
38
39
    /**
40
     * @param int $size
41
     * @return string
42
     */
43
    public function img(Review $review, $size = null)
44
    {
45
        $size = $this->size($size);
46
        $src = $review->get('avatar');
47
        $attributes = [
48
            'alt' => sprintf(__('Avatar for %s', 'site-reviews'), $review->author()),
49
            'height' => $size * 2,
50
            'loading' => 'lazy',
51
            'src' => $this->url($src, $size * 2),
52
            'style' => sprintf('width:%1$spx; height:%1$spx;', $size),
53
            'width' => $size * 2,
54
        ];
55
        if (glsr()->isAdmin()) {
56
            $attributes['data-fallback'] = $this->fallback($size);
57
        }
58
        $attributes = glsr()->filterArray('avatar/attributes', $attributes, $review);
59
        return glsr(Builder::class)->img($attributes);
60
    }
61
62
    /**
63
     * @param string|false $avatarUrl
64
     * @param int $size
65
     * @return string
66
     */
67 15
    public function url($avatarUrl, $size = null)
68
    {
69 15
        if (filter_var($avatarUrl, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
70 15
            return $avatarUrl;
71
        }
72
        return $this->fallback($size);
73
    }
74
75
    /**
76
     * @param int $size
77
     * @return int
78
     */
79 15
    protected function size($size = null)
80
    {
81 15
        if ($size = Cast::toInt($size)) {
82
            return $size;
83
        }
84 15
        $size = glsr_get_option('reviews.avatars_size', static::FALLBACK_SIZE, 'int');
85 15
        return Helper::ifEmpty($size, static::FALLBACK_SIZE, $strict = true);
86
    }
87
}
88