AppForm::_buildSchema()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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