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

UrlHelper::absoluteUrlWithProtocol()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 16
nc 13
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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