WidgetCollection::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * This file is part of the sauls/widget package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Widget\Collection;
14
15
use Sauls\Component\Collection\ArrayCollection;
16
use Sauls\Component\Widget\Exception\CollectionItemNotFoundException;
17
use Sauls\Component\Widget\Named;
18
use Sauls\Component\Widget\WidgetInterface;
19
use Throwable;
20
21
use function get_class;
22
use function is_string;
23
use function sprintf;
24
25
class WidgetCollection extends ArrayCollection
26
{
27 17
    public function set($key, $value): void
28
    {
29 17
        if (is_subclass_of($value, Named::class)) {
30 17
            parent::set($value->getName(), $value);
31
        }
32
33 17
        parent::set(get_class($value), $value);
34
35 17
        if (is_string($key)) {
36 2
            parent::set($key, $value);
37
        }
38 17
    }
39
40
    /**
41
     * @throws CollectionItemNotFoundException
42
     */
43 12
    public function get($key, $default = null): WidgetInterface
44
    {
45
        try {
46 12
            return parent::get($key, $default);
47 4
        } catch (Throwable $e) {
48 4
            throw new CollectionItemNotFoundException(
49 4
                sprintf('`%s` collection does not have item with name `%s`', get_class($this), $key)
50
            );
51
        }
52
    }
53
}
54