Passed
Push — i18n ( 3172bc )
by Arnaud
03:47
created

Url::__construct()   D

Complexity

Conditions 19
Paths 96

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 19.0045

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 19
eloc 45
nc 96
nop 3
dl 0
loc 71
ccs 42
cts 43
cp 0.9767
crap 19.0045
rs 4.5166
c 3
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
 * 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\Renderer\Page as PageRenderer;
17
use Cecil\Util;
18
use Cocur\Slugify\Slugify;
19
20
class Url
21
{
22
    /** @var Builder */
23
    protected $builder;
24
    /** @var Config */
25
    protected $config;
26
    /** @var Page|Asset|string|null */
27
    protected $value;
28
    /** @var array */
29
    protected $options;
30
    /** @var string */
31
    protected $baseurl;
32
    /** @var string */
33
    protected $url;
34
    /** @var Slugify */
35
    private static $slugifier;
36
37
    /**
38
     * Creates an URL from string, Page or Asset.
39
     *
40
     * $options[
41
     *     'canonical' => true,
42
     *     'format'    => 'json',
43
     * ];
44
     *
45
     * @param Builder                $builder
46
     * @param Page|Asset|string|null $value
47
     * @param array|null             $options
48
     */
49 1
    public function __construct(Builder $builder, $value, array $options = null)
50
    {
51 1
        $this->builder = $builder;
52 1
        $this->config = $builder->getConfig();
53 1
        if (!self::$slugifier instanceof Slugify) {
54 1
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
55
        }
56 1
        $this->baseurl = (string) $this->config->get('baseurl');
57
58
        // handles options
59 1
        $canonical = null;
60 1
        $format = null;
61 1
        extract(is_array($options) ? $options : [], EXTR_IF_EXISTS);
62 1
63
        // canonical URL?
64
        $base = '';
65 1
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
66 1
            $base = rtrim($this->baseurl, '/');
67 1
        }
68
        if ($canonical === false) {
69 1
            $base = '';
70 1
        }
71
72
        // value is empty (ie: `url()`)
73
        if (is_null($value) || empty($value) || $value == '/') {
74 1
            $this->url = $base.'/';
75
76
            return;
77
        }
78
79 1
        switch (true) {
80 1
            // Page
81
            case $value instanceof Page:
82 1
                if (!$format) {
83
                    $format = $value->getVariable('output');
84
                    if (is_array($value->getVariable('output'))) {
85
                        $format = $value->getVariable('output')[0];
86
                    }
87 1
                    if (!$format) {
88 1
                        $format = 'html';
89 1
                    }
90 1
                }
91 1
                $this->url = $base.'/'.ltrim((new PageRenderer($this->config))->getUrl($value, $format), '/');
92
                break;
93 1
            // Asset
94 1
            case $value instanceof Asset:
95
                $asset = $value;
96
                $this->url = $base.'/'.ltrim($asset['path'], '/');
97 1
                break;
98 1
            // string
99
            case is_string($value):
100 1
                // potential Page ID
101 1
                $pageId = self::$slugifier->slugify($value);
102 1
                switch (true) {
103 1
                    // External URL
104
                    case Util\Url::isUrl($value):
105 1
                        $this->url = $value;
106
                        break;
107 1
                    // Page ID as string
108
                    case $this->builder->getPages()->has($pageId):
109
                        $page = $this->builder->getPages()->get($pageId);
110 1
                        $this->url = new self($this->builder, $page, $options);
111 1
                        break;
112 1
                    // asset as string
113
                    case false !== strpos($value, '.') ? true : false:
114 1
                        $this->url = $base.'/'.ltrim($value, '/');
115 1
                        break;
116 1
                    // others cases?
117 1
                    default:
118
                        // others cases
119 1
                        $this->url = $base.'/'.$value;
120 1
                }
121 1
        }
122
    }
123
124
    /**
125 1
     * Returns URL.
126
     *
127
     * @return string
128 1
     */
129
    public function __toString(): string
130
    {
131
        return $this->url;
132
    }
133
134
    /**
135 1
     * Returns URL.
136
     *
137 1
     * @return string
138
     */
139
    public function getUrl(): string
140
    {
141
        return $this->__toString();
142
    }
143
}
144