Passed
Push — 1.6 ( c1892f...19716f )
by Robbie
02:51
created

PopulateThemeSampleDataTask::createContactForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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