Passed
Pull Request — master (#18)
by Brian
05:45
created

AppForm::_buildSchema()   A

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