Test Failed
Push — master ( 894c40...e5d2d2 )
by Julien
11:34
created

Position::getPositionBehavior()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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\Model;
13
14
use Exception;
15
use Zemit\Mvc\Model;
16
use Zemit\Mvc\Model\Behavior\Position as PositionBehavior;
17
18
trait Position
19
{
20
    use Model\AbstractTrait\AbstractBehavior;
21
    use Attribute;
22
    use Options;
23
    
24
    public Model\Behavior\Position $positionBehavior;
25
    
26
    /**
27
     * Initializing Position
28
     */
29
    public function initializePosition(?array $options = null): void
30
    {
31
        $options ??= $this->getOptionsManager()->get('position') ?? [];
32
        $this->setPositionBehavior(new PositionBehavior($options));
33
    }
34
    
35
    /**
36
     * Set Position Behavior
37
     */
38
    public function setPositionBehavior(PositionBehavior $positionBehavior): void
39
    {
40
        $this->positionBehavior = $positionBehavior;
41
        $this->addBehavior($this->positionBehavior);
42
    }
43
    
44
    /**
45
     * Get Position Behavior
46
     */
47
    public function getPositionBehavior(): PositionBehavior
48
    {
49
        return $this->positionBehavior;
50
    }
51
    
52
    /**
53
     * Re-ordering an entity
54
     * - Update position+1 done using afterSave event
55
     *
56
     * @return mixed
57
     * @throws Exception
58
     */
59
    public function reorder(?int $position = null, ?string $positionField = null)
60
    {
61
        $positionField ??= $this->getPositionBehavior()->getField();
62
        
63
        if ($this->fireEventCancel('beforeReorder') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireEventCancel() 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

63
        if ($this->/** @scrutinizer ignore-call */ fireEventCancel('beforeReorder') === false) {
Loading history...
64
            return false;
65
        }
66
        
67
        $this->assign([$positionField => $position], [$positionField]);
0 ignored issues
show
Bug introduced by
It seems like assign() 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

67
        $this->/** @scrutinizer ignore-call */ 
68
               assign([$positionField => $position], [$positionField]);
Loading history...
68
        $saved = $this->save() && (!$this->hasSnapshotData() || $this->hasUpdated($positionField));
0 ignored issues
show
Bug introduced by
It seems like hasUpdated() 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

68
        $saved = $this->save() && (!$this->hasSnapshotData() || $this->/** @scrutinizer ignore-call */ hasUpdated($positionField));
Loading history...
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

68
        $saved = $this->/** @scrutinizer ignore-call */ save() && (!$this->hasSnapshotData() || $this->hasUpdated($positionField));
Loading history...
Bug introduced by
It seems like hasSnapshotData() 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

68
        $saved = $this->save() && (!$this->/** @scrutinizer ignore-call */ hasSnapshotData() || $this->hasUpdated($positionField));
Loading history...
69
        
70
        if ($saved) {
71
            $this->fireEvent('afterReorder');
0 ignored issues
show
Bug introduced by
It seems like fireEvent() 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

71
            $this->/** @scrutinizer ignore-call */ 
72
                   fireEvent('afterReorder');
Loading history...
72
        }
73
        
74
        return $saved;
75
    }
76
}
77