Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AssetManager | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
6.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withAddedPath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getAsset | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\HtmlBuilders\Assets; |
| 4 | |
| 5 | use RuntimeException; |
| 6 | |
| 7 | final class AssetManager |
| 8 | { |
| 9 | /** @var array<int, string> $paths */ |
| 10 | private array $paths; |
| 11 | |
| 12 | public function __construct(string ...$paths) |
| 13 | { |
| 14 | $this->paths = $paths; |
| 15 | } |
| 16 | |
| 17 | /** @param array<int, string> $paths */ |
| 18 | public static function create(array $paths): self |
| 19 | { |
| 20 | return new self(...$paths); |
| 21 | } |
| 22 | |
| 23 | public function withAddedPath(string... $paths): self |
| 24 | { |
| 25 | $returnValue = new self(); |
| 26 | $returnValue->paths = [...$this->paths, ...$paths]; |
| 27 | |
| 28 | return $returnValue; |
| 29 | } |
| 30 | |
| 31 | public function getAsset(string $filename): Asset |
| 32 | { |
| 33 | foreach ($this->paths as $path) { |
| 34 | if (file_exists($path . '/'. $filename)) { |
| 35 | return new Asset($path . '/'. $filename); |
| 36 | } |
| 37 | } |
| 38 | throw new RuntimeException('Asset ' . $filename . ' is not found!'); |
| 39 | } |
| 40 | } |