Completed
Push — master ( 69d6ff...60d462 )
by WEBEWEB
01:44
created

AbstractAssetManager::getResources()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 E
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Manager\Asset;
13
14
use WBW\Bundle\BootstrapBundle\Manager\AbstractManager;
15
use WBW\Bundle\BootstrapBundle\Manager\ManagerInterface;
16
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
17
use WBW\Library\Core\FileSystem\FileHelper;
18
19
/**
20
 * Abstract asset manager.
21
 *
22
 * @author e <https://github.com/e/>
23
 * @package WBW\Bundle\BootstrapBundle\Manager\Asset
24
 * @abstract
25
 */
26
abstract class AbstractAssetManager extends AbstractManager {
27
28
    /**
29
     * Directory.
30
     *
31
     * @var string
32
     */
33
    private $directory;
34
35
    /**
36
     * Extension.
37
     *
38
     * @var string
39
     */
40
    private $extension;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param string $directory The directory.
46
     * @param string $extension The extension.
47
     */
48
    public function __construct($directory, $extension) {
49
        parent::__construct();
50
        $this->setDirectory($directory);
51
        $this->setExtension($extension);
52
    }
53
54
    /**
55
     * Determines if the resource exists.
56
     *
57
     * @return boolean Returns true in case of success, false otrherwise.
58
     */
59
    public function exists() {
60
        return file_exists($this->getFilename());
61
    }
62
63
    /**
64
     * Get the directory.
65
     *
66
     * @return string Returns the directory.
67
     */
68
    public function getDirectory() {
69
        return $this->directory;
70
    }
71
72
    /**
73
     * Get the extension.
74
     *
75
     * @return string Returns the extension.
76
     */
77
    public function getExtension() {
78
        return $this->extension;
79
    }
80
81
    /**
82
     * Get the filename.
83
     *
84
     * @return string Returns the filename.
85
     */
86
    public function getFilename() {
87
        return $this->getDirectory() . "/resources" . $this->getExtension();
88
    }
89
90
    /**
91
     * Get the resources.
92
     *
93
     * @return array Returns the resources.
94
     * @throw IllegalArgumentException Throw an illegal argument exception if a resource does not math the extension.
95
     */
96
    protected function getResources() {
97
98
        // Initialize the output.
99
        $output = [];
100
101
        // Hande each provider.
102
        foreach ($this->getProviders() as $provider) {
103
104
            // Handle each resource.
105
            foreach ($provider->getResources() as $resource) {
106
107
                // Build and check the resource path.
108
                $resourcePath = $provider->getDirectory() . $resource;
109
                if (0 === preg_match("/" . $this->getExtension() . "$/", $resourcePath)) {
110
                    $msg = sprintf("The resource \"%s\" must end with the extension %s", $resourcePath, $this->getExtension());
111
                    throw new IllegalArgumentException($msg);
112
                }
113
114
                // Add the resource.
115
                $output[] = $resourcePath;
116
            }
117
        }
118
119
        // Return the output.
120
        return $output;
121
    }
122
123
    /**
124
     * Set the directory.
125
     *
126
     * @param string $directory The directory.
127
     * @return ManagerInterface Returns this resources manager.
128
     */
129
    protected function setDirectory($directory) {
130
        $this->directory = $directory;
131
        return $this;
132
    }
133
134
    /**
135
     * Set the extension.
136
     *
137
     * @param string $extension The extension.
138
     * @return ManagerInterface Returns this resources manager.
139
     */
140
    protected function setExtension($extension) {
141
        $this->extension = $extension;
142
        return $this;
143
    }
144
145
    /**
146
     * Write.
147
     *
148
     * @return void
149
     * @throw IllegalArgumentException Throw an illegal argument exception if a resource does not math the extension.
150
     */
151
    public function write() {
152
        foreach ($this->getResources() as $current) {
153
            FileHelper::appendTo($current, $this->getFilename(), true);
154
        }
155
    }
156
157
}
158