Completed
Push — master ( 7b9015...c5b311 )
by Vladimir
26s queued 11s
created

AssetHandlerTrait::getFileFromPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 2
1
<?php
2
3
namespace allejo\stakx\Markup;
4
5
use allejo\stakx\Document\ContentItem;
6
use allejo\stakx\Filesystem\File;
7
use allejo\stakx\Filesystem\FilesystemLoader as fs;
8
use allejo\stakx\Manager\AssetManager;
9
10
/**
11
 * This trait provides the functionality for registering linked files with our AssetManager.
12
 *
13
 * @since 0.2.1
14
 */
15
trait AssetHandlerTrait
16
{
17
    /** @var AssetManager */
18
    protected $assetManager;
19
20
    /** @var ContentItem */
21
    protected $contentItem;
22
23
    /**
24
     * Get a File object from a local path relative to the ContentItem.
25
     *
26
     * @param string $localPath
27
     *
28
     * @return File
29
     */
30
    private function getFileFromPath($localPath)
31
    {
32
        $path = fs::path($this->contentItem->getAbsoluteFilePath())
33
            ->getParentDirectory()
34
            ->generatePath($localPath);
35
36
        return new File($path);
37
    }
38
39
    /**
40
     * Get the permalink this file would belong at.
41
     *
42
     * This is taken from the ContentItem's target path and puts the asset at the same location as a sibling.
43
     *
44
     * @param File $file
45
     *
46
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be \string?

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...
47
     */
48
    private function getPermalinkFromFile(File $file)
49
    {
50
        $folder = fs::path($this->contentItem->getTargetFile())->getParentDirectory();
51
52
        return fs::getRelativePath($folder->generatePath($file->getFilename()));
0 ignored issues
show
Documentation introduced by
$folder->generatePath($file->getFilename()) is of type object<allejo\stakx\Filesystem\FilesystemPath>, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55
    /**
56
     * Check if a given string is a valid URL.
57
     *
58
     * @param string $url
59
     *
60
     * @return bool
61
     */
62
    private function isValidURL($url)
63
    {
64
        return filter_var($url, FILTER_VALIDATE_URL);
65
    }
66
67
    /**
68
     * Given a URL to a local path, register this function with the AssetManager so it can be available at compile time.
69
     *
70
     * @since 0.2.1
71
     *
72
     * @param string $path
73
     *
74
     * @return void
75
     */
76
    protected function registerAsset($path)
77
    {
78
        if ($this->isValidURL($path))
79
        {
80
            return;
81
        }
82
83
        $asset = $this->getFileFromPath($path);
84
        $permalink = $this->getPermalinkFromFile($asset);
85
86
        $this->assetManager->addExplicitAsset($permalink, $asset);
87
    }
88
}
89