Passed
Push — developer ( eca833...8db73e )
by Radosław
30:59
created

Widgets::getAll()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 20

Duplication

Lines 6
Ratio 30 %

Importance

Changes 0
Metric Value
dl 6
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 9
nop 0
1
<?php
2
/**
3
 * Widget base file.
4
 *
5
 * @copyright YetiForce Sp. z o.o.
6
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
7
 * @author    Radosław Skrzypczak <[email protected]>
8
 */
9
10
namespace App;
11
12
/**
13
 * Widget class.
14
 */
15
final class Widgets
16
{
17
	/** @var string Module name. */
18
	private $moduleName;
19
20
	/**
21
	 * Get instance.
22
	 *
23
	 * @param string $moduleName
24
	 *
25
	 * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
26
	 */
27
	public static function getInstance(string $moduleName): self
28
	{
29
		$instance = new self();
30
		$instance->moduleName = $moduleName;
31
		return $instance;
32
	}
33
34
	/**
35
	 * Get all widgets.
36
	 *
37
	 * @return array
38
	 */
39
	public function getAll(): array
40
	{
41
		$widgets = [];
42 View Code Duplication
		if (\App\Cache::has('Widgets', $this->moduleName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
			$widgetsData = \App\Cache::get('Widgets', $this->moduleName);
44
		} else {
45
			$widgetsData = \App\Api::getInstance()->call("{$this->moduleName}/Widgets") ?: [];
46
			\App\Cache::save('Widgets', $this->moduleName, $widgetsData, \App\Cache::LONG);
47
		}
48
		foreach ($widgetsData as $widgetData) {
49
			if (!\in_array($widgetData['type'], ['RelatedModule'])) {
50
				continue;
51
			}
52
			$handlerModule = \App\Loader::getModuleClassName($this->moduleName, 'Widget', $widgetData['type']);
53
			$widget = new $handlerModule($this->moduleName);
54
			$widget->setData($widgetData);
55
			$widgets[$widgetData['id']] = $widget;
56
		}
57
		return $widgets;
58
	}
59
}
60