Skip to content

Commit 0be65e1

Browse files
committed
drop-deprecated-hook-internals
1 parent dbd848f commit 0be65e1

1 file changed

Lines changed: 1 addition & 263 deletions

File tree

library/Notifications/Hook/ObjectsRendererHook.php

Lines changed: 1 addition & 263 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,15 @@
66
namespace Icinga\Module\Notifications\Hook;
77

88
use Generator;
9-
use Icinga\Application\Hook;
10-
use Icinga\Application\Logger;
11-
use Icinga\Module\Notifications\Hook\V2\SourceHook;
12-
use Icinga\Module\Notifications\Model\Objects;
13-
use ipl\Html\Attributes;
14-
use ipl\Html\BaseHtmlElement;
15-
use ipl\Html\HtmlElement;
16-
use ipl\Html\Text;
179
use ipl\Html\ValidHtml;
18-
use ipl\Web\Url;
19-
use ipl\Web\Widget\Link;
20-
use Throwable;
2110

2211
/**
2312
* Base hook to prepare and render objects
2413
*
25-
* @deprecated For object name, use the db column `object.name`. To create object link, use
26-
* {@see SourceHook::createObjectLink()} instead.
14+
* @deprecated Sources are expected to transmit human readable object names already with each event.
2715
*/
2816
abstract class ObjectsRendererHook
2917
{
30-
/**
31-
* Array of Object ID tags for each source
32-
*
33-
* It has the following structure : ['object source type' => ['object ID' => [object ID tags]]].
34-
*
35-
* @var array<string, array<string, array<string, string>>>
36-
*/
37-
private static array $objectIdTags = [];
38-
39-
/**
40-
* Array of HTMLs for objects with their corresponding object IDs as keys
41-
*
42-
* It has the following structure : ['object ID' => registered HTML for the object name].
43-
*
44-
* @var array<string, ValidHtml>
45-
*/
46-
private static array $objectNameHtmls = [];
47-
48-
/**
49-
* Array of object names with their corresponding object IDs as keys
50-
*
51-
* It has the following structure : ['object ID' => 'object name'].
52-
*
53-
* @var array<string, string>
54-
*/
55-
private static array $objectNames = [];
56-
5718
/**
5819
* Get the object names for the objects using the object ID tags
5920
*
@@ -88,227 +49,4 @@ abstract public function getSourceType(): string;
8849
* @return ?ValidHtml Returns null if no object with given tag found
8950
*/
9051
abstract public function createObjectLink(array $objectIdTag): ?ValidHtml;
91-
92-
/**
93-
* Register object ID tags to the cache
94-
*
95-
* @param Objects $obj
96-
*
97-
* @return void
98-
*/
99-
final public static function register(Objects $obj): void
100-
{
101-
self::$objectIdTags[$obj->source->type][$obj->id] = $obj->id_tags;
102-
}
103-
104-
/**
105-
* Load HTMLs to be rendered for the object names to the cache using the cached objects
106-
*
107-
* @param bool $asHtml If true loads object names as HTMLs otherwise as string
108-
*
109-
* @return void
110-
*/
111-
final public static function load(bool $asHtml = true): void
112-
{
113-
self::prepare(self::$objectIdTags, $asHtml); // Prepare object names as HTML or string
114-
115-
self::$objectIdTags = [];
116-
}
117-
118-
/**
119-
* Prepare the objects to be rendered using the given object ID tags for each source
120-
*
121-
* The supplied object ID tags must have the following structure:
122-
* ['object source type' => ['object ID' => [object ID tags]]].
123-
*
124-
* @param array<string, array<string, array<string, string>>> $objectIdTags Array of object ID tags for each source
125-
* @param bool $asHtml When true, object names are prepared as HTML otherwise as string
126-
*
127-
* @return void
128-
*/
129-
private static function prepare(array $objectIdTags, bool $asHtml = true): void
130-
{
131-
$idTagToObjectIdMap = [];
132-
$objectNames = $asHtml ? self::$objectNameHtmls : self::$objectNames;
133-
foreach ($objectIdTags as $sourceType => $objects) {
134-
foreach ($objects as $objectId => $tags) {
135-
if (! isset($objectNames[$objectId])) {
136-
$idTagToObjectIdMap[$sourceType][] = [$objectId, $tags];
137-
}
138-
}
139-
}
140-
141-
$objectDisplayNames = [];
142-
143-
/** @var self $hook */
144-
foreach (Hook::all('Notifications\\ObjectsRenderer') as $hook) {
145-
$source = $hook->getSourceType();
146-
147-
if (isset($idTagToObjectIdMap[$source])) {
148-
try {
149-
$objectIDTagsForSource = array_map(
150-
function ($object) {
151-
return $object[1];
152-
},
153-
$idTagToObjectIdMap[$source]
154-
);
155-
156-
$objectNamesFromSource = $asHtml
157-
? $hook->getHtmlForObjectNames($objectIDTagsForSource)
158-
: $hook->getObjectNames($objectIDTagsForSource);
159-
160-
/** @var array $objectIdTag */
161-
foreach ($objectNamesFromSource as $objectIdTag => $objectName) {
162-
foreach ($idTagToObjectIdMap[$source] as $key => $val) {
163-
$diff = array_intersect_assoc($val[1], $objectIdTag);
164-
if (count($diff) === count($val[1]) && count($diff) === count($objectIdTag)) {
165-
unset($idTagToObjectIdMap[$source][$key]);
166-
167-
if ($asHtml) {
168-
$objectName = HtmlElement::create(
169-
'div',
170-
Attributes::create([
171-
'class' => [
172-
'icinga-module',
173-
'module-' . ($source === 'icinga2' ? 'icingadb' : $source)
174-
]
175-
]),
176-
$objectName
177-
);
178-
}
179-
180-
$objectDisplayNames[$val[0]] = $objectName;
181-
182-
continue 2;
183-
}
184-
}
185-
}
186-
} catch (Throwable $e) {
187-
Logger::error('Failed to load hook %s:', get_class($hook), $e);
188-
}
189-
}
190-
}
191-
192-
if ($asHtml) {
193-
self::$objectNameHtmls += $objectDisplayNames;
194-
} else {
195-
self::$objectNames += $objectDisplayNames;
196-
}
197-
}
198-
199-
/**
200-
* Get the object name of the given object
201-
*
202-
* If an HTML for the object name is not loaded, it is prepared using object ID tags and the same is returned.
203-
*
204-
* @param Objects $obj
205-
*
206-
* @return BaseHtmlElement
207-
*/
208-
final public static function getObjectName(Objects $obj): BaseHtmlElement
209-
{
210-
$objId = $obj->id;
211-
if (! isset(self::$objectNameHtmls[$objId])) {
212-
self::prepare([$obj->source->type => [$objId => $obj->id_tags]]);
213-
}
214-
215-
if (isset(self::$objectNameHtmls[$objId])) {
216-
return self::$objectNameHtmls[$objId];
217-
}
218-
219-
self::$objectNameHtmls[$objId] = new HtmlElement(
220-
'div',
221-
null,
222-
Text::create(self::createObjectNameAsString($obj))
223-
);
224-
225-
return self::$objectNameHtmls[$objId];
226-
}
227-
228-
/**
229-
* Get the object name of the given object as string
230-
*
231-
* If the object name is not loaded, it is prepared using object ID tags and the same is returned.
232-
*
233-
* @param Objects $obj
234-
*
235-
* @return string
236-
*/
237-
final public static function getObjectNameAsString(Objects $obj): string
238-
{
239-
$objId = $obj->id;
240-
if (! isset(self::$objectNames[$objId])) {
241-
self::prepare([$obj->source->type => [$objId => $obj->id_tags]], false);
242-
}
243-
244-
if (isset(self::$objectNames[$objId])) {
245-
return self::$objectNames[$objId];
246-
}
247-
248-
return self::createObjectNameAsString($obj);
249-
}
250-
251-
/**
252-
* Create object name string for the given object
253-
*
254-
* @param Objects $obj
255-
*
256-
* @return string
257-
*/
258-
private static function createObjectNameAsString(Objects $obj): string
259-
{
260-
$objectTags = [];
261-
262-
foreach ($obj->id_tags as $tag => $value) {
263-
$objectTags[] = sprintf('%s=%s', $tag, $value);
264-
}
265-
266-
return implode(', ', $objectTags);
267-
}
268-
269-
/**
270-
* Render object link for the given object
271-
*
272-
* @param Objects $object
273-
*
274-
* @return ?ValidHtml
275-
*/
276-
final public static function renderObjectLink(Objects $object): ?ValidHtml
277-
{
278-
/** @var self $hook */
279-
foreach (Hook::all('Notifications\\ObjectsRenderer') as $hook) {
280-
try {
281-
$sourceType = $hook->getSourceType();
282-
if ($object->source->type === $sourceType) {
283-
$objectLink = $hook->createObjectLink($object->id_tags);
284-
if ($objectLink === null) {
285-
break;
286-
}
287-
288-
return $objectLink->addAttributes([
289-
'data-base-target' => '_next',
290-
'class' => [
291-
'icinga-module',
292-
'module-' . ($sourceType === 'icinga2' ? 'icingadb' : $sourceType)
293-
]
294-
]);
295-
}
296-
} catch (Throwable $e) {
297-
Logger::error('Failed to load hook %s:', get_class($hook), $e);
298-
}
299-
}
300-
301-
// Fallback, if the hook is not implemented
302-
if (! $object->url) {
303-
return null;
304-
}
305-
306-
$objUrl = Url::fromPath($object->url);
307-
308-
return new Link(
309-
Text::create(self::createObjectNameAsString($object)),
310-
$objUrl->isExternal() ? $objUrl->getAbsoluteUrl() : $objUrl->getRelativeUrl(),
311-
['class' => 'subject', 'data-base-target' => '_next']
312-
);
313-
}
31452
}

0 commit comments

Comments
 (0)