Passed
Push — develop ( 359c84...5df010 )
by Brent
04:09
created

Js::saveFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Renderer\Extension;
4
5
use Pageon\Html\Source;
6
use Stitcher\Exception\InvalidConfiguration;
7
use Stitcher\Exception\StitcherException;
8
use Stitcher\File;
9
use Stitcher\Renderer\Extension;
10
use Symfony\Component\Filesystem\Filesystem;
11
12
class Js implements Extension
13
{
14
    protected $publicDirectory;
15
16
    protected $defer = false;
17
    protected $async = false;
18
19 9
    public function __construct(
20
        string $publicDirectory
21
    ) {
22 9
        $this->publicDirectory = $publicDirectory;
23 9
    }
24
25 4
    public function name(): string
26
    {
27 4
        return 'js';
28
    }
29
30 5
    public function link(string $src): string
31
    {
32 5
        $source = $this->parseSource($src);
33
34 5
        $script = "<script src=\"{$source->url()}\"";
35
36 5
        if ($this->defer) {
37 3
            $script .= ' defer';
38
        }
39
40 5
        if ($this->async) {
41 1
            $script .= ' async';
42
        }
43
44 5
        $script .= "></script>";
45
46 5
        return $script;
47
    }
48
49 1
    public function inline(string $src): string
50
    {
51 1
        $source = $this->parseSource($src);
52
53 1
        return '<script>' . $source->content() . '</script>';
54
    }
55
56 3
    public function defer(): Js
57
    {
58 3
        $this->defer = true;
59
60 3
        return $this;
61
    }
62
63 1
    public function async(): Js
64
    {
65 1
        $this->async = true;
66
67 1
        return $this;
68
    }
69
70 7
    public function parseSource(string $src): Source
71
    {
72 7
        $src = ltrim($src, '/');
73
74 7
        ['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src);
0 ignored issues
show
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...
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...
75
76 7
        $content = File::read($src);
77
78 7
        if (!$content) {
79
            throw InvalidConfiguration::fileNotFound(File::path($src));
80
        }
81
82 7
        $path = "{$dirname}/{$filename}.{$extension}";
83
84 7
        $this->saveFile($path, $content);
85
86 7
        return new Source("/{$path}", $content);
87
    }
88
89 7
    protected function saveFile(string $path, string $content): void
90
    {
91 7
        $fs = new Filesystem();
92
93 7
        $fs->dumpFile(
94 7
            File::path("{$this->publicDirectory}/{$path}"),
95 7
            $content
96
        );
97 7
    }
98
}
99