Passed
Push — analysis-ajLGvB ( bce2e9 )
by Arnaud
05:13 queued 13s
created

Url   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 124
rs 10
wmc 19

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
C createUrl() 0 89 17
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 Slugify */
26
    private static $slugifier;
27
28
    /**
29
     * @param Builder $builder
30
     */
31
    public function __construct(Builder $builder)
32
    {
33
        $this->builder = $builder;
34
        $this->config = $builder->getConfig();
35
        if (!self::$slugifier instanceof Slugify) {
36
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
37
        }
38
    }
39
40
    /**
41
     * Creates an URL.
42
     *
43
     * $options[
44
     *     'canonical' => true,
45
     *     'addhash'   => false,
46
     *     'format'    => 'json',
47
     * ];
48
     *
49
     * @param Page|Asset|string|null $value
50
     * @param array|null             $options
51
     *
52
     * @return mixed
53
     */
54
    public function createUrl($value = null, $options = null)
55
    {
56
        $baseurl = (string) $this->config->get('baseurl');
57
        $hash = md5((string) $this->config->get('time'));
58
        $base = '';
59
60
        // handles options
61
        $canonical = null;
62
        $addhash = false;
63
        $format = null;
64
        extract(is_array($options) ? $options : []);
65
66
        // set baseurl
67
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
68
            $base = rtrim($baseurl, '/');
69
        }
70
        if ($canonical === false) {
71
            $base = '';
72
        }
73
74
        // value is empty: url()
75
        if (empty($value) || $value == '/') {
76
            return $base.'/';
77
        }
78
79
        // value is a Page item
80
        if ($value instanceof Page) {
81
            if (!$format) {
82
                $format = $value->getVariable('output');
83
                if (is_array($value->getVariable('output'))) {
84
                    $format = $value->getVariable('output')[0];
85
                }
86
                if (!$format) {
87
                    $format = 'html';
88
                }
89
            }
90
            $url = $value->getUrl($format, $this->config);
91
            $url = $base.'/'.ltrim($url, '/');
92
93
            return $url;
94
        }
95
96
        // value is an Asset object
97
        if ($value instanceof Asset) {
98
            $asset = $value;
99
            $url = $asset['path'];
100
            if ($addhash) {
101
                $url .= '?'.$hash;
102
            }
103
            $url = $base.'/'.ltrim($url, '/');
104
            $asset['path'] = $url;
105
106
            return $asset;
107
        }
108
109
        // value is an external URL
110
        if (Util::isExternalUrl($value)) {
111
            $url = $value;
112
113
            return $url;
114
        }
115
116
        // value is a string
117
        $value = Util::joinPath($value);
118
119
        // value is (certainly) a path to a ressource (ie: 'path/file.pdf')
120
        if (false !== strpos($value, '.')) {
121
            $url = $value;
122
            if ($addhash) {
123
                $url .= '?'.$hash;
124
            }
125
            $url = $base.'/'.ltrim($url, '/');
126
127
            return $url;
128
        }
129
130
        // others cases
131
        $url = $base.'/'.$value;
132
133
        // value is a page ID (ie: 'path/my-page')
134
        try {
135
            $pageId = self::$slugifier->slugify($value);
136
            $page = $this->builder->getPages()->get($pageId);
137
            $url = $this->createUrl($page, $options);
138
        } catch (\DomainException $e) {
139
            // nothing to do
140
        }
141
142
        return $url;
143
    }
144
}
145