Completed
Push — master ( f0b491...11ee78 )
by Thomas
09:37
created

ExtensionRegistry   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 2
cbo 3
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 30 5
A getExtensions() 0 7 2
A getExtensionsByPackage() 0 10 3
1
<?php
2
namespace keeko\core\service;
3
4
use keeko\core\model\Extension;
5
use keeko\core\model\ExtensionQuery;
6
use phootwork\collection\ArrayList;
7
use phootwork\collection\Map;
8
use phootwork\json\Json;
9
10
class ExtensionRegistry {
11
	
12
	/** @var Map */
13
	private $extensions;
14
	
15
	/** @var Map */
16
	private $packages;
17
	
18
	public function __construct() {
19
		$this->extensions = new Map();
20
		$this->packages = new Map();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
21
		
22
		// load up all extensions
23
		$exts = ExtensionQuery::create()->joinPackage()->find();
24
		foreach ($exts as $ext) {
25
			/* @var $ext Extension */
26
			$key = $ext->getKey();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
27
			$packageName = $ext->getPackage()->getName();
28
			$data = Json::decode($ext->getData($ext->getData()));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
29
			
30
			// add to global extensions
31
			if (!$this->extensions->has($key)) {
32
				$this->extensions->set($key, new ArrayList());
33
			}
34
			$this->extensions->get($key)->add($data);
35
			
36
			// add to package extensions
37
			if (!$this->packages->has($packageName)) {
38
				$this->packages->set($packageName, new Map());
39
			}
40
			
41
			$pkg = $this->packages->get($packageName);
42
			if (!$pkg->has($key)) {
43
				$pkg->set($key, new ArrayList());
44
			}
45
			$pkg->get($key)->add($data);
46
		}
47
	}
48
49
	/**
50
	 * Returns the extension for the given key
51
	 *
52
	 * @param string $key
53
	 * @return array
54
	 */
55
	public function getExtensions($key) {
56
		if ($this->extensions->has($key)) {
57
			return $this->extensions->get($key)->toArray();
58
		}
59
		
60
		return [];
61
	}
62
	
63
	/**
64
	 * Returns the extension for the given key by a given packageName
65
	 *
66
	 * @param string $key
67
	 * @param string $packageName
68
	 * @return array
69
	 */
70
	public function getExtensionsByPackage($key, $packageName) {
71
		if ($this->packages->has($packageName)) {
72
			$pkg = $this->packages->get($packageName);
73
			if ($pkg->has($key)) {
74
				return $pkg->get($key)->toArray();
75
			}
76
		}
77
		
78
		return [];
79
	}
80
}