1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of PhuninNode. |
6
|
|
|
* |
7
|
|
|
** (c) 2013 - 2016 Cees-Jan Kiewiet |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WyriHaximus\PhuninNode\Plugins; |
14
|
|
|
|
15
|
|
|
use React\Promise\PromiseInterface; |
16
|
|
|
use WyriHaximus\PhuninNode\Configuration; |
17
|
|
|
use WyriHaximus\PhuninNode\Metric; |
18
|
|
|
use WyriHaximus\PhuninNode\Node; |
19
|
|
|
use WyriHaximus\PhuninNode\PluginInterface; |
20
|
|
|
use function React\Promise\resolve; |
21
|
|
|
use function WyriHaximus\PhuninNode\metricPromisesToObjectStorage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Plugins |
25
|
|
|
* @package WyriHaximus\PhuninNode\Plugins |
26
|
|
|
*/ |
27
|
|
|
class Plugins implements PluginInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Node |
31
|
|
|
*/ |
32
|
|
|
private $node; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cached configuration state |
36
|
|
|
* |
37
|
|
|
* @var Configuration |
38
|
|
|
*/ |
39
|
|
|
private $configuration; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
7 |
|
public function setNode(Node $node) |
45
|
|
|
{ |
46
|
7 |
|
$this->node = $node; |
47
|
7 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function getSlug(): string |
53
|
|
|
{ |
54
|
1 |
|
return 'plugins'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function getCategorySlug(): string |
61
|
|
|
{ |
62
|
1 |
|
return 'phunin_node'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
2 |
|
public function getConfiguration(): PromiseInterface |
69
|
|
|
{ |
70
|
2 |
|
if ($this->configuration instanceof Configuration) { |
71
|
1 |
|
return resolve($this->configuration); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->configuration = new Configuration(); |
75
|
2 |
|
$this->configuration->setPair('graph_category', 'phunin_node'); |
76
|
2 |
|
$this->configuration->setPair('graph_title', 'Plugins loaded'); |
77
|
2 |
|
$this->configuration->setPair('plugins_count.label', 'Plugins'); |
78
|
2 |
|
$this->configuration->setPair('plugins_category_count.label', 'Plugin Categories'); |
79
|
|
|
|
80
|
2 |
|
return resolve($this->configuration); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
2 |
|
public function getValues(): PromiseInterface |
87
|
|
|
{ |
88
|
2 |
|
return metricPromisesToObjectStorage([ |
89
|
2 |
|
$this->getPluginCountValue(), |
90
|
2 |
|
$this->getPluginCategoryCountValue(), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return Metric |
96
|
|
|
*/ |
97
|
2 |
|
private function getPluginCountValue(): Metric |
98
|
|
|
{ |
99
|
2 |
|
return new Metric('plugins_count', $this->node->getPlugins()->count()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Metric |
104
|
|
|
*/ |
105
|
2 |
|
private function getPluginCategoryCountValue(): Metric |
106
|
|
|
{ |
107
|
2 |
|
$categories = []; |
108
|
2 |
|
$plugins = $this->node->getPlugins(); |
109
|
2 |
|
foreach ($plugins as $plugin) { |
110
|
2 |
|
$category = $plugin->getCategorySlug(); |
111
|
2 |
|
$categories[$category] = true; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
return new Metric('plugins_category_count', count($categories)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getCapabilities(): array |
121
|
|
|
{ |
122
|
|
|
return []; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|