PopulateThemeSampleDataTask::createContactForm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 36
nc 2
nop 0
dl 0
loc 54
rs 9.344
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CWP\CWP\Tasks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ield\EditableEmailField 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\EditableFormField\EditableFormStep;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...mField\EditableFormStep 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\UserForms\Model\EditableFormField\EditableTextField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...Field\EditableTextField 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
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...
13
use SilverStripe\Versioned\Versioned;
14
15
/**
16
 * Used to populate sample data when installing the starter or Wātea theme
17
 */
18
class PopulateThemeSampleDataTask extends BuildTask
19
{
20
    protected $title = 'Populate sample data for theme demo';
21
22
    protected $description = 'Populates some sample data for showcasing the functionality of the '
23
        . 'starter and Wātea themes';
24
25
    /**
26
     * A series of method calls to create sample data
27
     *
28
     * @param HTTPRequest $request
29
     */
30
    public function run($request)
31
    {
32
        if (!class_exists(UserDefinedForm::class)) {
33
            return;
34
        }
35
        $this->handleContactForm();
36
    }
37
38
    /**
39
     * Decide whether to create a contact user defined form, and call it to be be created if so
40
     *
41
     * @return $this
42
     */
43
    protected function handleContactForm()
44
    {
45
        if (!$this->getContactFormExists()) {
46
            $this->createContactForm();
47
        }
48
        return $this;
49
    }
50
51
    /**
52
     * Determine whether a "contact us" userform exists yet
53
     *
54
     * @return bool
55
     */
56
    protected function getContactFormExists()
57
    {
58
        $exists = false;
59
        foreach (UserDefinedForm::get()->column('ID') as $formId) {
60
            $count = Versioned::get_all_versions(UserDefinedForm::class, $formId)
61
                ->filter('URLSegment', 'contact')
62
                ->count();
63
64
            if ($count >= 1) {
65
                $exists = true;
66
                break;
67
            }
68
        }
69
        return $exists;
70
    }
71
72
    /**
73
     * Create a "contact us" userform. Please note that this form does not have any recipients by default, so
74
     * no emails will be sent. To add recipients - edit the page in the CMS and add a recipient via the "Recipients"
75
     * tab.
76
     *
77
     * @return $this
78
     */
79
    protected function createContactForm()
80
    {
81
        $form = UserDefinedForm::create(array(
82
            'Title' => 'Contact',
83
            'URLSegment' => 'contact',
84
            'Content' => '<p>$UserDefinedForm</p>',
85
            'SubmitButtonText' => 'Submit',
86
            'ClearButtonText' => 'Clear',
87
            'OnCompleteMessage' => "<p>Thanks, we've received your submission and will be in touch shortly.</p>",
88
            'EnableLiveValidation' => true
89
        ));
90
91
        $form->write();
92
93
        // Add form fields
94
        $fields = array(
95
            EditableFormStep::create([
96
                'Title' => _t(
97
                    'SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFormStep.TITLE_FIRST',
98
                    'First Page'
99
                )
100
            ]),
101
            EditableTextField::create([
102
                'Title' => 'Name',
103
                'Required' => true,
104
                'RightTitle' => 'Please enter your first and last name'
105
            ]),
106
            EditableEmailField::create([
107
                'Title' => Email::class,
108
                'Required' => true,
109
                'Placeholder' => '[email protected]'
110
            ]),
111
            EditableTextField::create([
112
                'Title' => 'Subject'
113
            ]),
114
            EditableTextField::create([
115
                'Title' => 'Message',
116
                'Required' => true,
117
                'Rows' => 5
118
            ])
119
        );
120
121
        foreach ($fields as $field) {
122
            $field->write();
123
            $form->Fields()->add($field);
124
            $field->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
125
        }
126
127
        $form->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
128
        $form->flushCache();
129
130
        $this->output(' + Created "contact" UserDefinedForm page');
131
132
        return $this;
133
    }
134
135
    /**
136
     * Output a message either to the console or browser
137
     *
138
     * @param  string $message
139
     * @return $this
140
     */
141
    protected function output($message)
142
    {
143
        if (Director::is_cli()) {
144
            $message .= PHP_EOL;
145
        } else {
146
            $message = sprintf('<p>%s</p>', $message);
147
        }
148
        echo $message;
149
150
        return $this;
151
    }
152
}
153