Completed
Push — master ( 6e63c6...3a4c8a )
by Maxim
02:27
created

AssetManager::getLink()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\mvc\assets;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class AssetManager
8
{
9
10
    /**
11
     * @var AbstractAsset[]
12
     */
13
    protected $assets = [];
14
    /**
15
     * @var string
16
     */
17
    protected $webRoot;
18
    /**
19
     * @var string
20
     */
21
    protected $assetDirName;
22
    /**
23
     * @var Filesystem
24
     */
25
    protected $filesystem;
26
27
    /**
28
     * @param Filesystem $filesystem
29
     * @param string $webRoot
30
     * @param string $assetDirName
31
     */
32
    public function __construct(Filesystem $filesystem, string $webRoot, string $assetDirName)
33
    {
34
        $this->filesystem = $filesystem;
35
        $this->webRoot = \rtrim($webRoot, '/');
36
        $this->assetDirName = $assetDirName;
37
    }
38
39
    /**
40
     * @param AbstractAsset $asset
41
     *
42
     * @throws \Symfony\Component\Filesystem\Exception\IOException
43
     */
44
    public function registerAsset(AbstractAsset $asset)
45
    {
46
        if ($asset->publish) {
47
            $this->publishAsset($asset);
48
        }
49
        $this->assets[] = $asset;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 View Code Duplication
    public function applyCss(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $result = [];
58
        foreach ($this->assets as $asset) {
59
            $links = $this->getLinks($asset, $asset->css());
60
            foreach ($links as $link) {
61
                $result[] = '<link rel="stylesheet" href="' . $link . '">';
62
            }
63
        }
64
        return \implode('', $result);
65
    }
66
67
    /**
68
     * @return string
69
     */
70 View Code Duplication
    public function applyJs(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $result = [];
73
        foreach ($this->assets as $asset) {
74
            $links = $this->getLinks($asset, $asset->js());
75
            foreach ($links as $link) {
76
                $result[] = '<script src="' . $link . '"></script>';
77
            }
78
        }
79
        return \implode('', $result);
80
    }
81
82
    /**
83
     * @param AbstractAsset $asset
84
     *
85
     * @throws \Symfony\Component\Filesystem\Exception\IOException
86
     */
87
    protected function publishAsset(AbstractAsset $asset)
88
    {
89
        $hash = $asset->getHash();
90
        $dir = $this->webRoot . '/' . $this->assetDirName . '/' . $hash;
91
        if ($asset->useLinks) {
92
            if (!$this->filesystem->exists($dir)) {
93
                $this->filesystem->symlink($asset->getBasePath(), $dir, true);
94
            }
95
        } else {
96
            if ($this->filesystem->exists($dir)) {
97
                $this->filesystem->remove($dir);
98
            }
99
            $this->filesystem->mirror($asset->getBasePath(), $dir);
100
            $this->filesystem->chmod($dir, 0755, 000, true);
101
        }
102
    }
103
104
    /**
105
     * @param AbstractAsset $asset
106
     *
107
     * @return string
108
     */
109
    private function getWebDir(AbstractAsset $asset): string
110
    {
111
        return $asset->publish
112
            ? '/' . $this->assetDirName . '/' . $asset->getHash() . '/'
113
            : '/';
114
    }
115
116
    /**
117
     * @param AbstractAsset $asset
118
     * @param array $files
119
     *
120
     * @return array
121
     */
122
    private function getLinks(AbstractAsset $asset, array $files): array
123
    {
124
        $result = [];
125
        $dir = $this->getWebDir($asset);
126
        foreach ($files as $file) {
127
            $result[] = $this->getLink($file, $dir);
128
        }
129
        return $result;
130
    }
131
132
    /**
133
     * @param $file
134
     * @param $dir
135
     *
136
     * @return string
137
     */
138
    private function getLink($file, $dir): string
139
    {
140
        $link = $file;
141
        if (false === \strpos($file, 'http') && false === \strpos($file, '//')) {
142
            $filePath = $dir . \ltrim($file, '/');
143
            $timestamp = \filemtime($this->webRoot . $filePath);
144
            $link = $filePath . '?' . $timestamp;
145
        }
146
147
        return $link;
148
    }
149
}
150