Passed
Pull Request — master (#1636)
by Arnaud
09:06 queued 04:00
created

Url   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 98.11%

Importance

Changes 7
Bugs 1 Features 2
Metric Value
eloc 60
c 7
b 1
f 2
dl 0
loc 120
ccs 52
cts 53
cp 0.9811
rs 10
wmc 23

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getUrl() 0 3 1
F __construct() 0 72 21
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
                        $format = $value->getVariable('output')[0]; // first format by default
90
                    }
91 1
                    if (!$format) {
92 1
                        $format = 'html'; // 'html' format by default
93
                    }
94
                }
95 1
                $this->url = $base . '/' . ltrim((new PageRenderer($this->config))->getUrl($value, $format), '/');
96 1
                if ($value->hasVariable('canonical') && $value->getVariable('canonical')['url']) { // canonical URL
97
                    $this->url = $value->getVariable('canonical')['url'];
98
                }
99 1
                break;
100 1
            case $value instanceof Asset:
101 1
                $this->url = $base . '/' . ltrim($value['path'], '/');
102 1
                break;
103 1
            case is_string($value):
104
                // potential Page ID
105 1
                $pageId = self::$slugifier->slugify($value);
106
                // force language?
107 1
                $lang = '';
108 1
                if ($language !== null && $language != $this->config->getLanguageDefault()) {
109 1
                    $pageId = "$pageId.$language";
110 1
                    $lang = "$language/";
111
                }
112
                switch (true) {
113 1
                    case Util\Url::isUrl($value):
114 1
                        $this->url = $value;
115 1
                        break;
116 1
                    case $this->builder->getPages()->has($pageId):
117 1
                        $this->url = (string) new self(
118 1
                            $this->builder,
119 1
                            $this->builder->getPages()->get($pageId),
120 1
                            $options
121 1
                        );
122 1
                        break;
123
                    default:
124 1
                        $this->url = $base . '/' . $lang . ltrim($value, '/');
125
                }
126
        }
127
    }
128
129
    /**
130
     * Returns URL.
131
     */
132 1
    public function __toString(): string
133
    {
134 1
        return (string) $this->url;
135
    }
136
137
    /**
138
     * Returns URL.
139
     */
140 1
    public function getUrl(): string
141
    {
142 1
        return $this->__toString();
143
    }
144
}
145