Passed
Push — 1.x-dev ( bca2f1...8e6f2b )
by Brian
02:53
created

AppController::add()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11.2481

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
c 2
b 0
f 0
nc 11
nop 0
dl 0
loc 31
ccs 9
cts 19
cp 0.4737
crap 11.2481
rs 9.0111
1
<?php
2
3
/**
4
 * AppController
5
 */
6
7
namespace Fr3nch13\Jira\Controller;
8
9
use App\Controller\AppController as BaseController;
10
use Fr3nch13\Jira\Form\AppForm as JiraForm;
11
12
/**
13
 * App Controller
14
 *
15
 * The base controller for the jira plugin.
16
 *
17
 * -----------------------------
18
 * Inherited:
19
 *
20
 * {@inheritdoc}
21
 *
22
 */
23
class AppController extends BaseController
24
{
25
    /**
26
     * Human name of this object.
27
     * @var string
28
     */
29
    public $humanName = '';
30
31
    /**
32
     * The form object.
33
     * @var object
34
     */
35
    public $JiraForm;
36
37
    /**
38
     * Initialize method
39
     *
40
     * @return void
41
     */
42 2
    public function initialize(): void
43
    {
44 2
        parent::initialize();
45
46 2
        $this->modelClass = false;
47
48 2
        $this->humanName = __('Task');
49 2
        $this->JiraForm = new JiraForm();
50 2
    }
51
52
    /**
53
     * The html form.
54
     *
55
     * @return \Cake\Http\Response|null Redirects on success.
56
     */
57 1
    public function add(): ?\Cake\Http\Response
58
    {
59 1
        $errors = [];
60 1
        if ($this->getRequest()->is('post')) {
61
            $data = $this->getRequest()->getData();
62
            if ($this->getRequest()->getQuery()) {
63
                $query = $this->getRequest()->getQuery();
64
                if (isset($query['referer'])) {
65
                    $data['referer'] = $query['referer'];
66
                }
67
            }
68
            if ($this->JiraForm->execute($data)) {
69
                $this->Flash->success(__('The {0} has been saved.', [$this->humanName]));
70
71
                return $this->redirect(['action' => 'thankyou', '?' => ['type' => $this->humanName]]);
72
            } else {
73
                $errors = $this->JiraForm->getErrors();
74
                $this->Flash->error(__('There was a problem saving the {0}.', [$this->humanName]));
75
            }
76
        }
77
78 1
        if ($this->getRequest()->is('get')) {
79 1
            $this->JiraForm->setData($this->JiraForm->getFormData());
80
        }
81
82 1
        $this->set([
83 1
            'form' => $this->JiraForm,
84 1
            'errors' => $errors,
85
        ]);
86
87 1
        return null;
88
    }
89
90
    /**
91
     * The thank you page after they've submitted their report.
92
     *
93
     * @return void
94
     */
95 1
    public function thankyou(): void
96
    {
97
        //
98 1
    }
99
}
100