Passed
Pull Request — master (#37)
by Brian
05:49 queued 02:39
created

AppForm   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 72.09%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 135
ccs 31
cts 43
cp 0.7209
rs 10
c 3
b 0
f 0
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _buildSchema() 0 11 3
B validationDefault() 0 22 9
A __construct() 0 13 3
A _execute() 0 21 4
A setFormData() 0 3 1
A getFormData() 0 3 1
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
     * @var \Fr3nch13\Jira\Lib\JiraProject Contains the loaded Jira Project object.
26
     */
27
    protected $JiraProject;
28
29
    /**
30
     * @var string The type of issue we're submitting.
31
     */
32
    public $issueType = 'Task';
33
34
    /**
35
     * @var array Settings for this form and for the JiraProject.
36
     */
37
    public $settings = [];
38
39
    /**
40
     * Constructor
41
     *
42
     * @param \Cake\Event\EventManager|null $eventManager The event manager.
43
     *  Defaults to a new instance.
44
     */
45 8
    public function __construct(?EventManager $eventManager = null)
46
    {
47 8
        if ($eventManager !== null) {
48
            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
    }
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
    public function validationDefault(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
            /** @scrutinizer ignore-call */
119 1
            $result = $this->JiraProject->submitIssue($this->issueType, $data);
120
        } catch (Exception $e) {
121
            /** @scrutinizer ignore-call */
122
            $errors = $this->JiraProject->getErrors();
123
            foreach ($errors as $k => $v) {
124
                // track the errors specific to jira/the JiraProject object.
125
                $this->setErrors(['jira' => [$k => $v]]);
126
            }
127
128
            return false;
129
        }
130
131 1
        if ($result > 0) {
132 1
            return true;
133
        }
134
135
        return false;
136
    }
137
138
    /**
139
     * Sets the formData variable.
140
     *
141
     * @param array $data The array of data.
142
     * @return void
143
     */
144 8
    public function setFormData(array $data = []): void
145
    {
146 8
        $this->JiraProject->setFormData($this->issueType, $data);
147
    }
148
149
    /**
150
     * Gets the formData variable.
151
     *
152
     * @return array The array of the current form data.
153
     */
154 8
    public function getFormData(): array
155
    {
156 8
        return $this->JiraProject->getFormData($this->issueType);
157
    }
158
}
159