Passed
Push — master ( 789ab6...490312 )
by Brian
01:34 queued 11s
created

AppForm::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
c 2
b 0
f 0
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
     *  Defaults to a new instance.
46
     * @return void
47
     */
48 2
    public function __construct(EventManager $eventManager = null)
49
    {
50 2
        parent::__construct($eventManager);
51
52 2
        $this->JiraProject = new JiraProject();
53
54 2
        if (!empty($this->settings)) {
55
            $this->JiraProject->modifyAllowedTypes($this->issueType, $this->settings);
56
        }
57
58
        /** @scrutinizer ignore-call */
59 2
        $formData = $this->getFormData($this->issueType);
60 2
        $this->setFormData($formData);
61 2
    }
62
63
    /**
64
     * Defines the schema from the JiraProject Object.
65
     *
66
     * @param \Cake\Form\Schema $schema The existing schema.
67
     * @return \Cake\Form\Schema The modified schema.
68
     */
69 1
    protected function _buildSchema(Schema $schema)
70
    {
71 1
        $data = $this->getFormData();
72 1
        if (!isset($data['fields'])) {
73
            throw new Exception(__('Missing the fields.'));
74
        }
75 1
        foreach ($data['fields'] as $k => $v) {
76 1
            $schema->addField($k, $v);
77
        }
78
79 1
        return $schema;
80
    }
81
82
    /**
83
     * Defines the validations
84
     *
85
     * @param \Cake\Validation\Validator $validator The existing validator.
86
     * @return \Cake\Validation\Validator The modified validator.
87
     */
88 1
    protected function _buildValidator(Validator $validator)
89
    {
90 1
        $data = $this->getFormData();
91 1
        if (!isset($data['fields'])) {
92
            throw new Exception(__('Missing the fields.'));
93
        }
94 1
        foreach ($data['fields'] as $k => $v) {
95 1
            if ($v['type'] == 'string' || $v['type'] == 'text') {
96 1
                $validator->scalar($k);
97
            }
98 1
            if ($v['type'] == 'email') {
99
                $validator->email($k);
100
            }
101 1
            if ($v['type'] == 'boolean') {
102 1
                $validator->boolean($k);
103
            }
104
        }
105
106 1
        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 int|bool True is the issue was submitted or false if there was an problem.
114
     */
115
    protected function _execute(array $data = [])
116
    {
117
        try {
118
            /** @scrutinizer ignore-call */
119
            $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
        return $result;
132
    }
133
134
    /**
135
     * Sets the formData variable.
136
     *
137
     * @param array $data The array of data.
138
     * @return void
139
     */
140 2
    public function setFormData(array $data = [])
141
    {
142 2
        $this->JiraProject->setFormData($this->issueType, $data);
143 2
    }
144
145
    /**
146
     * Gets the formData variable.
147
     *
148
     * @return array The array of the current form data.
149
     */
150 2
    public function getFormData()
151
    {
152 2
        return $this->JiraProject->getFormData($this->issueType);
153
    }
154
}
155