Passed
Push — 1.x-dev ( 6e580d...1e4c8d )
by Brian
03:23
created

AppForm::_execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.1971

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 15
ccs 3
cts 8
cp 0.375
crap 5.1971
rs 10
1
<?php
2
/**
3
 * AppForm
4
 */
5
6
namespace Fr3nch13\Jira\Form;
7
8
use Cake\Event\EventManager;
9
use Cake\Form\Form;
10
use Cake\Form\Schema;
11
use Cake\Validation\Validator;
12
use Fr3nch13\Jira\Exception\Exception;
13
use Fr3nch13\Jira\Lib\JiraProject;
14
15
/**
16
 * App Form
17
 *
18
 * Used to submit an issue to Jira.
19
 */
20
class AppForm extends Form
21
{
22
    /**
23
     * Contains the loaded Jira Project object.
24
     *
25
     * @var \Fr3nch13\Jira\Lib\JiraProject
26
     */
27
    protected $JiraProject;
28
29
    /**
30
     * The type of issue we're submitting.
31
     * @var string
32
     */
33
    public $issueType = 'Task';
34
35
    /**
36
     * Settings for this form and for the JiraProject.
37
     * @var array
38
     */
39
    public $settings = [];
40
41
    /**
42
     * Constructor
43
     *
44
     * @param \Cake\Event\EventManager|null $eventManager The event manager.
45
     */
46 8
    public function __construct(EventManager $eventManager = null)
47
    {
48 8
        parent::__construct($eventManager);
49
50 8
        $this->JiraProject = new JiraProject();
51
52 8
        if (!empty($this->settings)) {
53
            $this->JiraProject->modifyAllowedTypes($this->issueType, $this->settings);
54
        }
55
56 8
        $formData = $this->getFormData();
57 8
        $this->setFormData($formData);
58 8
    }
59
60
    /**
61
     * Defines the schema from the JiraProject Object.
62
     *
63
     * @param \Cake\Form\Schema $schema The existing schema.
64
     * @return \Cake\Form\Schema The modified schema.
65
     */
66 2
    protected function _buildSchema(Schema $schema): Schema
67
    {
68 2
        $data = $this->getFormData();
69 2
        if (!isset($data['fields'])) {
70
            throw new Exception(__('Missing the fields.'));
71
        }
72 2
        foreach ($data['fields'] as $k => $v) {
73 2
            $schema->addField($k, $v);
74
        }
75
76 2
        return $schema;
77
    }
78
79
    /**
80
     * Defines the validations
81
     *
82
     * @param \Cake\Validation\Validator $validator The existing validator.
83
     * @return \Cake\Validation\Validator The modified validator.
84
     */
85 3
    protected function _buildValidator(Validator $validator): Validator
86
    {
87 3
        $data = $this->getFormData();
88 3
        if (!isset($data['fields'])) {
89
            throw new Exception(__('Missing the fields.'));
90
        }
91 3
        foreach ($data['fields'] as $k => $v) {
92 3
            if ($v['type'] == 'string' || $v['type'] == 'text') {
93 3
                $validator->scalar($k);
94
            }
95 3
            if ($v['type'] == 'email') {
96
                $validator->email($k);
97
            }
98 3
            if ($v['type'] == 'boolean') {
99
                $validator->boolean($k);
100
            }
101 3
            if (isset($v['required']) && $v['required'] === true) {
102 3
                $validator->requirePresence($k);
103
            }
104
        }
105
106 3
        return $validator;
107
    }
108
109
    /**
110
     * Submit the issue to Jira.
111
     *
112
     * @param array $data The array of post data from the form template.
113
     * @return bool True is the issue was submitted or false if there was an problem.
114
     */
115 1
    protected function _execute(array $data = []): bool
116
    {
117
        try {
118 1
            $result = $this->JiraProject->submitIssue($this->issueType, $data);
119
        } catch (Exception $e) {
120
            $errors = $this->JiraProject->getErrors();
121
            foreach ($errors as $k => $v) {
122
                // track the errors specific to jira/the JiraProject object.
123
                $this->setErrors(["jira" => [$k => $v]]);
124
            }
125
126
            return false;
127
        }
128
129 1
        return $result;
130
    }
131
132
    /**
133
     * Sets the formData variable.
134
     *
135
     * @param array $data The array of data.
136
     * @return void
137
     */
138 8
    public function setFormData(array $data = []): void
139
    {
140 8
        $this->JiraProject->setFormData($this->issueType, $data);
141 8
    }
142
143
    /**
144
     * Gets the formData variable.
145
     *
146
     * @return array The array of the current form data.
147
     */
148 8
    public function getFormData(): array
149
    {
150 8
        return $this->JiraProject->getFormData($this->issueType);
151
    }
152
}
153