RepresentationCollection::getDefault()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Advanced Grid for Yii2.
4
 *
5
 * @link      https://github.com/hiqdev/yii2-higrid
6
 * @package   yii2-higrid
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\higrid\representations;
12
13
use Yii;
14
use yii\base\InvalidConfigException;
15
16
/**
17
 * Class RepresentationCollection represents a collection of [[Representation]] objects
18
 * and provides API to access them.
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class RepresentationCollection implements RepresentationCollectionInterface
23
{
24
    /**
25
     * RepresentationCollection constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->fillRepresentations();
30
    }
31
32
    /**
33
     * @var array
34
     */
35
    protected $representations = [];
36
37
    /**
38
     * @void
39
     */
40
    protected function fillRepresentations()
41
    {
42
        $this->representations = [];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getAll()
49
    {
50
        foreach ($this->representations as $name => $representation) {
51
            if (!is_object($representation)) {
52
                $representation = $this->representations[$name] = Yii::createObject(array_merge([
53
                    'class' => Representation::class,
54
                ], $representation));
55
            }
56
57
            if (!$representation instanceof RepresentationInterface) {
58
                throw new InvalidConfigException('Representation "' . $name . '" must be instance of RepresentationInterface');
59
            }
60
61
            if (!$representation->isVisible()) {
62
                unset($this->representations[$name]);
63
            }
64
        }
65
66
        return $this->representations;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getByName($name)
73
    {
74
        if (!$this->exists($name)) {
75
            return $this->getDefault();
76
        }
77
78
        return $this->getAll()[$name];
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getDefault()
85
    {
86
        $representations = $this->getAll();
87
        if (count($representations) === 0) {
88
            throw new InvalidConfigException('Default can not be applied because collection is empty.');
89
        }
90
91
        return reset($representations);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($representations); of type hiqdev\higrid\representa...entationInterface|false adds false to the return on line 91 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...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function exists($name)
98
    {
99
        return isset($this->getAll()[$name]);
100
    }
101
}
102