Passed
Pull Request — master (#1)
by Evgenii
01:11
created

is_supported_format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Helick\Imgix;
4
5
use Imgix\UrlBuilder;
6
7
/**
8
 * Get the imgix domain.
9
 *
10
 * @return string
11
 */
12
function domain(): string
13
{
14
    return defined('IMGIX_DOMAIN')
15
        ? IMGIX_DOMAIN
0 ignored issues
show
Bug introduced by
The constant Helick\Imgix\IMGIX_DOMAIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
        : '';
17
}
18
19
/**
20
 * Get the imgix supported formats.
21
 *
22
 * @return array
23
 */
24
function supported_formats(): array
25
{
26
    /**
27
     * Control the imgix supported formats.
28
     *
29
     * @param array $supportedFormats
30
     */
31
    $supportedFormats = apply_filters('helick_imgix_supported_formats', ['gif', 'jpg', 'jpeg', 'png']);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
    $supportedFormats = /** @scrutinizer ignore-call */ apply_filters('helick_imgix_supported_formats', ['gif', 'jpg', 'jpeg', 'png']);
Loading history...
32
    $supportedFormats = (array)$supportedFormats;
33
34
    return $supportedFormats;
35
}
36
37
/**
38
 * Check whether the given format is supported.
39
 *
40
 * @param string $format
41
 *
42
 * @return bool
43
 */
44
function is_supported_format(string $format): bool
45
{
46
    return in_array($format, supported_formats(), true);
47
}
48
49
/**
50
 * Check whether the given image url is processable.
51
 *
52
 * @param string $imageUrl
53
 *
54
 * @return bool
55
 */
56
function is_processable_image_url(string $imageUrl): bool
57
{
58
    if (strpos($imageUrl, wp_upload_dir()['baseurl']) !== 0) {
0 ignored issues
show
Bug introduced by
The function wp_upload_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
    if (strpos($imageUrl, /** @scrutinizer ignore-call */ wp_upload_dir()['baseurl']) !== 0) {
Loading history...
59
        return false;
60
    }
61
62
    $imageUrlPath = parse_url($imageUrl, PHP_URL_PATH);
63
64
    $imageFormat = pathinfo($imageUrlPath, PATHINFO_EXTENSION);
65
    $imageFormat = strtolower($imageFormat);
66
67
    if (!is_supported_format($imageFormat)) {
68
        return false;
69
    }
70
71
    /**
72
     * Control whether the given image url is processable.
73
     *
74
     * @param bool   $isProcessable
75
     * @param string $imageUrl
76
     * @param array  $args
77
     */
78
    $isProcessable = apply_filters('helick_imgix_image_url_processable', true, $imageUrl, $args);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
    $isProcessable = /** @scrutinizer ignore-call */ apply_filters('helick_imgix_image_url_processable', true, $imageUrl, $args);
Loading history...
Comprehensibility Best Practice introduced by
The variable $args seems to be never defined.
Loading history...
79
    $isProcessable = (bool)$isProcessable;
80
81
    return $isProcessable;
82
}
83
84
/**
85
 * Get the imgix url.
86
 *
87
 * @param string $imageUrl
88
 * @param array  $args
89
 * @param bool   $useHttps
90
 * @param string $signKey
91
 *
92
 * @return string
93
 */
94
function url(string $imageUrl, array $args = [], bool $useHttps = true, string $signKey = ''): string
95
{
96
    if (!is_processable_image_url($imageUrl)) {
97
        return $imageUrl;
98
    }
99
100
    $imageUrlPath = parse_url($imageUrl, PHP_URL_PATH);
101
102
    $imageFile = basename($imageUrlPath);
103
    $imageFile = urlencode($imageFile);
104
105
    $imageUrlPath = str_replace($imageUrl, $imageFile, $imageUrlPath);
106
107
    /**
108
     * Control the imgix image url path.
109
     *
110
     * @param string $imageUrlPath
111
     * @param array  $args
112
     */
113
    $imageUrlPath = apply_filters('helick_imgix_image_url_path', $imageUrlPath, $args);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
    $imageUrlPath = /** @scrutinizer ignore-call */ apply_filters('helick_imgix_image_url_path', $imageUrlPath, $args);
Loading history...
114
115
    /**
116
     * Control the imgix arguments.
117
     *
118
     * @param array  $args
119
     * @param string $imageUrlPath
120
     */
121
    $args = apply_filters('helick_imgix_args', $args, $imageUrlPath);
122
123
    $urlBuilder = new UrlBuilder(domain(), $useHttps, $signKey);
124
    $imgixUrl   = $urlBuilder->createURL($imageUrlPath, $args);
125
126
    /**
127
     * Control the imgix url.
128
     *
129
     * @param string $imgixUrl
130
     * @param string $imageUrl
131
     * @param array  $args
132
     */
133
    $imgixUrl = apply_filters('helick_imgix_url', $imgixUrl, $imageUrl, $args);
134
135
    return $imgixUrl;
136
}
137