Passed
Push — develop ( 3d3b3e...df5074 )
by Brent
03:50
created

Js::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Renderer\Extension;
4
5
use JSMin\JSMin;
6
use Pageon\Html\Source;
7
use Stitcher\Exception\InvalidConfiguration;
8
use Stitcher\File;
9
use Stitcher\Renderer\Extension;
10
use Symfony\Component\Filesystem\Filesystem;
11
12
class Js implements Extension
13
{
14
    /** @var string */
15
    protected $publicDirectory;
16
17
    /** @var bool */
18
    protected $defer = false;
19
20
    /** @var bool */
21
    protected $async = false;
22
23
    /** @var bool */
24
    protected $minify = false;
25
26 10
    public function __construct(
27
        string $publicDirectory
28
    ) {
29 10
        $this->publicDirectory = $publicDirectory;
30 10
    }
31
32 5
    public function setMinify(bool $minify): self
33
    {
34 5
        $this->minify = $minify;
35
36 5
        return $this;
37
    }
38
39 5
    public function name(): string
40
    {
41 5
        return 'js';
42
    }
43
44 5
    public function link(string $src): string
45
    {
46 5
        $source = $this->parseSource($src);
47
48 5
        $script = "<script src=\"{$source->url()}\"";
49
50 5
        if ($this->defer) {
51 3
            $script .= ' defer';
52
        }
53
54 5
        if ($this->async) {
55 1
            $script .= ' async';
56
        }
57
58 5
        $script .= '></script>';
59
60 5
        return $script;
61
    }
62
63 1
    public function inline(string $src): string
64
    {
65 1
        $source = $this->parseSource($src);
66
67 1
        return '<script>' . $source->content() . '</script>';
68
    }
69
70 3
    public function defer(): Js
71
    {
72 3
        $this->defer = true;
73
74 3
        return $this;
75
    }
76
77 1
    public function async(): Js
78
    {
79 1
        $this->async = true;
80
81 1
        return $this;
82
    }
83
84 7
    public function parseSource(string $src): Source
85
    {
86 7
        $src = ltrim($src, '/');
87
88 7
        ['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src);
0 ignored issues
show
Bug introduced by
The variable $filename does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $extension does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
89
90 7
        $content = File::read($src);
91
92 7
        if (!$content) {
93
            throw InvalidConfiguration::fileNotFound(File::path($src));
94
        }
95
96 7
        if ($this->minify) {
97
            $content = JSMin::minify($content);
98
        }
99
100 7
        $path = "{$dirname}/{$filename}.{$extension}";
101
102 7
        $this->saveFile($path, $content);
103
104 7
        return new Source("/{$path}", $content);
105
    }
106
107 7
    protected function saveFile(string $path, string $content): void
108
    {
109 7
        $fs = new Filesystem();
110
111 7
        $fs->dumpFile(
112 7
            File::path("{$this->publicDirectory}/{$path}"),
113 7
            $content
114
        );
115 7
    }
116
}
117