IconService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 47
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerNotificationIcon() 0 21 3
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Service;
19
20
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
21
use CuyZ\Notiz\Service\Traits\ExtendedSelfInstantiateTrait;
22
use TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider;
23
use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;
24
use TYPO3\CMS\Core\Imaging\IconRegistry;
25
use TYPO3\CMS\Core\SingletonInterface;
26
use TYPO3\CMS\Core\Utility\StringUtility;
27
28
class IconService implements SingletonInterface
29
{
30
    use ExtendedSelfInstantiateTrait;
31
32
    /**
33
     * @var IconRegistry
34
     */
35
    protected $iconRegistry;
36
37
    /**
38
     * @var array
39
     */
40
    protected $registeredIcons = [];
41
42
    /**
43
     * @param IconRegistry $iconRegistry
44
     */
45
    public function __construct(IconRegistry $iconRegistry)
46
    {
47
        $this->iconRegistry = $iconRegistry;
48
    }
49
50
    /**
51
     * @param NotificationDefinition $notification
52
     * @return string
53
     */
54
    public function registerNotificationIcon(NotificationDefinition $notification): string
55
    {
56
        $iconIdentifier = 'tx-notiz-icon-notification-' . $notification->getIdentifier();
57
58
        if (!in_array($iconIdentifier, $this->registeredIcons)) {
59
            $this->registeredIcons[] = $iconIdentifier;
60
61
            $iconPath = $notification->getIconPath();
62
63
            $iconProviderClass = StringUtility::endsWith(strtolower($iconPath), 'svg')
64
                ? SvgIconProvider::class
65
                : BitmapIconProvider::class;
66
67
            $this->iconRegistry->registerIcon(
68
                $iconIdentifier,
69
                $iconProviderClass,
70
                ['source' => $notification->getIconPath()]
71
            );
72
        }
73
74
        return $iconIdentifier;
75
    }
76
}
77