Passed
Push — assets ( 096b86...119c83 )
by Arnaud
02:56
created

Asset   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 52
dl 0
loc 129
rs 10
c 3
b 1
f 0
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getFile() 0 35 7
A getHtml() 0 22 5
A getInline() 0 10 3
A isFile() 0 17 4
A __toString() 0 3 1
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\Exception\Exception;
14
use Cecil\Util;
15
16
class Asset extends AbstractAsset
17
{
18
    protected $asset = [];
19
    protected $fileLoaded = false;
20
21
    /**
22
     * Loads a file.
23
     *
24
     * @param string     $path
25
     * @param array|null $options
26
     *
27
     * @return self
28
     */
29
    public function getFile(string $path, array $options = null): self
30
    {
31
        if (false === $filePath = $this->isFile($path)) {
0 ignored issues
show
introduced by
The condition false === $filePath = $this->isFile($path) is always false.
Loading history...
32
            throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path));
33
        }
34
35
        $this->fileLoaded = true;
36
37
        $baseurl = (string) $this->config->get('baseurl');
38
        $base = '';
39
40
        // handles options
41
        $canonical = null;
42
        $attributs = null;
43
        extract(is_array($options) ? $options : []);
44
45
        // set baseurl
46
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
47
            $base = rtrim($baseurl, '/');
48
        }
49
        if ($canonical === false) {
50
            $base = '';
51
        }
52
53
        // prepares options
54
        $fileInfo = new \SplFileInfo($filePath);
55
        $this->asset['path'] = $base.'/'.ltrim($path, '/');
56
        $this->asset['ext'] = $fileInfo->getExtension();
57
        $this->asset['type'] = explode('/', mime_content_type($fileInfo->getPathname()))[0];
58
        if ($this->asset['type'] == 'text') {
59
            $this->asset['content'] = file_get_contents($fileInfo->getPathname());
60
        }
61
        $this->asset['attributs'] = $attributs;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Checks if a (static) file exists.
68
     *
69
     * @param string $path
70
     *
71
     * @return string
72
     */
73
    public function isFile(string $path): string
74
    {
75
        $filePath = Util::joinFile($this->config->getStaticPath(), $path);
76
77
        if (Util::getFS()->exists($filePath)) {
78
            return $filePath;
79
        }
80
81
        // checks each theme
82
        foreach ($this->config->getTheme() as $theme) {
83
            $filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path);
84
            if (Util::getFS()->exists($filePath)) {
85
                return $filePath;
86
            }
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function __toString(): string
96
    {
97
        return $this->asset['path'];
98
    }
99
100
    /**
101
     * Returns as HTML tag.
102
     *
103
     * @return string
104
     */
105
    public function getHtml(): string
106
    {
107
        if (!$this->fileLoaded) {
108
            throw new Exception(\sprintf('%s() error: you must load a file first.', __FUNCTION__));
109
        }
110
111
        if ($this->asset['type'] == 'image') {
112
            return \sprintf(
113
                '<img src="%s" title="%s" alt="%s">',
114
                $this->asset['path'],
115
                $this->asset['attributs']['title'],
116
                $this->asset['attributs']['alt']
117
            );
118
        }
119
120
        switch ($this->asset['ext']) {
121
            case 'css':
122
                return \sprintf('<link rel="stylesheet" href="%s">', $this->asset['path']);
123
            case 'js':
124
                return \sprintf('<script src="%s"></script>', $this->asset['path']);
125
            default:
126
                throw new Exception(\sprintf('%s() error: available with CSS et JS files only.', __FUNCTION__));
127
        }
128
    }
129
130
    /**
131
     * Returns file's content.
132
     *
133
     * @return string
134
     */
135
    public function getInline(): string
136
    {
137
        if (!$this->fileLoaded) {
138
            throw new Exception(\sprintf('%s() error: you must load a file first.', __FUNCTION__));
139
        }
140
        if (!array_key_exists('content', $this->asset)) {
141
            throw new Exception(\sprintf('%s() error: available with CSS et JS files only.', __FUNCTION__));
142
        }
143
144
        return $this->asset['content'];
145
    }
146
}
147