Passed
Branch kernel-refactor-experiment (48ec6b)
by Caen
03:37
created

Hyperlinks   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 32
c 2
b 1
f 0
dl 0
loc 115
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A image() 0 7 2
A url() 0 13 3
A uriPath() 0 7 2
A relativeLink() 0 14 3
A formatHtmlPath() 0 16 5
A __construct() 0 3 1
A hasSiteUrl() 0 3 1
1
<?php
2
3
namespace Hyde\Framework\Foundation;
4
5
use Hyde\Framework\Contracts\HydeKernelContract;
6
use Hyde\Framework\Exceptions\BaseUrlNotSetException;
7
use Hyde\Framework\Models\Pages\DocumentationPage;
8
9
/**
10
 * Contains helpers and logic for resolving web paths for compiled files.
11
 */
12
class Hyperlinks
13
{
14
    protected HydeKernelContract $kernel;
15
16
    public function __construct(HydeKernelContract $kernel)
17
    {
18
        $this->kernel = $kernel;
19
    }
20
21
    /**
22
     * Format a link to an HTML file, allowing for pretty URLs, if enabled.
23
     *
24
     * @see \Hyde\Framework\Testing\Unit\FileHelperPageLinkPrettyUrlTest
25
     */
26
    public function formatHtmlPath(string $destination): string
27
    {
28
        if (config('site.pretty_urls', false) === true) {
29
            if (str_ends_with($destination, '.html')) {
30
                if ($destination === 'index.html') {
31
                    return '/';
32
                }
33
                if ($destination === DocumentationPage::getOutputDirectory().'/index.html') {
34
                    return DocumentationPage::getOutputDirectory().'/';
35
                }
36
37
                return substr($destination, 0, -5);
38
            }
39
        }
40
41
        return $destination;
42
    }
43
44
    /**
45
     * Inject the proper number of `../` before the links in Blade templates.
46
     *
47
     * @param  string  $destination  relative to output directory on compiled site
48
     * @return string
49
     *
50
     * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest
51
     */
52
    public function relativeLink(string $destination): string
53
    {
54
        if (str_starts_with($destination, '../')) {
55
            return $destination;
56
        }
57
58
        $nestCount = substr_count($this->kernel->currentPage(), '/');
0 ignored issues
show
Bug introduced by
The method currentPage() does not exist on Hyde\Framework\Contracts\HydeKernelContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\HydeKernelContract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $nestCount = substr_count($this->kernel->/** @scrutinizer ignore-call */ currentPage(), '/');
Loading history...
59
        $route = '';
60
        if ($nestCount > 0) {
61
            $route .= str_repeat('../', $nestCount);
62
        }
63
        $route .= $this->formatHtmlPath($destination);
64
65
        return str_replace('//', '/', $route);
66
    }
67
68
    /**
69
     * Gets a relative web link to the given image stored in the _site/media folder.
70
     */
71
    public function image(string $name): string
72
    {
73
        if (str_starts_with($name, 'http')) {
74
            return $name;
75
        }
76
77
        return $this->relativeLink('media/'.basename($name));
78
    }
79
80
    /**
81
     * Return a qualified URI path, if SITE_URL is set in .env, else return false.
82
     *
83
     * @deprecated v0.53.0-beta - Use Hyde::url() or Hyde::hasSiteUrl() instead.
84
     *
85
     * @param  string  $path  optional relative path suffix. Omit to return base url.
86
     * @return string|false
87
     */
88
    public function uriPath(string $path = ''): string|false
89
    {
90
        if (config('site.url', false)) {
91
            return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? '');
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * Check if a site base URL has been set in config (or .env).
99
     */
100
    public function hasSiteUrl(): bool
101
    {
102
        return ! blank(config('site.url'));
103
    }
104
105
    /**
106
     * Return a qualified URI path to the supplied path if a base URL is set.
107
     *
108
     * @param  string  $path  optional relative path suffix. Omit to return base url.
109
     * @param  string|null  $default  optional default value to return if no site url is set.
110
     * @return string
111
     *
112
     * @throws BaseUrlNotSetException If no site URL is set and no default is provided
113
     */
114
    public function url(string $path = '', ?string $default = null): string
115
    {
116
        $path = $this->formatHtmlPath(trim($path, '/'));
117
118
        if ($this->hasSiteUrl()) {
119
            return rtrim(rtrim(config('site.url'), '/').'/'.($path ?? ''), '/');
120
        }
121
122
        if ($default !== null) {
123
            return $default.'/'.($path ?? '');
124
        }
125
126
        throw new BaseUrlNotSetException();
127
    }
128
}
129