Completed
Push — master ( e972d6...927902 )
by Nikola
02:23
created

WidgetManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 80
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 10 2
A createWithDefaultWidgets() 0 7 1
A has() 0 6 1
A get() 0 16 3
A createWidget() 0 14 3
A registerWidget() 0 10 4
1
<?php
2
/**
3
 * This file is part of the Disqus Helper package.
4
 *
5
 * Copyright (c) Nikola Posa <[email protected]>
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
namespace DisqusHelper\Widget;
12
13
use DisqusHelper\Exception\InvalidWidgetConfigurationException;
14
use DisqusHelper\Exception\InvalidWidgetException;
15
use DisqusHelper\Exception\WidgetNotFoundException;
16
17
class WidgetManager implements WidgetLocatorInterface
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $widgets;
23
24 17
    private function __construct()
25
    {
26 17
    }
27
28 17
    public static function create(array $widgets) : WidgetManager
29
    {
30 17
        $widgetManager = new self();
31
32 17
        foreach ($widgets as $widgetId => $widget) {
33 16
            $widgetManager->registerWidget($widgetId, $widget);
34
        }
35
36 17
        return $widgetManager;
37
    }
38
39 10
    public static function createWithDefaultWidgets() : WidgetManager
40
    {
41 10
        return self::create([
42 10
            'thread' => ThreadWidget::class,
43
            'commentscount' => CommentsCountWidget::class,
44
        ]);
45
    }
46
47 17
    public function registerWidget(string $widgetId, $widget)
48
    {
49 17
        if (!($widget instanceof WidgetInterface || is_string($widget) || is_callable($widget))) {
50 1
            throw InvalidWidgetConfigurationException::forConfiguration($widgetId, $widget);
51
        }
52
53 16
        $widgetId = strtolower($widgetId);
54
55 16
        $this->widgets[$widgetId] = $widget;
56 16
    }
57
58 4
    public function has(string $widgetId) : bool
59
    {
60 4
        $widgetId = strtolower($widgetId);
61
62 4
        return isset($this->widgets[$widgetId]);
63
    }
64
65 10
    public function get(string $widgetId) : WidgetInterface
66
    {
67 10
        $widgetId = strtolower($widgetId);
68
69 10
        if (!isset($this->widgets[$widgetId])) {
70 2
            throw WidgetNotFoundException::forWidgetId($widgetId);
71
        }
72
73 8
        $widget = $this->createWidget($widgetId);
74
75 8
        if (!$widget instanceof WidgetInterface) {
76 1
            throw InvalidWidgetException::forWidget($widget);
77
        }
78
79 7
        return $widget;
80
    }
81
82 8
    protected function createWidget($widgetId)
83
    {
84 8
        $widget = $this->widgets[$widgetId];
85
86 8
        if (is_string($widget)) {
87 5
            return new $widget();
88
        }
89
90 3
        if (is_callable($widget)) {
91 2
            return $widget();
92
        }
93
94 1
        return $widget;
95
    }
96
}