Passed
Push — master ( 589679...68f11e )
by Romain
03:19
created

DefinitionError   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinitionErrorMessage() 0 7 1
A addData() 0 13 3
A __construct() 0 4 1
A getDefinitionErrorTca() 0 13 1
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Backend\FormEngine\DataProvider;
18
19
use CuyZ\Notiz\Core\Definition\DefinitionService;
20
use CuyZ\Notiz\Core\Notification\TCA\EntityTcaWriter;
21
use CuyZ\Notiz\Service\Container;
22
use CuyZ\Notiz\Service\ViewService;
23
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
24
use TYPO3\CMS\Core\Utility\ArrayUtility;
25
26
/**
27
 * If a definition error is found, the whole TCA is modified for entity
28
 * notifications; instead of normal fields, an error message is shown.
29
 */
30
class DefinitionError implements FormDataProviderInterface
31
{
32
    /**
33
     * @var DefinitionService
34
     */
35
    private $definitionService;
36
37
    /**
38
     * @var ViewService
39
     */
40
    private $viewService;
41
42
    /**
43
     * Manual dependency injection.
44
     */
45
    public function __construct()
46
    {
47
        $this->definitionService = Container::get(DefinitionService::class);
48
        $this->viewService = Container::get(ViewService::class);
49
    }
50
51
    /**
52
     * @param array $result
53
     * @return array
54
     */
55
    public function addData(array $result)
56
    {
57
        $tableName = $result['tableName'];
58
59
        if (!isset($GLOBALS['TCA'][$tableName]['ctrl'][EntityTcaWriter::ENTITY_NOTIFICATION])) {
60
            return $result;
61
        }
62
63
        if ($this->definitionService->getValidationResult()->hasErrors()) {
64
            ArrayUtility::mergeRecursiveWithOverrule($GLOBALS['TCA'][$tableName], $this->getDefinitionErrorTca());
65
        }
66
67
        return $result;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    private function getDefinitionErrorTca()
74
    {
75
        return [
76
            'types' => [
77
                '0' => [
78
                    'showitem' => 'definition_error_message',
79
                ],
80
            ],
81
            'columns' => [
82
                'definition_error_message' => [
83
                    'config' => [
84
                        'type' => 'user',
85
                        'userFunc' => self::class . '->getDefinitionErrorMessage',
86
                    ],
87
                ],
88
            ],
89
        ];
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getDefinitionErrorMessage()
96
    {
97
        $view = $this->viewService->getStandaloneView('Backend/TCA/DefinitionErrorMessage');
98
99
        $view->assign('result', $this->definitionService->getValidationResult());
100
101
        return $view->render();
102
    }
103
}
104