Passed
Push — master ( 83a0ee...80a0ec )
by Arnaud
05:28
created

Url::__construct()   F

Complexity

Conditions 24
Paths 160

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 24.0328

Importance

Changes 9
Bugs 4 Features 1
Metric Value
cc 24
eloc 53
c 9
b 4
f 1
nc 160
nop 3
dl 0
loc 76
ccs 50
cts 52
cp 0.9615
crap 24.0328
rs 3.6666

How to fix   Long Method    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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Assets;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Config;
19
use Cecil\Renderer\Page as PageRenderer;
20
use Cecil\Util;
21
use Cocur\Slugify\Slugify;
22
23
class Url
24
{
25
    /** @var Builder */
26
    protected $builder;
27
28
    /** @var Config */
29
    protected $config;
30
31
    /** @var Page|Asset|string|null */
32
    protected $value;
33
34
    /** @var array */
35
    protected $options;
36
37
    /** @var string */
38
    protected $baseurl;
39
40
    /** @var string */
41
    protected $url;
42
43
    /** @var Slugify */
44
    private static $slugifier;
45
46
    /**
47
     * Creates an URL from a Page, an Asset or a string.
48
     *
49
     * @param Builder                $builder
50
     * @param Page|Asset|string|null $value
51
     * @param array|null             $options Rendering options, e.g.: ['canonical' => true, 'format' => 'html', 'language' => 'fr']
52
     */
53 1
    public function __construct(Builder $builder, $value, array $options = null)
54
    {
55 1
        $this->builder = $builder;
56 1
        $this->config = $builder->getConfig();
57 1
        if (!self::$slugifier instanceof Slugify) {
58 1
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
59
        }
60 1
        $this->baseurl = (string) $this->config->get('baseurl');
61
62
        // handles options
63 1
        $canonical = null; // if true, add prefix URL with baseurl
64 1
        $format = null;    // set output format
65 1
        $language = null;  // force language
66 1
        extract(is_array($options) ? $options : [], EXTR_IF_EXISTS);
67
68
        // canonical URL?
69 1
        $base = '';
70 1
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
71 1
            $base = rtrim($this->baseurl, '/');
72
        }
73 1
        if ($canonical === false) {
74 1
            $base = '';
75
        }
76
77
        // value is empty (i.e.: `url()`)
78 1
        if (is_null($value) || empty($value) || $value == '/') {
79 1
            $this->url = $base . '/';
80
81 1
            return;
82
        }
83
84
        switch (true) {
85 1
            case $value instanceof Page:
86 1
                if (!$format) {
87 1
                    $format = $value->getVariable('output');
88 1
                    if (is_array($value->getVariable('output'))) {
89 1
                        $default = array_search('html', $value->getVariable('output')) ?: 0;
90 1
                        $format = $value->getVariable('output')[$default];
91
                    }
92 1
                    if (!$format) {
93 1
                        $format = 'html';
94
                    }
95
                }
96 1
                $this->url = $base . '/' . ltrim((new PageRenderer($this->config))->getUrl($value, $format), '/');
97 1
                if ($canonical && $value->hasVariable('canonical') && $value->getVariable('canonical')['url']) { // canonical URL
98
                    $this->url = $value->getVariable('canonical')['url'];
99
                }
100 1
                break;
101 1
            case $value instanceof Asset:
102 1
                $this->url = $base . '/' . ltrim($value['path'], '/');
103 1
                if ((bool) $this->config->get('assets.images.cdn.enabled')) {
104
                    $this->url = (string) $value;
105
                }
106 1
                break;
107 1
            case is_string($value):
108
                // potential Page ID
109 1
                $pageId = self::$slugifier->slugify($value);
110
                // force language?
111 1
                $lang = '';
112 1
                if ($language !== null && $language != $this->config->getLanguageDefault()) {
113 1
                    $pageId = "$pageId.$language";
114 1
                    $lang = "$language/";
115
                }
116
                switch (true) {
117 1
                    case Util\Url::isUrl($value):
118 1
                        $this->url = $value;
119 1
                        break;
120 1
                    case $this->builder->getPages()->has($pageId):
121 1
                        $this->url = (string) new self(
122 1
                            $this->builder,
123 1
                            $this->builder->getPages()->get($pageId),
124 1
                            $options
125 1
                        );
126 1
                        break;
127
                    default:
128 1
                        $this->url = $base . '/' . $lang . ltrim($value, '/');
129
                }
130
        }
131
    }
132
133
    /**
134
     * Returns URL.
135
     */
136 1
    public function __toString(): string
137
    {
138 1
        return (string) $this->url;
139
    }
140
141
    /**
142
     * Returns URL.
143
     */
144 1
    public function getUrl(): string
145
    {
146 1
        return $this->__toString();
147
    }
148
}
149