Passed
Pull Request — master (#232)
by Nathan
13:55
created

RecordCreatedEvent::getExampleProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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
25
final class RecordCreatedEvent extends AbstractEvent implements ProvidesExampleProperties
26
{
27
    /**
28
     * @label Event/TYPO3:record_created.marker.status
29
     * @marker
30
     *
31
     * @var string
32
     */
33
    protected $status;
34
35
    /**
36
     * @label Event/TYPO3:record_created.marker.table
37
     * @marker
38
     *
39
     * @var string
40
     */
41
    protected $table;
42
43
    /**
44
     * @label Event/TYPO3:record_created.marker.uid
45
     * @marker
46
     *
47
     * @var string
48
     */
49
    protected $uid;
50
51
    /**
52
     * @label Event/TYPO3:record_created.marker.record
53
     * @marker
54
     *
55
     * @var array
56
     */
57
    protected $record;
58
59
    public function run($status, $table, $recordId, array $updatedFields, DataHandler $dataHandler)
60
    {
61
        if ($table !== $this->configuration['table']) {
62
            $this->cancelDispatch();
63
        }
64
65
        $this->checkStatus($status);
66
67
        $this->uid = $this->findUid($recordId, $table, $status, $dataHandler);
68
        $this->record = $dataHandler->recordInfo($table, $this->uid, '*');
69
70
        $actualCType = '';
71
72
        if (isset($updatedFields['CType']) && is_string($updatedFields['CType'])) {
73
            $actualCType = $updatedFields['CType'];
74
        } elseif (isset($this->record['CType']) && is_string($this->record['CType'])) {
75
            $actualCType = $this->record['CType'];
76
        }
77
78
        if ($table === 'tt_content'
79
            && !empty($this->configuration['ctype'])
80
            && !preg_match($this->configuration['ctype'], $actualCType)
81
        ) {
82
            $this->cancelDispatch();
83
        }
84
85
        $this->status = $status;
86
        $this->table = $table;
87
    }
88
89
    public function getExampleProperties(): array
90
    {
91
        return [
92
            'status' => 'new',
93
            'table' => $this->configuration['table'],
94
            'uid' => 1337,
95
            'record' => [
96
                'uid' => 1337,
97
                'pid' => 42,
98
                'starttime' => 1612014706,
99
            ],
100
        ];
101
    }
102
103
    private function findUid($id, $table, $status, DataHandler $dataHandler)
104
    {
105
        $uid = $id;
106
107
        if ($status === 'new') {
108
            if (!$dataHandler->substNEWwithIDs[$id]) {
109
                //postProcessFieldArray
110
                $uid = 0;
111
            } else {
112
                //afterDatabaseOperations
113
                $uid = $dataHandler->substNEWwithIDs[$id];
114
                if (isset($dataHandler->autoVersionIdMap[$table][$uid])) {
115
                    $uid = $dataHandler->autoVersionIdMap[$table][$uid];
116
                }
117
            }
118
        }
119
120
        return (int)$uid;
121
    }
122
123
    /**
124
     * @param string $status Either "new" or "update"
125
     * @throws CancelEventDispatch
126
     */
127
    private function checkStatus($status)
128
    {
129
        if (!isset($this->configuration['statuses'])
130
            || !is_string($this->configuration['statuses'])
131
        ) {
132
            return;
133
        }
134
135
        if (strlen($this->configuration['statuses']) === 0) {
136
            return;
137
        }
138
139
        $statuses = explode(',', $this->configuration['statuses']);
140
141
        if (empty($statuses)) {
142
            return;
143
        }
144
145
        if (!in_array($status, $statuses)) {
146
            $this->cancelDispatch();
147
        }
148
    }
149
}
150