Passed
Push — master ( 05e846...9de07a )
by Jason
02:44
created

JobController::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Jobs\Page;
4
5
use Dynamic\Jobs\Model\JobSubmission;
6
use PageController;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\Email\Email;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FileField;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\Forms\FormAction;
13
use SilverStripe\Forms\HiddenField;
14
use SilverStripe\Forms\ReadonlyField;
15
use SilverStripe\Forms\RequiredFields;
16
use SilverStripe\View\ViewableData_Customised;
17
18
/**
19
 * Class JobController
20
 * @package Dynamic\Jobs\Model
21
 */
22
class JobController extends PageController
23
{
24
    /**
25
     * @var array
26
     */
27
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
28
        'apply',
29
        'JobApp',
30
        'complete',
31
    ];
32
33
    /**
34
     * @return ViewableData_Customised
35
     */
36 2
    public function apply()
37
    {
38 2
        $Form = $this->JobApp();
39
40 2
        $Form->Fields()->insertBefore(
41 2
            ReadonlyField::create(
42 2
                'PositionName',
43 2
                'Position',
44 2
                $this->getTitle()
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on Dynamic\Jobs\Page\JobController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

44
                $this->/** @scrutinizer ignore-call */ 
45
                       getTitle()
Loading history...
45
            ),
46 2
            'FirstName'
0 ignored issues
show
Bug introduced by
'FirstName' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

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

46
            /** @scrutinizer ignore-type */ 'FirstName'
Loading history...
47
        );
48 2
        $Form->Fields()->push(HiddenField::create('JobID', 'JobID', $this->ID));
49
50 2
        $page = $this->customise([
51 2
            'Form' => $Form,
52
        ]);
53
54 2
        return $page;
55
    }
56
57
    /**
58
     * @return Form
59
     */
60 3
    public function JobApp()
61
    {
62 3
        $App = singleton(JobSubmission::class);
63
64 3
        $fields = $App->getFrontEndFields();
65
66 3
        $actions = FieldList::create(
67 3
            new FormAction('doApply', 'Apply')
68
        );
69
70 3
        $required = new RequiredFields([
71 3
            'FirstName',
72
            'LastName',
73
            'Email',
74
            'Phone',
75
        ]);
76
77
78 3
        return Form::create($this, "JobApp", $fields, $actions, $required);
79
    }
80
81
    /**
82
     * @param $data
83
     * @param $form
84
     */
85 1
    public function doApply(array $data, Form $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

85
    public function doApply(/** @scrutinizer ignore-unused */ array $data, Form $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...
86
    {
87 1
        $entry = new JobSubmission();
88 1
        $form->saveInto($entry);
89
90 1
        $entry->JobID = $this->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property JobID does not exist on Dynamic\Jobs\Model\JobSubmission. Since you implemented __set, consider adding a @property annotation.
Loading history...
91
92
        // adds relation to uploaded file
93
        /** @var FileField $fileField */
94 1
        $fileField = $form->Fields()->fieldByName('Resume');
95 1
        if ($fileField !== null) {
96 1
            $file = $fileField->getUpload()->getFile();
97 1
            if ($file !== null && $file->exists()) {
98
                $entry->ResumeID = $file->ID;
0 ignored issues
show
Bug introduced by
Accessing ID on the interface SilverStripe\Assets\Storage\AssetContainer suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug Best Practice introduced by
The property ResumeID does not exist on Dynamic\Jobs\Model\JobSubmission. Since you implemented __set, consider adding a @property annotation.
Loading history...
99
            }
100
        }
101
102 1
        if ($entry->write()) {
103 1
            $to = $this->parent()->EmailRecipient;
0 ignored issues
show
Bug introduced by
The method parent() does not exist on Dynamic\Jobs\Page\JobController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

103
            $to = $this->/** @scrutinizer ignore-call */ parent()->EmailRecipient;
Loading history...
104 1
            $from = $this->parent()->FromAddress;
105 1
            $subject = $this->parent()->EmailSubject;
106 1
            $body = $this->parent()->EmailMessage;
107
108 1
            $email = new Email($from, $to, $subject, $body);
109 1
            $email->setHTMLTemplate('Dynamic\Jobs\Email\JobSubmission')
110 1
                ->setData(
111 1
                    JobSubmission::get()
112 1
                        ->byID($entry->ID)
113
                );
114
115 1
            $email->send();
116
117 1
            $this->redirect(Controller::join_links($this->Link(), 'complete'));
118
        }
119
    }
120
121
    /**
122
     * @return ViewableData_Customised
123
     */
124 1
    public function complete()
125
    {
126 1
        return $this->customise([]);
127
    }
128
}
129