Passed
Pull Request — master (#11)
by Simon
01:34
created

SlackSignupForm   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A submitSlackForm() 0 8 1
A getFormActions() 0 8 2
A __construct() 0 19 4
B getFormFields() 0 25 2
A redirectSlack() 0 15 4
1
<?php
2
3
namespace Firesphere\StripeSlack\Form;
4
5
use Firesphere\StripeSlack\Model\SlackInvite;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\Forms\EmailField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\Forms\TextField;
14
use SilverStripe\SiteConfig\SiteConfig;
15
16
class SlackSignupForm extends Form
17
{
18
    private $siteConfig;
19
20
    /**
21
     * SlackSignupForm constructor.
22
     * @param Controller $controller
23
     * @param string $name
24
     * @param FieldList $fields
25
     * @param FieldList $actions
26
     * @param null $validator
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $validator is correct as it would always require null to be passed?
Loading history...
27
     */
28
    public function __construct(
29
        Controller $controller = null,
30
        $name,
31
        FieldList $fields = null,
32
        FieldList $actions = null,
33
        $validator = null
34
    ) {
35
        $this->siteConfig = SiteConfig::current_site_config();
36
        if (!$controller) {
37
            $controller = Controller::curr();
38
        }
39
        if (!$fields) {
40
            $fields = $this->getFormFields();
41
        }
42
        if (!$actions) {
43
            $actions = $this->getFormActions();
44
        }
45
46
        parent::__construct($controller, $name, $fields, $actions, $validator);
47
    }
48
49
    /**
50
     * @return FieldList
51
     */
52
    protected function getFormFields()
53
    {
54
        if (!$this->siteConfig->SlackToken) {
55
            return FieldList::create([
56
                LiteralField::create(
57
                    'Setup',
0 ignored issues
show
Bug introduced by
'Setup' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                    /** @scrutinizer ignore-type */ 'Setup',
Loading history...
58
                    _t('SlackSignupForm.Setup', 'StripeSlack has not yet been configured correctly')
59
                )
60
            ]);
61
        }
62
        $fields = FieldList::create(
63
            [
64
                LiteralField::create(
65
                    'Intro',
66
                    _t('SlackSignupForm.Intro', 'Fill out the form below to request access to Slack')
67
                ),
68
                TextField::create('Name', _t('SlackSignupForm.Name', 'My name is')),
69
                EmailField::create('Email', _t('SlackSignupForm.Email', 'My email address is'))
70
                    ->setAttribute('required', true),
71
            ]
72
        );
73
74
        $this->extend('updateFormFields', $fields);
75
76
        return $fields;
77
    }
78
79
    /**
80
     * @return FieldList
81
     */
82
    protected function getFormActions()
83
    {
84
        if (!$this->siteConfig->SlackToken) {
85
            return FieldList::create();
86
        }
87
88
        return FieldList::create([
89
            FormAction::create('submitSlackForm', _t('SlackSignupForm.Submit', 'Submit'))
0 ignored issues
show
Bug introduced by
'submitSlackForm' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            FormAction::create(/** @scrutinizer ignore-type */ 'submitSlackForm', _t('SlackSignupForm.Submit', 'Submit'))
Loading history...
90
        ]);
91
    }
92
93
    /**
94
     * @param array $data
95
     * @param SlackSignupForm $form
96
     */
97
    protected function submitSlackForm($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

97
    protected function submitSlackForm(/** @scrutinizer ignore-unused */ $data, $form)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        $signup = SlackInvite::create();
100
        $form->saveInto($signup);
101
        $userID = $signup->write();
102
        /** @var SlackInvite $signup We need to re-fetch from the database after writing */
103
        $signup = SlackInvite::get()->byID($userID);
104
        $this->redirectSlack($signup->Invited);
105
    }
106
107
    /**
108
     * This method seems long, but it's primarily switching between CMS and normal user
109
     * Plus a check if the URL's are set on the config.
110
     *
111
     * @param boolean $success
112
     * @return bool|HTTPResponse
113
     */
114
    public function redirectSlack($success)
115
    {
116
        $config = SiteConfig::current_site_config();
117
        if (!$success) {
118
            if ($config->SlackErrorBackURLID) {
119
                return $this->controller->redirect($config->SlackErrorBackURL()->Link());
120
            }
121
122
            return $this->controller->redirect($this->controller->Link('oops'));
123
        }
124
        if ($config->SlackBackURLID) {
125
            return $this->controller->redirect($config->SlackBackURL()->Link());
126
        }
127
128
        return $this->controller->redirect($this->controller->Link('success'));
129
    }
130
}
131