Passed
Push — analysis-peWDDV ( 08939e )
by Arnaud
05:35 queued 15s
created

Url::__construct()   D

Complexity

Conditions 19
Paths 104

Size

Total Lines 80
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 19
eloc 49
c 3
b 1
f 0
nc 104
nop 3
dl 0
loc 80
rs 4.4833

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 $version;
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
     *     'addhash'   => false,
42
     *     'format'    => 'json',
43
     * ];
44
     *
45
     * @param Builder                $builder
46
     * @param Page|Asset|string|null $value
47
     * @param array|null             $options
48
     */
49
    public function __construct(Builder $builder, $value, array $options = null)
50
    {
51
        $this->builder = $builder;
52
        $this->config = $builder->getConfig();
53
        if (!self::$slugifier instanceof Slugify) {
54
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
55
        }
56
        $this->baseurl = (string) $this->config->get('baseurl');
57
        $this->version = (string) $this->builder->time;
58
59
        // handles options
60
        $canonical = null;
61
        $addhash = false;
62
        $format = null;
63
        extract(is_array($options) ? $options : [], EXTR_IF_EXISTS);
64
65
        // canonical URL?
66
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
67
            $base = rtrim($this->baseurl, '/');
68
        }
69
        if ($canonical === false) {
70
            $base = '';
71
        }
72
73
        // value is empty (ie: `url()`)
74
        if (empty($value) || $value == '/') {
75
            return $base.'/';
76
        }
77
78
        // potential page id
79
        $pageId = self::$slugifier->slugify((string) $value);
80
81
        switch (true) {
82
            // Page
83
            case $value instanceof Page:
84
                if (!$format) {
85
                    $format = $value->getVariable('output');
86
                    if (is_array($value->getVariable('output'))) {
87
                        $format = $value->getVariable('output')[0];
88
                    }
89
                    if (!$format) {
90
                        $format = 'html';
91
                    }
92
                }
93
                $url = $value->getUrl($format, $this->config);
94
                $url = $base.'/'.ltrim($url, '/');
95
96
                return $url;
97
            // Asset
98
            case $value instanceof Asset:
99
                $asset = $value;
100
                $url = $asset['path'];
101
                if ($addhash) {
102
                    $url .= '?'.$this->version;
103
                }
104
                $url = $base.'/'.ltrim($url, '/');
105
                $asset['path'] = $url;
106
107
                return $asset;
108
            // External URL
109
            case Util::isExternalUrl($value):
110
                return $value;
111
            // asset as string
112
            case false !== strpos($value, '.') ? true : false:
113
                $url = $value;
114
                if ($addhash) {
115
                    $url .= '?'.$this->version;
116
                }
117
                $url = $base.'/'.ltrim($url, '/');
118
119
                return $url;
120
            // Page ID as string
121
            case $this->builder->getPages()->has($pageId):
122
                $page = $this->builder->getPages()->get($pageId);
123
124
                return self::__construct($this->builder, $page, $options);
125
            // others cases?
126
            default:
127
                // others cases
128
                $url = $base.'/'.(string) $value;
129
        }
130
    }
131
}
132