Completed
Pull Request — master (#48)
by Vladimir
02:36
created

PermalinkDocument   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 128
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPermalink() 0 10 1
buildPermalink() 0 1 ?
A getTargetFile() 0 13 2
A getRedirects() 0 9 2
B getPathPermalink() 0 24 3
B sanitizePermalink() 0 27 2
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
abstract class PermalinkDocument extends ReadableDocument
0 ignored issues
show
Coding Style introduced by
PermalinkDocument does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
11
{
12
    protected $permalink;
13
    protected $redirects;
14
15
    /**
16
     * Get the destination of where this Content Item would be written to when the website is compiled.
17
     *
18
     * @return string
19
     */
20 20
    final public function getTargetFile()
21
    {
22 20
        $permalink = $this->getPermalink();
23 20
        $missingFile = (substr($permalink, -1) == '/');
24 20
        $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink);
25
26 20
        if ($missingFile)
27
        {
28 12
            $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html';
29
        }
30
31 20
        return ltrim($permalink, DIRECTORY_SEPARATOR);
32
    }
33
34
    /**
35
     * Get the permalink of this Content Item.
36
     *
37
     * @throws \Exception
38
     *
39
     * @return string
40
     */
41 39
    final public function getPermalink()
42
    {
43 39
        $this->buildPermalink();
44
45 39
        $this->permalink = $this->sanitizePermalink($this->permalink);
46 39
        $this->permalink = str_replace(DIRECTORY_SEPARATOR, '/', $this->permalink);
47 39
        $this->permalink = '/' . ltrim($this->permalink, '/'); // Permalinks should always use '/' and not be OS specific
48
49 39
        return $this->permalink;
50
    }
51
52
    /**
53
     * Get an array of URLs that will redirect to.
54
     *
55
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
     */
57 14
    final public function getRedirects()
58
    {
59 14
        if (is_null($this->redirects))
60
        {
61 3
            $this->getPermalink();
62
        }
63
64 14
        return $this->redirects;
65
    }
66
67
    /**
68
     * Get the permalink based off the location of where the file is relative to the website. This permalink is to be
69
     * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter.
70
     *
71
     * @return string
72
     */
73 3
    protected function getPathPermalink()
74
    {
75
        // Remove the protocol of the path, if there is one and prepend a '/' to the beginning
76 3
        $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath());
77 3
        $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR);
78
79
        // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator
80 3
        if (DIRECTORY_SEPARATOR !== '/')
81
        {
82
            $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath);
83
        }
84
85
        // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx
86 3
        $folders = explode(DIRECTORY_SEPARATOR, $cleanPath);
87
88 3
        if (substr($folders[0], 0, 1) === '_')
89
        {
90 1
            array_shift($folders);
91
        }
92
93 3
        $cleanPath = implode(DIRECTORY_SEPARATOR, $folders);
94
95 3
        return $cleanPath;
96
    }
97
98
    /**
99
     * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens.
100
     *
101
     * @param string $permalink A permalink
102
     *
103
     * @return string $permalink The sanitized permalink
104
     */
105 39
    protected function sanitizePermalink($permalink)
106
    {
107
        // Remove multiple '/' together
108 39
        $permalink = preg_replace('/\/+/', '/', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
109
110
        // Replace all spaces with hyphens
111 39
        $permalink = str_replace(' ', '-', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
112
113
        // Remove all disallowed characters
114 39
        $permalink = preg_replace('/[^0-9a-zA-Z-_\/\\\.]/', '', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
115
116
        // Handle unnecessary extensions
117 39
        $extensionsToStrip = array('twig');
118
119 39
        if (in_array($this->fs->getExtension($permalink), $extensionsToStrip))
120
        {
121 4
            $permalink = $this->fs->removeExtension($permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
122
        }
123
124
        // Remove any special characters before a sane value
125 39
        $permalink = preg_replace('/^[^0-9a-zA-Z-_]*/', '', $permalink);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
126
127
        // Convert permalinks to lower case
128 39
        $permalink = mb_strtolower($permalink, 'UTF-8');
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $permalink. This often makes code more readable.
Loading history...
129
130 39
        return $permalink;
131
    }
132
133
    /**
134
     * @return void
135
     */
136
    abstract protected function buildPermalink();
137
}
138