DefaultDefinitionComponents   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 75
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFiles() 0 28 6
A __construct() 0 3 1
A register() 0 13 3
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\Domain\Definition\Builder\Component;
19
20
use CuyZ\Notiz\Core\Definition\Builder\Component\DefinitionComponents;
21
use CuyZ\Notiz\Core\Definition\Builder\Component\Source\DefinitionSource;
22
use CuyZ\Notiz\Core\Support\NotizConstants;
23
use CuyZ\Notiz\Domain\Definition\Builder\Component\Source\TypoScriptDefinitionSource;
24
use CuyZ\Notiz\Service\ExtensionConfigurationService;
25
use TYPO3\CMS\Core\SingletonInterface;
26
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27
28
/**
29
 * Service used for registration of default definition components supplied by
30
 * the extension.
31
 */
32
class DefaultDefinitionComponents implements SingletonInterface
33
{
34
    const DEFAULT_DEFINITION_FILE_PRIORITY = 1337;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $registrationDone = false;
40
41
    /**
42
     * @var ExtensionConfigurationService
43
     */
44
    protected $extensionConfigurationService;
45
46
    /**
47
     * @param ExtensionConfigurationService $extensionConfigurationService
48
     */
49
    public function __construct(ExtensionConfigurationService $extensionConfigurationService)
50
    {
51
        $this->extensionConfigurationService = $extensionConfigurationService;
52
    }
53
54
    /**
55
     * Default definition comes from TypoScript files, so a definition source
56
     * must be added to the definition builder and the files must be registered.
57
     *
58
     * @param DefinitionComponents $components
59
     */
60
    public function register(DefinitionComponents $components)
61
    {
62
        if ($this->registrationDone) {
63
            return;
64
        }
65
66
        $this->registrationDone = true;
67
68
        /** @var TypoScriptDefinitionSource $typoScriptDefinitionSource */
69
        $typoScriptDefinitionSource = $components->addSource(DefinitionSource::SOURCE_TYPOSCRIPT);
70
71
        foreach ($this->getDefaultFiles() as $file) {
72
            $typoScriptDefinitionSource->addFilePath($file, self::DEFAULT_DEFINITION_FILE_PRIORITY);
73
        }
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    private function getDefaultFiles(): array
80
    {
81
        $defaultFiles = [
82
            NotizConstants::TYPOSCRIPT_PATH . 'Channel/Channels.Default.typoscript',
83
            NotizConstants::TYPOSCRIPT_PATH . 'Notification/Notifications.typoscript',
84
        ];
85
86
        // TYPO3 events can be enabled/disabled in the extension configuration.
87
        if ($this->extensionConfigurationService->getConfigurationValue('events.typo3')) {
88
            $defaultFiles[] = NotizConstants::TYPOSCRIPT_PATH . 'Event/Events.TYPO3.typoscript';
89
90
            if (ExtensionManagementUtility::isLoaded('scheduler')) {
91
                $defaultFiles[] = NotizConstants::TYPOSCRIPT_PATH . 'Event/Events.Scheduler.typoscript';
92
            }
93
        }
94
95
        // The core extension "form" can dispatch events.
96
        if (ExtensionManagementUtility::isLoaded('form')) {
97
            $defaultFiles[] = NotizConstants::TYPOSCRIPT_PATH . 'Event/Events.Form.typoscript';
98
        }
99
100
        if (ExtensionManagementUtility::isLoaded('blog')
101
            && version_compare(ExtensionManagementUtility::getExtensionVersion('blog'), '9.0.0', '>')
102
        ) {
103
            $defaultFiles[] = NotizConstants::TYPOSCRIPT_PATH . 'Event/Events.Blog.typoscript';
104
        }
105
106
        return $defaultFiles;
107
    }
108
}
109