Passed
Push — menu ( 0c6171...3ef669 )
by Arnaud
16:20 queued 12:53
created

Url::__construct()   F

Complexity

Conditions 20
Paths 192

Size

Total Lines 77
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 20.004

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 20
eloc 48
c 3
b 0
f 0
nc 192
nop 3
dl 0
loc 77
ccs 45
cts 46
cp 0.9783
crap 20.004
rs 3.4

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