Passed
Pull Request — master (#232)
by Nathan
04:32
created

RecordCreatedEvent::checkPid()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 0
dl 0
loc 33
rs 9.4222
c 0
b 0
f 0
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\Event\TYPO3;
19
20
use CuyZ\Notiz\Core\Event\AbstractEvent;
21
use CuyZ\Notiz\Core\Event\Exception\CancelEventDispatch;
22
use CuyZ\Notiz\Core\Event\Support\ProvidesExampleProperties;
23
use TYPO3\CMS\Core\DataHandling\DataHandler;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Core\Utility\RootlineUtility;
26
use TYPO3\CMS\Extbase\Object\ObjectManager;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Object\ObjectManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
final class RecordCreatedEvent extends AbstractEvent implements ProvidesExampleProperties
29
{
30
    /**
31
     * @label Event/TYPO3:record_created.marker.status
32
     * @marker
33
     *
34
     * @var string
35
     */
36
    protected $status;
37
38
    /**
39
     * @label Event/TYPO3:record_created.marker.table
40
     * @marker
41
     *
42
     * @var string
43
     */
44
    protected $table;
45
46
    /**
47
     * @label Event/TYPO3:record_created.marker.uid
48
     * @marker
49
     *
50
     * @var string
51
     */
52
    protected $uid;
53
54
    /**
55
     * @label Event/TYPO3:record_created.marker.record
56
     * @marker
57
     *
58
     * @var array
59
     */
60
    protected $record;
61
62
    public function run($status, $table, $recordId, array $updatedFields, DataHandler $dataHandler)
63
    {
64
        if ($table !== $this->configuration['table']) {
65
            $this->cancelDispatch();
66
        }
67
68
        $this->checkStatus($status);
69
70
        $this->uid = $this->findUid($recordId, $table, $status, $dataHandler);
71
        $this->record = $dataHandler->recordInfo($table, $this->uid, '*');
72
73
        $actualCType = '';
74
75
        if (isset($updatedFields['CType']) && is_string($updatedFields['CType'])) {
76
            $actualCType = $updatedFields['CType'];
77
        } elseif (isset($this->record['CType']) && is_string($this->record['CType'])) {
78
            $actualCType = $this->record['CType'];
79
        }
80
81
        if ($table === 'tt_content'
82
            && !empty($this->configuration['ctype'])
83
            && !preg_match($this->configuration['ctype'], $actualCType)
84
        ) {
85
            $this->cancelDispatch();
86
        }
87
88
        $this->checkPid();
89
90
        $this->status = $status;
91
        $this->table = $table;
92
    }
93
94
    public function getExampleProperties(): array
95
    {
96
        return [
97
            'status' => 'new',
98
            'table' => $this->configuration['table'],
99
            'uid' => 1337,
100
            'record' => [
101
                'uid' => 1337,
102
                'pid' => 42,
103
                'starttime' => 1612014706,
104
            ],
105
        ];
106
    }
107
108
    private function findUid($id, $table, $status, DataHandler $dataHandler)
109
    {
110
        $uid = $id;
111
112
        if ($status === 'new') {
113
            if (!$dataHandler->substNEWwithIDs[$id]) {
114
                //postProcessFieldArray
115
                $uid = 0;
116
            } else {
117
                //afterDatabaseOperations
118
                $uid = $dataHandler->substNEWwithIDs[$id];
119
                if (isset($dataHandler->autoVersionIdMap[$table][$uid])) {
120
                    $uid = $dataHandler->autoVersionIdMap[$table][$uid];
121
                }
122
            }
123
        }
124
125
        return (int)$uid;
126
    }
127
128
    /**
129
     * @param string $status Either "new" or "update"
130
     * @throws CancelEventDispatch
131
     */
132
    private function checkStatus($status)
133
    {
134
        if (!isset($this->configuration['statuses'])
135
            || !is_string($this->configuration['statuses'])
136
        ) {
137
            return;
138
        }
139
140
        if (strlen($this->configuration['statuses']) === 0) {
141
            return;
142
        }
143
144
        $statuses = explode(',', $this->configuration['statuses']);
145
146
        if (empty($statuses)) {
147
            return;
148
        }
149
150
        if (!in_array($status, $statuses)) {
151
            $this->cancelDispatch();
152
        }
153
    }
154
155
    private function checkPid()
156
    {
157
        if (!isset($this->configuration['pids'])) {
158
            return;
159
        }
160
161
        if (!is_string($this->configuration['pids'])) {
162
            return;
163
        }
164
165
        $authorizedPids = explode(',', $this->configuration['pids']);
166
167
        $currentPid = $this->record['pid'];
168
169
        /** @var ObjectManager $objectManager */
170
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
171
172
        /** @var RootlineUtility $rootlineUtility */
173
        $rootlineUtility = $objectManager->get(RootlineUtility::class, $currentPid);
174
175
        $rootline = array_map(function ($page) {
176
            return $page['pid'];
177
        }, $rootlineUtility->get());
178
179
        $rootline[] = $currentPid;
180
181
        foreach ($rootline as $rootlinePid) {
182
            if (in_array($rootlinePid, $authorizedPids)) {
183
                return;
184
            }
185
        }
186
187
        $this->cancelDispatch();
188
    }
189
}
190