Passed
Pull Request — master (#1676)
by Arnaud
05:15 queued 14s
created

Url::__construct()   F

Complexity

Conditions 27
Paths 184

Size

Total Lines 87
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 27.4917

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 27
eloc 58
nc 184
nop 3
dl 0
loc 87
ccs 52
cts 57
cp 0.9123
crap 27.4917
rs 3.4666
c 2
b 1
f 0

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\Menu\Entry as MenuEntry;
18
use Cecil\Collection\Page\Page;
19
use Cecil\Config;
20
use Cecil\Renderer\Page as PageRenderer;
21
use Cecil\Util;
22
use Cocur\Slugify\Slugify;
23
24
class Url
25
{
26
    /** @var Builder */
27
    protected $builder;
28
29
    /** @var Config */
30
    protected $config;
31
32
    /** @var Page|Asset|string|null */
33
    protected $value;
34
35
    /** @var array */
36
    protected $options;
37
38
    /** @var string */
39
    protected $baseurl;
40
41
    /** @var string */
42
    protected $url;
43
44
    /** @var Slugify */
45
    private static $slugifier;
46
47
    /**
48
     * Creates an URL from a Page, an Asset or a string.
49
     *
50
     * @param Builder                $builder
51
     * @param Page|Asset|string|null $value
52
     * @param array|null             $options Rendering options, e.g.: ['canonical' => true, 'format' => 'html', 'language' => 'fr']
53
     */
54 1
    public function __construct(Builder $builder, $value, array $options = null)
55
    {
56 1
        $this->builder = $builder;
57 1
        $this->config = $builder->getConfig();
58 1
        if (!self::$slugifier instanceof Slugify) {
59 1
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
60
        }
61 1
        $this->baseurl = (string) $this->config->get('baseurl');
62
63
        // handles options
64 1
        $canonical = null; // if true, add prefix URL with baseurl
65 1
        $format = null;    // set output format
66 1
        $language = null;  // force language
67 1
        extract(\is_array($options) ? $options : [], EXTR_IF_EXISTS);
68
69
        // canonical URL?
70 1
        $base = '';
71 1
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
72 1
            $base = rtrim($this->baseurl, '/');
73
        }
74 1
        if ($canonical === false) {
75 1
            $base = '';
76
        }
77
78
        // value is empty (i.e.: `url()`)
79 1
        if (\is_null($value) || empty($value) || $value == '/') {
80 1
            $this->url = $base . '/';
81
82 1
            return;
83
        }
84
85
        switch (true) {
86 1
            case $value instanceof Page:
87
                /** @var Page $value */
88 1
                if (!$format) {
89 1
                    $format = $value->getVariable('output');
90 1
                    if (\is_array($value->getVariable('output'))) {
91 1
                        $default = array_search('html', $value->getVariable('output')) ?: 0;
92 1
                        $format = $value->getVariable('output')[$default];
93
                    }
94 1
                    if (!$format) {
95 1
                        $format = 'html';
96
                    }
97
                }
98 1
                $this->url = $base . '/' . ltrim((new PageRenderer($this->config))->getUrl($value, $format), '/');
99 1
                if ($canonical && $value->hasVariable('canonical') && $value->getVariable('canonical')['url']) { // canonical URL
100
                    $this->url = $value->getVariable('canonical')['url'];
101
                }
102 1
                break;
103 1
            case $value instanceof MenuEntry:
104
                /** @var MenuEntry $value */
105
                $this->url = $base . '/' . ltrim($value['url'], '/');
106
                break;
107 1
            case $value instanceof Asset:
108
                /** @var Asset $value */
109 1
                $this->url = $base . '/' . ltrim($value['path'], '/');
110 1
                if ($value->isImageInCdn()) {
111
                    $this->url = (string) $value;
112
                }
113 1
                break;
114 1
            case \is_string($value):
115
                /** @var string $value */
116
                // potential Page ID
117 1
                $pageId = self::$slugifier->slugify($value);
118
                // force language?
119 1
                $lang = '';
120 1
                if ($language !== null && $language != $this->config->getLanguageDefault()) {
121 1
                    $pageId = "$language/$pageId";
122 1
                    $lang = "$language/";
123
                }
124
                switch (true) {
125 1
                    case Util\Url::isUrl($value):
126 1
                        $this->url = $value;
127 1
                        break;
128 1
                    case $this->builder->getPages()->has($pageId):
129 1
                        $this->url = (string) new self(
130 1
                            $this->builder,
131 1
                            $this->builder->getPages()->get($pageId),
132 1
                            $options
133 1
                        );
134 1
                        break;
135
                    default:
136
                        // remove double language prefix
137 1
                        if ($lang && Util\Str::startsWith($value, $lang)) {
138
                            $value = substr($value, \strlen($lang));
139
                        }
140 1
                        $this->url = $base . '/' . $lang . ltrim($value, '/');
141
                }
142
        }
143
    }
144
145
    /**
146
     * Returns URL.
147
     */
148 1
    public function __toString(): string
149
    {
150 1
        return (string) $this->url;
151
    }
152
153
    /**
154
     * Returns URL.
155
     */
156 1
    public function getUrl(): string
157
    {
158 1
        return $this->__toString();
159
    }
160
}
161