Completed
Push — master ( f8e5cb...66cdb5 )
by Maxime
02:52
created

Image   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
C url() 0 48 13
A detectFormat() 0 14 5
1
<?php
2
3
namespace Distilleries\Contentful\Helpers;
4
5
class Image
6
{
7
    /**
8
     * Return image URL to serve based on given parameters.
9
     *
10
     * @param  string  $url
11
     * @param  integer  $width
12
     * @param  integer  $height
13
     * @param  string  $format
14
     * @param  integer  $quality
15
     * @param  boolean|null  $useProgressive
16
     * @param  string  $fit
17
     * @return string
18
     */
19
    public static function url(string $url, int $width = 0, int $height = 0, $format = '', int $quality = 0, ?bool $useProgressive = null, $fit = ''): string
20
    {
21
        if (empty($url)) {
22
            return '';
23
        }
24
25
        $imageUrl = '';
26
27
        $format = static::detectFormat($format);
28
        if ($format === 'webp') {
29
            $useProgressive = false;
30
        }
31
        if ($useProgressive === null) {
32
            $useProgressive = ! empty(config('contentful.image.use_progressive'));
33
        }
34
35
        if (empty($fit)) {
36
            $fit = 'fill';
37
        } else {
38
            $fit = ($fit !== 'default') ? $fit : null;
39
        }
40
41
        collect([
42
            'w' => $width,
43
            'h' => $height,
44
            'q' => ! empty($quality) ? $quality : config('contentful.image.default_quality'),
45
            'fm' => config('contentful.image.use_webp') ? $format : null,
46
            'fl' => $useProgressive ? 'progressive' : null,
47
            'fit' => $fit,
48
        ])->filter(function ($value) {
49
            return ! empty($value);
50
        })->each(function ($value, $key) use (& $imageUrl) {
51
            $imageUrl .= $key . '=' . $value . '&';
52
        });
53
54
        if (str_contains($url, '?')) {
55
            $url = explode('?', $url);
56
            $url = $url[0];
57
        }
58
59
        $searchHosts = config('contentful.image.search_hosts');
60
        $replaceHost = config('contentful.image.replace_host');
61
        if (! empty($searchHosts) and ! empty($replaceHost)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
62
            $url = str_replace(explode(',', $searchHosts), $replaceHost, $url);
63
        }
64
65
        return ! empty($url) ? $url . '?' . trim($imageUrl, '&'): '';
66
    }
67
68
    /**
69
     * Auto-detect image format to serve (based on browser capability).
70
     *
71
     * @param  string  $format
72
     * @return string|null
73
     */
74
    protected static function detectFormat(string $format = ''): ?string
75
    {
76
        /** @var \Jenssegers\Agent\Agent $agent */
77
        $agent = app('agent');
78
79
        if (empty($format)) {
80
            $browser = mb_strtolower($agent->browser());
81
            if (($browser === 'chrome') and ! $agent->isMobile()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
82
                $format = config('contentful.image.use_webp') ? 'webp' : null;
83
            }
84
        }
85
86
        return $format;
87
    }
88
}