Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AssetManager
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 set
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 remove
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 count
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Asset
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Asset;
16
17/**
18 * Asset manager class.
19 *
20 * @package phpOMS\Asset
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25final class AssetManager implements \Countable
26{
27    /**
28     * Assets.
29     *
30     * @var array<string, string>
31     * @since 1.0.0
32     */
33    private array $assets = [];
34
35    /**
36     * Add asset.
37     *
38     * @param string $id        Asset id
39     * @param string $asset     Asset
40     * @param bool   $overwrite Overwrite
41     *
42     * @return bool Returns true if the asset could be set otherwise false
43     *
44     * @since 1.0.0
45     */
46    public function set(string $id, string $asset, bool $overwrite = true) : bool
47    {
48        if ($overwrite || !isset($this->assets[$id])) {
49            $this->assets[$id] = $asset;
50
51            return true;
52        }
53
54        return false;
55    }
56
57    /**
58     * Remove asset.
59     *
60     * @param string $id Asset id
61     *
62     * @return bool Returns true if the asset could be removed otherwise false
63     *
64     * @since 1.0.0
65     */
66    public function remove(string $id) : bool
67    {
68        if (isset($this->assets[$id])) {
69            unset($this->assets[$id]);
70
71            return true;
72        }
73
74        return false;
75    }
76
77    /**
78     * Get asset.
79     *
80     * @param string $id Asset id
81     *
82     * @return null|string
83     *
84     * @since 1.0.0
85     */
86    public function get(string $id) : ?string
87    {
88        if (isset($this->assets[$id])) {
89            return $this->assets[$id];
90        }
91
92        return null;
93    }
94
95    /**
96     * Get asset count.
97     *
98     * @return int Returns the amount of assets (>= 0)
99     *
100     * @since 1.0.0
101     */
102    public function count() : int
103    {
104        return \count($this->assets);
105    }
106}