Passed
Push — develop ( a40575...26f189 )
by Andrew
03:50
created

UrlHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D absoluteUrlWithProtocol() 0 26 10
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS 3.x
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
8
 * @copyright Copyright (c) 2017 nystudio107
9
 */
10
11
namespace nystudio107\imageoptimize\helpers;
12
13
use Craft;
14
use craft\helpers\UrlHelper as CraftUrlHelper;
15
16
use yii\base\Exception;
17
18
/**
19
 * ImageOptimize Settings model
20
 *
21
 * @author    nystudio107
22
 * @package   ImageOptimize
23
 * @since     1.0.0
24
 */
25
class UrlHelper extends CraftUrlHelper
26
{
27
    // Public Static Properties
28
    // =========================================================================
29
30
    // Public Static Methods
31
    // =========================================================================
32
33
    /**
34
     * Return an absolute URL with protocol that curl will be happy with
35
     *
36
     * @param string $url
37
     *
38
     * @return string
39
     */
40
    public static function absoluteUrlWithProtocol($url)
41
    {
42
        // Make this a full URL
43
        if (!self::isAbsoluteUrl($url)) {
44
            if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
1 ignored issue
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: {currentAssign}, Probably Intended Meaning: {alternativeAssign}
Loading history...
45
                || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
46
            ) {
47
                $protocol = "https";
48
            } else {
49
                $protocol = "http";
50
            }
51
            if (self::isProtocolRelativeUrl($url)) {
52
                $url = self::urlWithScheme($url, $protocol);
53
            } else {
54
                try {
55
                    $url = self::siteUrl($url, null, $protocol);
56
                    if (self::isProtocolRelativeUrl($url)) {
57
                        $url = self::urlWithScheme($url, $protocol);
58
                    }
59
                } catch (Exception $e) {
60
                    Craft::error($e->getMessage(), __METHOD__);
61
                }
62
            }
63
        }
64
65
        return $url;
66
    }
67
}
68