Test Failed
Push — master ( 507036...762659 )
by Julien
21:33
created

Save::save()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 29
c 3
b 0
f 0
nc 9
nop 0
dl 0
loc 51
ccs 0
cts 33
cp 0
crap 56
rs 8.5226

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Traits\Query;
13
14
use Phalcon\Messages\Message;
15
use Phalcon\Mvc\ModelInterface;
16
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractExpose;
17
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractInjectable;
18
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractModel;
19
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams;
20
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractQuery;
21
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractSave;
22
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractWith;
23
use Zemit\Mvc\Controller\Traits\Abstracts\Query\Fields\AbstractMapFields;
24
use Zemit\Mvc\Controller\Traits\Abstracts\Query\Fields\AbstractSaveFields;
25
use Zemit\Mvc\Model\Interfaces\EagerLoadInterface;
26
27
trait Save
28
{
29
    use AbstractSave;
30
    
31
    use AbstractExpose;
32
    use AbstractInjectable;
33
    use AbstractModel;
34
    use AbstractParams;
35
    use AbstractQuery;
36
    use AbstractWith;
37
    use AbstractMapFields;
38
    use AbstractSaveFields;
39
    
40
    /**
41
     * Saves the entity and returns the result as an array.
42
     * @todo handle dynamic and/or composed primary keys
43
     * @todo handle multiple entities
44
     * @todo segregate create vs update vs save
45
     * 
46
     * @return array The result of the save operation, including the saved status, messages,
47
     *               and optionally the saved entity data if the save was successful.
48
     */
49
    public function save(): array
50
    {
51
        $post = $this->getParams();
52
        $saveFields = $this->getSaveFields()?->toArray();
53
        $mapFields = $this->getMapFields()?->toArray();
54
        
55
        // @todo find what we should do here to handle: 1. dynamic and/or composed primary keys
56
        if (isset($post['id'])) {
57
            $model = $this->findFirst();
58
            if (!isset($model)) {
59
                return [
60
                    'saved' => false,
61
                    'messages' => [new Message('Entity id `' . $post['id'] . '` not found.', 'id', 'NotFound', 404)],
62
                ];
63
            }
64
            unset($post['id']);
65
        }
66
        
67
        $model ??= $this->loadModel();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $model does not seem to be defined for all execution paths leading up to this point.
Loading history...
68
        assert($model instanceof ModelInterface);
69
        
70
        // before assign event
71
        $this->eventsManager->fire('rest:beforeAssign', $this, [&$model, &$post, &$saveFields, &$mapFields], false);
72
        $model->assign($post, $saveFields, $mapFields);
73
        
74
        if ($this->eventsManager->fire('rest:beforeSave', $this, [&$model]) === false) {
0 ignored issues
show
introduced by
The condition $this->eventsManager->fi...rray($model)) === false is always true.
Loading history...
Bug introduced by
Are you sure the usage of $this->eventsManager->fi..., $this, array($model)) targeting Phalcon\Events\Manager::fire() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
            return [
76
                'saved' => false,
77
                'messages' => $model->getMessages(),
78
            ];
79
        } 
80
        $saved = $model->save();
81
        $this->eventsManager->fire('rest:afterSave', $this, [&$model], false);
82
        
83
        $ret = [
84
            'saved' => $saved,
85
            'messages' => $model->getMessages()
86
        ];
87
        
88
        if ($saved) {
89
            // load relationship
90
            $with = $this->getWith()?->toArray();
91
            if (isset($with) && $model instanceof EagerLoadInterface) {
92
                $model = $model->load($with);
93
            }
94
            
95
            // expose the model data
96
            $ret['data'] = $this->expose($model);
97
        }
98
        
99
        return $ret;
100
    }
101
}
102