RepresentationCollectionFinder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A buildClassName() 0 4 1
A find() 0 10 2
A findOrFallback() 0 10 2
A findOrFail() 0 9 2
A forCurrentRoute() 0 13 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\grid;
12
13
use hiqdev\higrid\representations\RepresentationCollection;
14
use hiqdev\higrid\representations\RepresentationCollectionInterface;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\helpers\Inflector;
18
19
/**
20
 * Class RepresentationCollectionFinder helps to find a representation collection class
21
 * depending on [[module]] and [[controller]] name.
22
 *
23
 * @author Dmytro Naumenko <[email protected]>
24
 */
25
class RepresentationCollectionFinder implements RepresentationCollectionFinderInterface
26
{
27
    private $module;
28
    private $controller;
29
    /**
30
     * @var string
31
     * // TODO: declare format. example: '\hipanel\modules\%s\grid\%sRepresentations'
32
     */
33
    private $representationsLocation;
34
35
    public function __construct($module, $controller, string $representationsLocation)
36
    {
37
        $this->module = $module;
38
        $this->controller = $controller;
39
        $this->representationsLocation = $representationsLocation;
40
    }
41
42
    protected function buildClassName()
43
    {
44
        return sprintf($this->representationsLocation, $this->module, $this->controller);
45
    }
46
47
    /**
48
     * @return RepresentationCollectionInterface|RepresentationCollection
49
     */
50
    public function find()
51
    {
52
        $representationsClass = $this->buildClassName();
53
54
        if (!class_exists($representationsClass)) {
55
            return null;
56
        }
57
58
        return Yii::createObject(['class' => $representationsClass]);
59
    }
60
61
    /**
62
     * @return RepresentationCollectionInterface|RepresentationCollection
63
     */
64
    public function findOrFallback()
65
    {
66
        $collection = $this->find();
67
68
        if ($collection === null) {
69
            $collection = new RepresentationCollection();
70
        }
71
72
        return $collection;
73
    }
74
75
    /**
76
     * @throws InvalidConfigException When collection does not exist for the route
77
     * @return RepresentationCollection|RepresentationCollectionInterface
78
     */
79
    public function findOrFail()
80
    {
81
        $collection = $this->find();
82
        if ($collection === null) {
83
            throw new InvalidConfigException('Representation class "' . $this->buildClassName() . '" does not exist');
84
        }
85
86
        return $collection;
87
    }
88
89
    public static function forCurrentRoute(string $representationsLocation): RepresentationCollectionFinderInterface
90
    {
91
        $controller = Yii::$app->controller;
92
93
        if ($controller->module instanceof RepresentationCollectionFinderProviderInterface) {
94
            return $controller->module->getRepresentationCollectionFinder();
95
        }
96
97
        $module = $controller->module->id;
98
        $controller = Inflector::id2camel($controller->id);
99
100
        return new static($module, $controller, $representationsLocation);
101
    }
102
}
103