Completed
Push — master ( 3c4d84...92e7ec )
by Vladimir
02:30
created

PermalinkDocumentTrait::getPermalink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
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
use allejo\stakx\Service;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
13
trait PermalinkDocumentTrait
14
{
15
    /** @var string */
16
    protected $permalink = null;
17
18
    /** @var array */
19
    protected $redirects = null;
20
21
    /** Extensions that need to be stripped from permalinks. */
22
    private static $extensionsToStrip = ['twig'];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function handleSpecialRedirects()
28
    {
29 1
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 6
    final public function getTargetFile()
35
    {
36 6
        $permalink = $this->getPermalink();
37 6
        $missingFile = (substr($permalink, -1) == '/');
38 6
        $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink);
39
40 6
        if ($missingFile)
41
        {
42 2
            $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html';
43
        }
44
45 6
        return ltrim($permalink, DIRECTORY_SEPARATOR);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 25
    final public function getPermalink()
52
    {
53 25
        $this->buildPermalink();
0 ignored issues
show
Bug introduced by
It seems like buildPermalink() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
55 25
        $this->permalink = $this->sanitizePermalink($this->permalink);
56 25
        $this->permalink = str_replace(DIRECTORY_SEPARATOR, '/', $this->permalink);
57 25
        $this->permalink = '/' . ltrim($this->permalink, '/'); // Permalinks should always use '/' and not be OS specific
58
59 25
        return $this->permalink;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    final public function getRedirects()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
66
    {
67 2
        if ($this->redirects === null)
68
        {
69 1
            $this->getPermalink();
70
        }
71
72 2
        $this->handleSpecialRedirects();
73
74 2
        return $this->redirects;
75
    }
76
77
    /**
78
     * Get the permalink based off the location of where the file is relative to the website. This permalink is to be
79
     * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter.
80
     *
81
     * @return string
82
     */
83 3
    final protected function getPathPermalink()
84
    {
85
        // Remove the protocol of the path, if there is one and prepend a '/' to the beginning
86 3
        $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath());
0 ignored issues
show
Bug introduced by
It seems like getRelativeFilePath() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87 3
        $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR);
88
89
        // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator
90 3
        if (DIRECTORY_SEPARATOR !== '/')
91
        {
92
            $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath);
93
        }
94
95
        // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx
96 3
        $folders = explode(DIRECTORY_SEPARATOR, $cleanPath);
97
98 3
        if (substr($folders[0], 0, 1) === '_')
99
        {
100
            array_shift($folders);
101
        }
102
103 3
        $cleanPath = implode(DIRECTORY_SEPARATOR, $folders);
104
105 3
        return $cleanPath;
106
    }
107
108
    /**
109
     * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens.
110
     *
111
     * @param string $permalink A permalink
112
     *
113
     * @return string $permalink The sanitized permalink
114
     */
115 25
    final private function sanitizePermalink($permalink)
116
    {
117
        // Remove multiple '/' together
118 25
        $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...
119
120
        // Replace all spaces with hyphens
121 25
        $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...
122
123
        // Remove all disallowed characters
124 25
        $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...
125
126 25
        if (in_array(fs::getExtension($permalink), self::$extensionsToStrip))
127
        {
128 4
            $permalink = 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...
129
        }
130
131
        // Remove any special characters before a sane value
132 25
        $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...
133
134
        // Convert permalinks to lower case
135 25
        if (!Service::getParameter('build.preserveCase'))
136
        {
137 25
            $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...
138
        }
139
140 25
        return $permalink;
141
    }
142
}
143