Passed
Push — develop ( 241d0a...d27271 )
by Andrew
03:39
created

UrlHelper::absoluteUrlWithProtocol()   C

Complexity

Conditions 11
Paths 15

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 18
nc 15
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\errors\SiteNotFoundException;
15
use craft\helpers\UrlHelper as CraftUrlHelper;
16
17
use yii\base\Exception;
18
19
/**
20
 * ImageOptimize Settings model
21
 *
22
 * @author    nystudio107
23
 * @package   ImageOptimize
24
 * @since     1.0.0
25
 */
26
class UrlHelper extends CraftUrlHelper
27
{
28
    // Public Static Properties
29
    // =========================================================================
30
31
    // Public Static Methods
32
    // =========================================================================
33
34
    /**
35
     * Return an absolute URL with protocol that curl will be happy with
36
     *
37
     * @param string $url
38
     *
39
     * @return string
40
     */
41
    public static function absoluteUrlWithProtocol($url)
42
    {
43
        // Make this a full URL
44
        if (!self::isAbsoluteUrl($url)) {
45
            $protocol = "http";
46
            if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
47
                || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
48
            ) {
49
                $protocol = "https";
50
            }
51
            if (self::isProtocolRelativeUrl($url)) {
52
                try {
53
                    $url = self::urlWithScheme($url, $protocol);
54
                } catch (SiteNotFoundException $e) {
55
                    Craft::error($e->getMessage(), __METHOD__);
56
                }
57
            } else {
58
                try {
59
                    $url = self::siteUrl($url, null, $protocol);
60
                    if (self::isProtocolRelativeUrl($url)) {
61
                        $url = self::urlWithScheme($url, $protocol);
62
                    }
63
                } catch (Exception $e) {
64
                    Craft::error($e->getMessage(), __METHOD__);
65
                }
66
            }
67
        }
68
69
        return $url;
70
    }
71
}
72