Completed
Push — master ( 6de470...9e4701 )
by Dmitry
12:25
created

RepresentationCollection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fillRepresentations() 0 4 1
A getAll() 0 16 4
A getByName() 0 8 2
A getDefault() 0 8 2
A exists() 0 4 1
1
<?php
2
3
namespace hiqdev\higrid\representations;
4
5
use Yii;
6
use yii\base\Exception;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Class RepresentationCollection represents a collection of [[Representation]] objects
11
 * and provides API to access them.
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class RepresentationCollection implements RepresentationCollectionInterface
16
{
17
    /**
18
     * RepresentationCollection constructor.
19
     */
20
    public function __construct()
21
    {
22
        $this->fillRepresentations();
23
    }
24
25
    /**
26
     * @var array
27
     */
28
    protected $representations = [];
29
30
    /**
31
     * @void
32
     */
33
    protected function fillRepresentations()
34
    {
35
        $this->representations = [];
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAll()
42
    {
43
        foreach ($this->representations as $name => $representation) {
44
            if (!is_object($representation)) {
45
                $representation = $this->representations[$name] = Yii::createObject(array_merge([
46
                    'class' => Representation::class
47
                ], $representation));
48
            }
49
50
            if (!$representation instanceof RepresentationInterface) {
51
                throw new InvalidConfigException('Representation "' . $name .'" must be instance of RepresentationInterface');
52
            }
53
        }
54
55
        return $this->representations;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getByName($name)
62
    {
63
        if (!$this->exists($name)) {
64
            $this->getDefault();
65
        }
66
67
        return $this->getAll()[$name];
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getDefault()
74
    {
75
        if (count($this->getAll()) === 0) {
76
            throw new InvalidConfigException('Default can not be applied because collection is empty.');
77
        }
78
79
        return reset($this->getAll());
0 ignored issues
show
Bug introduced by
$this->getAll() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
Comprehensibility Best Practice introduced by
The expression reset($this->getAll()); of type hiqdev\higrid\representa...entationInterface|false adds false to the return on line 79 which is incompatible with the return type declared by the interface hiqdev\higrid\representa...onInterface::getDefault of type hiqdev\higrid\representa...RepresentationInterface. It seems like you forgot to handle an error condition.
Loading history...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function exists($name)
86
    {
87
        return isset($this->getAll()[$name]);
88
    }
89
}
90