Passed
Push — master ( f217ae...80b1d8 )
by Andreas
20:46
created

editor::get_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * @package midcom.grid
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\grid;
10
11
use Symfony\Component\HttpFoundation\InputBag;
12
use midcom_error;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
15
/**
16
 * Helper class for editable jqgrids
17
 *
18
 * @package midcom.grid
19
 */
20
class editor
21
{
22
    private string $operation;
23
24
    private $id;
25
26
    private array $data = [];
27
28 1
    public function __construct(InputBag $post, array $required_fields)
29
    {
30 1
        if (!$post->has('id') || !$post->has('oper')) {
31
            throw new midcom_error('Incomplete POST data');
32
        }
33
34 1
        $this->id = $post->get('id');
35 1
        $this->operation = $post->get('oper');
36
37 1
        if (!in_array($this->operation, ['edit', 'del'])) {
38
            throw new midcom_error('Invalid operation "' . $this->operation . '"');
39
        }
40
41 1
        foreach ($required_fields as $field) {
42 1
            if (!$post->has($field)) {
43
                throw new midcom_error('Incomplete POST data');
44
            }
45 1
            $this->data[$field] = $post->get($field);
46
        }
47
    }
48
49 1
    public function get_data() : array
50
    {
51 1
        return $this->data;
52
    }
53
54 1
    public function is_delete() : bool
55
    {
56 1
        return $this->operation == 'del';
57
    }
58
59 1
    public function get_id() : ?int
60
    {
61 1
        return str_starts_with($this->id, 'new_') ? null : (int) $this->id;
62
    }
63
64 1
    public function get_response(array $data) : JsonResponse
65
    {
66 1
        $data['oldid'] = $this->id;
67 1
        return new JsonResponse($data);
68
    }
69
}
70