Completed
Branch master (8e0976)
by Adam
04:13
created

ApplicationForm::buildForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 65
rs 8.7636
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 Coyote\Http\Forms\Job;
4
5
use Coyote\Services\FormBuilder\Form;
6
use Coyote\Services\FormBuilder\FormEvents;
7
use Coyote\Services\FormBuilder\ValidatesWhenSubmitted;
8
9
class ApplicationForm extends Form implements ValidatesWhenSubmitted
10
{
11
    /**
12
     * @var array
13
     */
14
    private $salaryChoices = [
15
        'od 1000 zł m-c',
16
        'od 2000 zł m-c',
17
        'od 3000 zł m-c',
18
        'od 4000 zł m-c',
19
        'od 5000 zł m-c',
20
        'od 6000 zł m-c',
21
        'od 7000 zł m-c',
22
        'od 8000 zł m-c',
23
        'od 9000 zł m-c',
24
        'od 10000 zł m-c',
25
    ];
26
27
    /**
28
     * @var array
29
     */
30
    private $dismissalPeriodChoices = [
31
        'Brak',
32
        '3 dni robocze',
33
        '1 tydzień',
34
        '2 tygodnie',
35
        '1 miesiąc',
36
        '3 miesiące'
37
    ];
38
39
    /**
40
     * @var string
41
     */
42
    protected $theme = self::THEME_INLINE;
43
44
    /**
45
     * It's public so we can use use attr from twig
46
     *
47
     * @var array
48
     */
49
    public $attr = [
50
        'method' => self::POST,
51
        'id' => 'job-application',
52
        'enctype' => 'multipart/form-data'
53
    ];
54
55
    public function __construct()
56
    {
57
        parent::__construct();
58
59
        $this->addEventListener(FormEvents::PRE_RENDER, function (Form $form) {
60
            if ($form->request->session()->getOldInput('cv')) {
0 ignored issues
show
Bug introduced by
The method getOldInput() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
                $name = explode('_', $form->get('cv')->getValue(), 2)[1];
62
63
                $attr = $form->get('cv')->getAttr();
64
                $form->get('cv')->setAttr(array_merge($attr, ['placeholder' => $name]));
65
            }
66
        });
67
68 View Code Duplication
        $this->addEventListener(FormEvents::POST_SUBMIT, function (Form $form) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $github = $form->get('github')->getValue();
70
71
            if ($github) {
72
                if (filter_var($github, FILTER_VALIDATE_URL) === false) {
73
                    $form->get('github')->setValue('https://github.com/' . $github);
74
                }
75
            }
76
        });
77
    }
78
79
    public function buildForm()
80
    {
81
        $this
82
            ->add('email', 'email', [
83
                'rules' => 'required|string|max:200|email',
84
                'label' => 'E-mail',
85
                'help' => 'Nie wysyłamy spamu! Obiecujemy.',
86
                'attr' => [
87
                    'placeholder' => 'Np. [email protected]'
88
                ]
89
            ])
90
            ->add('email_confirmation', 'honeypot')
91
            ->add('name', 'text', [
92
                'rules' => 'required|string|max:50',
93
                'label' => 'Imię i nazwisko'
94
            ])
95
            ->add('phone', 'text', [
96
                'rules'  => 'string|max:50',
97
                'label' => 'Numer telefonu',
98
                'help' => 'Podanie numeru telefonu nie jest obowiązkowe, ale pozwoli na szybki kontakt.'
99
            ])
100
            ->add('cv', 'hidden', [
101
                'label' => 'CV/Resume',
102
                'help' => 'CV/résumé z rozszerzeniem *.pdf, *.doc, *.docx lub *.rtf. Maksymalnie 5 MB.',
103
                'attr' => [
104
                    'placeholder' => 'Kliknij, aby dodać załącznik',
105
                    'id' => 'uploader',
106
                    'class' => 'form-control',
107
                    'data-upload-url' => route('job.application.upload')
108
                ],
109
                'template' => 'uploader'
110
            ])
111
            ->add('github', 'text', [
112
                'rules' => 'string|max:200',
113
                'label' => 'Konto Github',
114
                'help' => 'Nazwa użytkownika lub link do konta Github.',
115
                'row_attr' => [
116
                    'class' => 'github'
117
                ]
118
            ])
119
            ->add('salary', 'select', [
120
                'label' => 'Minimalne oczekiwania wynagrodzenie',
121
                'empty_value' => 'Do negocjacji',
122
                'choices' => array_combine($this->salaryChoices, $this->salaryChoices)
123
            ])
124
            ->add('dismissal_period', 'select', [
125
                'label' => 'Obecny okres wypowiedzenia',
126
                'empty_value' => 'Nie określono',
127
                'choices' => array_combine($this->dismissalPeriodChoices, $this->dismissalPeriodChoices)
128
            ])
129
            ->add('text', 'textarea', [
130
                'rules' => 'string|required|max:5000',
131
                'label' => 'Wiadomość dla pracodawcy/zleceniodawcy',
132
                'help' => 'Taką wiadomość otrzyma osoba, która wystawiła ogłoszenie'
133
            ])
134
            ->add('remember', 'checkbox', [
135
                'label' => 'Zapamiętaj dane podane w formularzu'
136
            ])
137
            ->add('submit', 'submit', [
138
                'label' => 'Zapisz',
139
                'attr' => [
140
                    'data-submit-state' => 'Wysyłanie...'
141
                ]
142
            ]);
143
    }
144
}
145