Image   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 41
c 7
b 0
f 0
dl 0
loc 119
ccs 43
cts 43
cp 1
rs 10
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 24 5
A replaceHosts() 0 14 5
A detectFit() 0 9 3
A detectFormat() 0 13 5
A detectProgressive() 0 11 3
1
<?php
2
3
namespace Distilleries\Contentful\Helpers;
4
5
use Illuminate\Support\Str;
6
7
class Image
8
{
9
    /**
10
     * Return image URL to serve based on given parameters.
11
     *
12
     * @param  string  $url
13
     * @param  integer  $width
14
     * @param  integer  $height
15
     * @param  string  $format
16
     * @param  integer  $quality
17
     * @param  boolean|null  $useProgressive
18
     * @param  string  $fit
19
     * @return string
20
     */
21 18
    public static function url(string $url, int $width = 0, int $height = 0, $format = '', int $quality = 0, ?bool $useProgressive = null, $fit = ''): string
22
    {
23 18
        if (empty($url)) {
24 2
            return '';
25
        }
26
27 16
        $imageUrl = '';
28
29 16
        $format = static::detectFormat($format);
30
31 16
        collect([
32 16
            'w' => $width,
33 16
            'h' => $height,
34 16
            'q' => !empty($quality) ? $quality : config('contentful.image.default_quality'),
35 16
            'fm' => config('contentful.image.use_webp') ? $format : null,
36 16
            'fl' => static::detectProgressive($format, $useProgressive) ? 'progressive' : null,
37 16
            'fit' => static::detectFit($fit),
38
        ])->filter(function ($value) {
39 16
            return !empty($value);
40
        })->each(function ($value, $key) use (& $imageUrl) {
41 16
            $imageUrl .= $key . '=' . $value . '&';
42 16
        });
43
44 16
        return static::replaceHosts($url, $imageUrl);
45
    }
46
47
    /**
48
     * Replace assets hosts by configured ones.
49
     *
50
     * @param null|string  $url
51
     * @param string  $imageUrl
52
     * @return string
53
     */
54 16
    protected static function replaceHosts(?string $url, string $imageUrl): string
55
    {
56 16
        if (Str::contains($url, '?')) {
57 6
            $url = explode('?', $url);
58 6
            $url = $url[0];
59
        }
60
61 16
        $searchHosts = config('contentful.image.search_hosts');
62 16
        $replaceHost = config('contentful.image.replace_host');
63 16
        if (! empty($searchHosts) && ! empty($replaceHost)) {
64 16
            $url = str_replace(explode(',', $searchHosts), $replaceHost, $url);
65
        }
66
67 16
        return ! empty($url) ? $url . '?' . trim($imageUrl, '&') : '';
68
    }
69
70
    /**
71
     * Auto-detect image format to serve (based on browser capability).
72
     *
73
     * @param  string  $format
74
     * @return string|null
75
     */
76 16
    protected static function detectFormat(string $format = ''): ?string
77
    {
78
        /** @var \Jenssegers\Agent\Agent $agent */
79 16
        $agent = app('agent');
80
81 16
        if (empty($format)) {
82 16
            $browser = Str::lower($agent->browser());
83 16
            if (($browser === 'chrome') && !$agent->isMobile()) {
84 16
                $format = config('contentful.image.use_webp') ? 'webp' : null;
85
            }
86
        }
87
88 16
        return $format;
89
    }
90
91
    /**
92
     * Detect if fit can be used.
93
     *
94
     * @param string|null  $fit
95
     * @return string|null
96
     */
97 16
    protected static function detectFit(?string $fit = null): ?string
98
    {
99 16
        if (empty($fit)) {
100 14
            $fit = 'fill';
101
        } else {
102 2
            $fit = ($fit !== 'default') ? $fit : null;
103
        }
104
105 16
        return $fit;
106
    }
107
108
    /**
109
     * Detect if progressive image can be used.
110
     *
111
     * @param null|string  $format
112
     * @param bool|null  $useProgressive
113
     * @return bool
114
     */
115 16
    protected static function detectProgressive(?string $format = '', ?bool $useProgressive = null): bool
116
    {
117 16
        if ($format === 'webp') {
118 4
            $useProgressive = false;
119
        }
120
121 16
        if ($useProgressive === null) {
122 10
            $useProgressive = ! empty(config('contentful.image.use_progressive'));
123
        }
124
125 16
        return $useProgressive;
126
    }
127
}
128