Completed
Push — master ( 369b2c...ffb215 )
by Anton
8s
created

Crud::createOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Controller;
13
14
use Bluz\Application\Exception\BadRequestException;
15
use Bluz\Application\Exception\NotFoundException;
16
use Bluz\Application\Exception\NotImplementedException;
17
use Bluz\Proxy\Messages;
18
use Bluz\Proxy\Request;
19
use Bluz\Validator\Exception\ValidatorException;
20
21
/**
22
 * Crud controller
23
 *
24
 * @package  Bluz\Controller
25
 * @author   Anton Shevchuk
26
 * @link     https://github.com/bluzphp/framework/wiki/Controller-Crud
27
 */
28
class Crud extends AbstractController
29
{
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @return mixed
34
     * @throws NotImplementedException
35
     * @throws NotFoundException
36
     * @throws BadRequestException
37
     */
38 11
    public function __invoke()
39
    {
40 11
        $primary = $this->getPrimaryKey();
41
42
        // switch by method
43 11
        switch ($this->method) {
44 11
            case Request::METHOD_GET:
45
                $result = [
46 3
                    'row' => $this->readOne($primary),
47
                    'method' => empty($primary)?Request::METHOD_POST:Request::METHOD_PUT
48
                ];
49 2
                break;
50 8
            case Request::METHOD_POST:
51
                try {
52
                    // Result is Primary Key(s)
53 2
                    $result = $this->createOne($this->data);
54 1
                    if (!Request::isXmlHttpRequest()) {
55
                        $result = [
56 1
                            'row'    => $this->readOne($result),
57
                            'method' => Request::METHOD_PUT
58
                        ];
59
                    }
60 1
                } catch (ValidatorException $e) {
61 1
                    $row = $this->readOne(null);
62 1
                    $row->setFromArray($this->data);
63
                    $result = [
64 1
                        'row'    => $row,
65 1
                        'errors' => $e->getErrors(),
66 1
                        'method' => $this->getMethod()
67
                    ];
68
                }
69 2
                break;
70 6
            case Request::METHOD_PATCH:
71 6
            case Request::METHOD_PUT:
72
                try {
73
                    // Result is numbers of affected rows
74 3
                    $result = $this->updateOne($primary, $this->data);
75 1
                    if (!Request::isXmlHttpRequest()) {
76
                        $result = [
77 1
                            'row'    => $this->readOne($primary),
78 1
                            'method' => $this->getMethod()
79
                        ];
80
                    }
81 2
                } catch (ValidatorException $e) {
82 1
                    $row = $this->readOne($primary);
83 1
                    $row->setFromArray($this->data);
84
                    $result = [
85 1
                        'row'    => $row,
86 1
                        'errors' => $e->getErrors(),
87 1
                        'method' => $this->getMethod()
88
                    ];
89
                }
90 2
                break;
91 3
            case Request::METHOD_DELETE:
92
                // Result is numbers of affected rows
93 2
                $result = $this->deleteOne($primary);
94 1
                break;
95
            default:
96 1
                throw new NotImplementedException();
97
        }
98 7
        return $result;
99
    }
100
101
    /**
102
     * Return primary key
103
     *
104
     * @return array
105
     */
106 11
    public function getPrimaryKey()
107
    {
108 11
        if (is_null($this->primary)) {
109 11
            $primary = $this->getCrud()->getPrimaryKey();
110 11
            $this->primary = array_intersect_key($this->data, array_flip($primary));
111
        }
112 11
        return $this->primary;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @param  array $data
119
     * @return mixed
120
     * @throws \Bluz\Application\Exception\ApplicationException
121
     * @throws \Bluz\Application\Exception\NotImplementedException
122
     */
123 2
    public function createOne($data)
124
    {
125 2
        $result = parent::createOne($data);
126
127 1
        Messages::addSuccess("Record was created");
128
129 1
        return $result;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     *
135
     * @param  mixed $id
136
     * @param  array $data
137
     * @return integer
138
     * @throws \Bluz\Application\Exception\ApplicationException
139
     * @throws \Bluz\Application\Exception\NotImplementedException
140
     */
141 3
    public function updateOne($id, $data)
142
    {
143 3
        $result = parent::updateOne($id, $data);
144
145 1
        Messages::addSuccess("Record was updated");
146
147 1
        return $result;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     *
153
     * @param  mixed $primary
154
     * @return integer
155
     * @throws \Bluz\Application\Exception\ApplicationException
156
     * @throws \Bluz\Application\Exception\NotImplementedException
157
     */
158 2
    public function deleteOne($primary)
159
    {
160 2
        $result = parent::deleteOne($primary);
161
162 1
        Messages::addSuccess("Record was deleted");
163
164 1
        return $result;
165
    }
166
}
167