Passed
Push — 1.x-dev ( bd0048...5a7a02 )
by
unknown
03:10
created

AppController::add()   B

Complexity

Conditions 10
Paths 27

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 25.6155

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 26
c 2
b 0
f 0
nc 27
nop 0
dl 0
loc 41
ccs 12
cts 26
cp 0.4615
crap 25.6155
rs 7.6666

How to fix   Complexity   

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
/**
4
 * AppController
5
 */
6
7
namespace Fr3nch13\Jira\Controller;
8
9
use App\Controller\AppController as BaseController;
10
use Cake\Core\Configure;
11
use Fr3nch13\Jira\Form\AppForm as JiraForm;
12
13
/**
14
 * App Controller
15
 *
16
 * The base controller for the jira plugin.
17
 *
18
 * -----------------------------
19
 * Inherited:
20
 *
21
 * {@inheritdoc}
22
 *
23
 * @property \Cake\Controller\Component\AuthComponent $Auth
24
 */
25
class AppController extends BaseController
26
{
27
    /**
28
     * Human name of this object.
29
     * @var string
30
     */
31
    public $humanName = '';
32
33
    /**
34
     * The form object.
35
     * @var object
36
     */
37
    public $JiraForm;
38
39
    /**
40
     * Initialize method
41
     *
42
     * @return void
43
     */
44 2
    public function initialize(): void
45
    {
46 2
        parent::initialize();
47
48 2
        $this->loadComponent('RequestHandler');
49 2
        if (isset($this->Auth)) {
50
            $this->Auth->allow(['add', 'thankyou']);
51
        }
52
53 2
        $this->modelClass = false;
54
55 2
        $this->humanName = __('Task');
56 2
        $this->JiraForm = new JiraForm();
57 2
    }
58
59
    /**
60
     * The html form.
61
     *
62
     * @return \Cake\Http\Response|null Redirects on success.
63
     */
64 1
    public function add(): ?\Cake\Http\Response
65
    {
66 1
        $errors = [];
67 1
        if ($this->getRequest()->is('post')) {
68
            $data = $this->getRequest()->getData();
69
            if ($this->getRequest()->getQuery()) {
70
                $query = $this->getRequest()->getQuery();
71
                if (isset($query['referer'])) {
72
                    $data['referer'] = $query['referer'];
73
                }
74
            }
75
            if ($this->JiraForm->execute($data)) {
76
                $this->Flash->success(__('The {0} has been saved.', [$this->humanName]));
77
78
                return $this->redirect(['action' => 'thankyou', '?' => ['type' => $this->humanName]]);
79
            } else {
80
                $errors = $this->JiraForm->getErrors();
81
                $this->Flash->error(__('There was a problem saving the {0}.', [$this->humanName]));
82
            }
83
        }
84
85 1
        if ($this->getRequest()->is('get')) {
86 1
            $this->JiraForm->setData($this->JiraForm->getFormData());
87
        }
88
89 1
        $username = null;
90 1
        if (isset($this->Auth) && $this->Auth->user() && Configure::read('Jira.usernameField')) {
91
            $user = $this->Auth->user();
92
            $usernameField = Configure::read('Jira.usernameField');
93
            if (isset($user->{$usernameField})) {
94
                $username = $user->{$usernameField};
95
            }
96
        }
97
98 1
        $this->set([
99 1
            'form' => $this->JiraForm,
100 1
            'errors' => $errors,
101 1
            'username' => $username,
102
        ]);
103
104 1
        return null;
105
    }
106
107
    /**
108
     * The thank you page after they've submitted their report.
109
     *
110
     * @return void
111
     */
112 1
    public function thankyou(): void
113
    {
114
        //
115 1
    }
116
}
117