UserFormFixes   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 12
Bugs 1 Features 0
Metric Value
wmc 13
eloc 76
c 12
b 1
f 0
dl 0
loc 111
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A performMigration() 0 40 4
B writeObjects() 0 42 7
A writeInner() 0 9 2
1
<?php
2
3
namespace Sunnysideup\MigrateData\Tasks;
4
5
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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...
6
use SilverStripe\ORM\DataList;
7
use SilverStripe\UserForms\Model\EditableFormField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\Model\EditableFormField 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...
8
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ecipient\EmailRecipient 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...
9
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ubmission\SubmittedForm 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...
10
use SilverStripe\UserForms\Model\UserDefinedForm;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\Model\UserDefinedForm 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...
11
use SilverStripe\Versioned\Versioned;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Versioned\Versioned 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...
12
13
class UserFormFixes extends MigrateDataTaskBase
14
{
15
    protected $title = 'Publish Userforms Models';
16
17
    protected $description = 'Some items in UserForms need to be saved / published - see: https://github.com/silverstripe/silverstripe-userforms/issues/804';
18
19
    protected $enabled = true;
20
21
    protected function performMigration()
22
    {
23
        // EditableFormField
24
        $this->flushNowLine();
25
        $this->flushNow('fixing EditableFormField');
26
        $this->flushNowLine();
27
        $objects = EditableFormField::get();
28
        $parentClassName = SiteTree::class;
29
        $field = 'Parent';
30
        $this->writeObjects($objects, $parentClassName, $field);
31
32
        // EmailRecipient::get()
33
        $this->flushNowLine();
34
        $this->flushNow('fixing EmailRecipient');
35
        $this->flushNowLine();
36
        $objects = EmailRecipient::get();
37
        $parentClassName = SiteTree::class;
38
        $field = 'Parent';
39
        $this->writeObjects($objects, $parentClassName, $field);
40
41
        // SubmittedForm
42
        $this->flushNowLine();
43
        $this->flushNow('fixing SubmittedForm');
44
        $this->flushNowLine();
45
        $objects = SubmittedForm::get();
46
        $parentClassName = SiteTree::class;
47
        $this->writeObjects($objects, $parentClassName, $field);
48
49
        $this->flushNowLine();
50
        $this->flushNow('fixing UserForm');
51
        $this->flushNowLine();
52
        $objects = UserDefinedForm::get();
53
        foreach ($objects as $object) {
54
            $this->flushNow('Publishing ' . $object->getTitle());
55
            $isPublished = $object->IsPublished()  && !$object->isModifiedOnDraft();
56
            ;
57
            $object->writeToStage(Versioned::DRAFT);
58
            $object->publishRecursive();
59
            if (!$isPublished) {
60
                $object->doUnpublish();
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param DataList $objects
67
     */
68
    protected function writeObjects($objects, string $parentClassName, string $field)
69
    {
70
        $classField = $field . 'Class';
71
        $idField = $field . 'ID';
72
        foreach ($objects as $object) {
73
            $this->flushNow('-');
74
            $relationClassValue = $object->{$classField};
75
            $relationIDValue = $object->{$idField};
76
            $this->writeObject($object);
77
            if (class_exists($relationClassValue)) {
78
                if ($relationIDValue) {
79
                    $relation = $relationClassValue::get_by_id($relationIDValue);
80
                    if ($relation && $relation->ClassName === $relationClassValue) {
81
                        $this->flushNow('... OK: ' . $object->ClassName . ' relation => ' . $relationClassValue . ' WHERE ID = ' . $relationIDValue);
82
83
                        continue;
84
                    }
85
                    $this->flushNow('... ERROR: : ' . $object->ClassName . ' relation => could not find: ' . $relationClassValue . ' WHERE ID = ' . $relationIDValue, 'error');
86
                    /** @var null|SiteTree $page */
87
                    $page = $parentClassName::get_by_id($relationIDValue);
88
                    if ($page) {
89
                        $this->flushNow('... FIXING ' . $object->getTitle());
90
                        $object->{$classField} = $page->ClassName;
91
                        $this->writeInner($object);
92
                        $this->flushNow('... FIXING: setting ' . $relationClassValue . ' WHERE ID = ' . $relationIDValue . ' to ' . $page->ClassName, 'repaired');
93
                    } else {
94
                        $this->flushNow('... Skipping page (should extend ' . $parentClassName . ') with ID: ' . $relationIDValue . ' as it could not be found.');
95
                    }
96
                } else {
97
                    $this->flushNow(
98
                        '
99
                        ... ERROR: : ' . $relationClassValue . ' class does not exist for ID = ' . $relationIDValue . ',
100
                        this should be set in the following field: ' . $idField . ' or ParentClass',
101
                        'error'
102
                    );
103
                }
104
            } else {
105
                $this->flushNow(
106
                    '
107
                    ... ERROR: : ' . $relationClassValue . ' class does not exist,
108
                    this should be set in the following field: ' . $classField . ' or ParentClass',
109
                    'error'
110
                );
111
            }
112
        }
113
    }
114
115
    protected function writeInner($object)
116
    {
117
        if ($object->hasMethod('writeToStage')) {
118
            $this->flushNow('... publishing: ' . $object->ClassName . '.' . $object->getTitle());
119
            $object->writeToStage(Versioned::DRAFT);
120
            $object->publishRecursive();
121
        } else {
122
            $this->flushNow('... writing: ' . $object->ClassName . '.' . $object->getTitle());
123
            $object->write();
124
        }
125
    }
126
}
127