Passed
Pull Request — master (#211)
by
unknown
15:53
created

UpdateMetadataObjectValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 10 3
1
<?php
2
/*
3
 * This file is part of the TYPO3 CMS project.
4
 *
5
 * It is free software; you can redistribute it and/or modify it under
6
 * the terms of the GNU General Public License, either version 2
7
 * of the License, or any later version.
8
 *
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 *
12
 * The TYPO3 project - inspiring people to share!
13
 */
14
15
if (!defined('TYPO3_MODE')) {
16
    die('Access denied.');
17
}
18
19
use EWW\Dpf\Domain\Repository\DocumentRepository;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Core\Registry;
22
use TYPO3\CMS\Extbase\Object\ObjectManager;
23
use EWW\Dpf\Security\Security;
24
25
26
class ext_update {
27
28
    // Ideally the version corresponds with the extension version
29
    const NEW_VERSION = "v5.0.0";
30
31
    // The version
32
    const VERSION_4_0_0 = "v4.0.0";
33
34
    public function access() {
35
        $registry = GeneralUtility::makeInstance(Registry::class);
36
        $version = $registry->get('tx_dpf','updatescript-'.self::NEW_VERSION);
37
        // If the version has already been registered in the table sys_register the updatscript will be blocked.
38
        if ($version) {
39
            return FALSE;
40
        }
41
42
        return TRUE;
43
    }
44
45
    public function main() {
46
        // This script registers itself into the sys_registry table to prevent a re-run with the same version number.
47
        $registry = GeneralUtility::makeInstance(Registry::class);
48
        $newVersion = $registry->get('tx_dpf','updatescript-'.self::NEW_VERSION);
49
50
        if ($newVersion) {
51
            return FALSE;
52
        } else {
53
            try {
54
                // The necessary updates.
55
                // 3->4: (new UpdateState)->execute();
56
                // 3->4: (new UpdateAccessRestrictions)->execute();
57
                // 3->4: (new UpdateVirtualType)->execute();
58
                (new UpdateMetadataObjectValidator)->execute();
59
            } catch (\Throwable $throwable) {
60
                return "Error while updating the extension: ".($throwable->getMessage());
61
            }
62
            $registry->set('tx_dpf','updatescript-'.self::NEW_VERSION, TRUE);
63
            return "The extension has been successfully updated.";
64
        }
65
    }
66
}
67
68
69
class UpdateState
70
{
71
    const OBJECT_STATE_NEW             = "NEW";
72
    const OBJECT_STATE_ACTIVE          = "ACTIVE";
73
    const OBJECT_STATE_INACTIVE        = "INACTIVE";
74
    const OBJECT_STATE_DELETED         = "DELETED";
75
    const OBJECT_STATE_LOCALLY_DELETED = "LOCALLY_DELETED";
76
77
    public function execute()
78
    {
79
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
80
        $documentRepository = $objectManager->get(DocumentRepository::class);
81
82
        $documents = $documentRepository->crossClientFindAll();
83
84
        foreach ($documents as $oldDocument) {
85
            $oldState = $oldDocument['state'];
86
            $objectIdentifier = $oldDocument['objectIdentifier'];
0 ignored issues
show
Unused Code introduced by
The assignment to $objectIdentifier is dead and can be removed.
Loading history...
87
88
            $newDocument = $documentRepository->findByUid($oldDocument['uid']);
89
90
            switch ($oldState) {
91
                case self::OBJECT_STATE_NEW:
92
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_NEW_NONE);
93
                    break;
94
                case self::OBJECT_STATE_ACTIVE:
95
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_IN_PROGRESS_ACTIVE);
96
                    break;
97
                case self::OBJECT_STATE_INACTIVE:
98
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_IN_PROGRESS_INACTIVE);
99
                    break;
100
                case self::OBJECT_STATE_DELETED:
101
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_IN_PROGRESS_DELETED);
102
                    break;
103
                case self::OBJECT_STATE_LOCALLY_DELETED:
104
                    $newDocument->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_NONE_NONE);
105
                    break;
106
            }
107
108
            $documentRepository->update($newDocument);
109
        }
110
    }
111
}
112
113
class UpdateAccessRestrictions
114
{
115
    public function execute() {
116
117
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
118
        $repositories[] = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataObjectRepository::class);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$repositories was never initialized. Although not strictly required by PHP, it is generally a good practice to add $repositories = array(); before regardless.
Loading history...
119
        $repositories[] = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataGroupRepository::class);
120
        $repositories[] = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataPageRepository::class);
121
122
        foreach ($repositories as $repository) {
123
            foreach ($repository->crossClientFindAll() as $record) {
124
                if ($record['backend_only']) {
125
                    $recordObject = $repository->findByUid($record['uid']);
126
                    $recordObject->setAccessRestrictionRoles(array(Security::ROLE_LIBRARIAN, Security::ROLE_RESEARCHER));
127
                    $repository->update($recordObject);
128
                }
129
            }
130
        }
131
    }
132
}
133
134
class UpdateVirtualType
135
{
136
    public function execute() {
137
138
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
139
        $repository = $objectManager->get(\EWW\Dpf\Domain\Repository\DocumentTypeRepository::class);
140
141
        foreach ($repository->crossClientFindAll() as $record) {
142
            if ($record['virtual']) {
143
                $recordObject = $repository->findByUid($record['uid']);
144
                $recordObject->setVirtualType($record['virtual'] === 1);
145
                $repository->update($recordObject);
146
            }
147
        }
148
149
       // $GLOBALS['TYPO3_DB']->sql_query("ALTER TABLE tx_dpf_domain_model_documenttype CHANGE virtual zzz_deleted_virtual SMALLINT UNSIGNED DEFAULT 0 NOT NULL");
150
151
    }
152
}
153
154
class UpdateMetadataObjectValidator
155
{
156
    public function execute() {
157
158
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
159
        $repository = $objectManager->get(\EWW\Dpf\Domain\Repository\MetadataObjectRepository::class);
160
161
        foreach ($repository->crossClientFindAll() as $record) {
162
            if ($record['data_type']) {
163
                $recordObject = $repository->findByUid($record['uid']);
164
                $recordObject->setValidator($record['data_type']);
165
                $repository->update($recordObject);
166
            }
167
        }
168
    }
169
}
170