JobController::JobApp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 19
ccs 8
cts 8
cp 1
crap 1
rs 9.9332
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\ArrayData;
17
use SilverStripe\View\ViewableData_Customised;
18
19
/**
20
 * Class JobController
21
 * @package Dynamic\Jobs\Model
22
 */
23
class JobController extends PageController
24
{
25
    /**
26
     * @var array
27
     */
28
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
29
        'apply',
30
        'JobApp',
31
        'complete',
32
    ];
33
34
    /**
35
     * @return ViewableData_Customised
36
     */
37 2
    public function apply()
38
    {
39 2
        $Form = $this->JobApp();
40
41 2
        $Form->Fields()->insertBefore(
42 2
            ReadonlyField::create(
43 2
                'PositionName',
44 2
                'Position',
45 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

45
                $this->/** @scrutinizer ignore-call */ 
46
                       getTitle()
Loading history...
46
            ),
47 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

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

86
    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...
87
    {
88 1
        $entry = new JobSubmission();
89 1
        $form->saveInto($entry);
90
91 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...
92
93
        // adds relation to uploaded file
94
        /** @var FileField $fileField */
95 1
        $fileField = $form->Fields()->fieldByName('Resume');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $fileField is correct as $form->Fields()->fieldByName('Resume') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96 1
        if ($fileField !== null) {
97 1
            $file = $fileField->getUpload()->getFile();
98 1
            if ($file !== null && $file->exists()) {
99
                $file->ShowInSearch = 0;
0 ignored issues
show
Bug introduced by
Accessing ShowInSearch on the interface SilverStripe\Assets\Storage\AssetContainer suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
100
                $file->write();
101
                $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...
102
            }
103
        }
104
105 1
        if ($entry->write()) {
106 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

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