Passed
Push — master ( 5b5c00...4c0c12 )
by Julien
04:52
created

SaveAction::saveAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.9332
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Controller\Rest\Actions;
13
14
use Phalcon\Http\ResponseInterface;
15
use Zemit\Mvc\Controller\AbstractTrait\AbstractInjectable;
16
use Zemit\Mvc\Controller\Rest\Response;
17
18
trait SaveAction
19
{
20
    use AbstractInjectable;
21
    use Response;
22
    
23
    /**
24
     * Saving a record (create & update)
25
     */
26
    public function saveAction(?int $id = null): ResponseInterface
27
    {
28
        $ret = $this->save($id);
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        $ret = $this->save($id);
Loading history...
29
        $this->view->setVars($ret);
0 ignored issues
show
Bug introduced by
The property view does not exist on Zemit\Mvc\Controller\Rest\Actions\SaveAction. Did you mean view;?
Loading history...
30
        $saved = $this->saveResultHasKey($ret, 'saved');
31
        $messages = $this->saveResultHasKey($ret, 'messages');
32
        
33
        if (!$saved) {
34
            if (!$messages) {
35
                $this->response->setStatusCode(422, 'Unprocessable Entity');
0 ignored issues
show
Bug introduced by
The property response does not exist on Zemit\Mvc\Controller\Rest\Actions\SaveAction. Did you mean response;?
Loading history...
36
            }
37
            else {
38
                $this->response->setStatusCode(400, 'Bad Request');
39
            }
40
        }
41
        
42
        return $this->setRestResponse($saved);
43
    }
44
    
45
    /**
46
     * Return true if the record or the records where saved
47
     * Return false if one record wasn't saved
48
     * Return null if nothing was saved
49
     */
50
    public function saveResultHasKey(array $array, string $key): bool
51
    {
52
        $ret = $array[$key] ?? null;
53
        
54
        if (isset($array[0])) {
55
            foreach ($array as $k => $r) {
56
                if (isset($r[$key])) {
57
                    if ($r[$key]) {
58
                        $ret = true;
59
                    }
60
                    else {
61
                        $ret = false;
62
                        break;
63
                    }
64
                }
65
            }
66
        }
67
        
68
        return (bool)$ret;
69
    }
70
}
71