ContactController::index()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 61
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 61
rs 9.5147
cc 3
eloc 39
nc 3
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
namespace App\Controller;
3
4
use Cake\Core\Configure;
5
use Cake\Event\Event;
6
use Cake\Mailer\MailerAwareTrait;
7
use Cake\Validation\Validator;
8
9
class ContactController extends AppController
10
{
11
    use MailerAwareTrait;
12
13
    /**
14
     * Beforefilter.
15
     *
16
     * @param Event $event The beforeFilter event that was fired.
17
     *
18
     * @return void
19
     */
20
    public function beforeFilter(Event $event)
21
    {
22
        parent::beforeFilter($event);
23
24
        $this->Auth->allow(['index']);
25
    }
26
27
    /**
28
     * Contact page.
29
     *
30
     * @return \Cake\Network\Response|void
31
     */
32
    public function index()
33
    {
34
        $contact = [
35
            'schema' => [
36
                'name' => [
37
                    'type' => 'string',
38
                    'length' => 100
39
                ],
40
                'email' => [
41
                    'type' => 'email',
42
                    'length' => 100
43
                ],
44
                'subject' => [
45
                    'type' => 'string',
46
                    'length' => 255
47
                ],
48
                'message' => [
49
                    'type' => 'string'
50
                ]
51
            ],
52
            'required' => [
53
                'name' => 1,
54
                'email' => 1,
55
                'message' => 1
56
            ]
57
        ];
58
59
        if ($this->request->is('post')) {
60
            $validator = new Validator();
61
            $validator
62
                ->notEmpty('email', __('You need to put your E-mail.'))
63
                ->add('email', 'validFormat', [
64
                    'rule' => 'email',
65
                    'message' => __("You must specify a valid E-mail address.")
66
                ])
67
                ->notEmpty('name', __('You need to put your name.'))
68
                ->notEmpty('message', __("You need to give a message."))
69
                ->add('message', 'minLength', [
70
                    'rule' => ['minLength', 10],
71
                    'message' => __("Your message can not contain less than {0} characters.", 10)
72
                ]);
73
74
            $contact['errors'] = $validator->errors($this->request->getParsedBody());
0 ignored issues
show
Bug introduced by
It seems like $this->request->getParsedBody() targeting Cake\Http\ServerRequest::getParsedBody() can also be of type null or object; however, Cake\Validation\Validator::errors() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
75
76
            if (empty($contact['errors'])) {
77
                $viewVars = [
78
                    'ip' => $this->request->clientIp()
79
                ];
80
81
                $viewVars = array_merge($this->request->getParsedBody(), $viewVars);
82
83
                $this->getMailer('Contact')->send('contact', [$viewVars]);
84
85
                $this->Flash->success(__("Your message has been sent successfully, you will get a response shortly !"));
86
87
                return $this->redirect(['controller' => 'pages', 'action' => 'home']);
88
            }
89
        }
90
91
        $this->set(compact('contact'));
92
    }
93
}
94