Completed
Push — master ( 396c8f...1fcaad )
by WEBEWEB
02:16
created

AssetHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkFilenames() 0 26 3
A getFilenames() 0 20 3
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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\Helper;
13
14
use WBW\Bundle\BootstrapBundle\Provider\AssetProviderInterface;
15
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
16
17
/**
18
 * Asset helper.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\BootstrapBundle\Helper
22
 */
23
class AssetHelper {
24
25
    /**
26
     * Check the filenames.
27
     *
28
     * @param AssetProviderInterface $provider The provider.
29
     * @param string $extension The extension.
30
     * @return array Returns the checked filenames.
31
     * @throw IllegalArgumentException Throw an illegal argument exception if a filename does not math the extension.
32
     */
33
    protected static function checkFilenames(AssetProviderInterface $provider, $extension) {
34
35
        // Initialize.
36
        $filenames = [];
37
        $pattern   = "/" . preg_quote($extension, ".") . "$/";
38
39
        // Handle each filename.
40
        foreach ($provider->getFilenames() as $current) {
41
42
            // Build the filename.
43
            $filename = $provider->getDirectory() . $current;
44
45
            // Check the filename.
46
            if (0 === preg_match($pattern, $filename)) {
47
48
                $message = sprintf("The filename \"%s\" must end with the extension %s", $filename, $extension);
49
                throw new IllegalArgumentException($message);
50
            }
51
52
            // Add the filename.
53
            $filenames[] = $filename;
54
        }
55
56
        // Return the filenames.
57
        return $filenames;
58
    }
59
60
    /**
61
     * Get the filenames.
62
     *
63
     * @param AssetProviderInterface[] $providers The asset providers.
64
     * @param strign $extension The extension.
65
     * @return array Returns the resources.
66
     * @throw IllegalArgumentException Throw an illegal argument exception if a filename does not math the extension.
67
     */
68
    public static function getFilenames(array $providers, $extension) {
69
70
        // Initialize the filenames.
71
        $filenames = [];
72
73
        // Handle each provider.
74
        foreach ($providers as $current) {
75
76
            // Check the provider.
77
            if (false === ($current instanceof AssetProviderInterface)) {
78
                continue;
79
            }
80
81
            // Add the filenames.
82
            $filenames = array_merge($filenames, static::checkFilenames($current, $extension));
83
        }
84
85
        // Return the filenames.
86
        return $filenames;
87
    }
88
89
}
90