Passed
Push — master ( 4404ca...3f333d )
by Julien
16:13 queued 10:15
created

Position   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 26.32%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 19
c 4
b 0
f 0
dl 0
loc 62
ccs 5
cts 19
cp 0.2632
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializePosition() 0 5 1
A setPositionBehavior() 0 3 1
A reorder() 0 18 5
A getPositionBehavior() 0 5 1
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\AbstractTrait\AbstractEventsManager;
17
use Zemit\Mvc\Model\Behavior\Position as PositionBehavior;
18
19
trait Position
20
{
21
    use AbstractEventsManager;
22
    use Behavior;
23
    use Options;
24
    
25
    /**
26
     * Initializing Position
27
     */
28 2
    public function initializePosition(?array $options = null): void
29
    {
30 2
        $options ??= $this->getOptionsManager()->get('position') ?? [];
31
        
32 2
        $this->setPositionBehavior(new PositionBehavior($options));
33
    }
34
    
35
    /**
36
     * Set Position Behavior
37
     */
38 2
    public function setPositionBehavior(PositionBehavior $positionBehavior): void
39
    {
40 2
        $this->setBehavior('position', $positionBehavior);
41
    }
42
    
43
    /**
44
     * Get Position Behavior
45
     */
46
    public function getPositionBehavior(): PositionBehavior
47
    {
48
        $behavior = $this->getBehavior('position');
49
        assert($behavior instanceof PositionBehavior);
50
        return $behavior;
51
    }
52
    
53
    /**
54
     * Reorders the current object's position in the list.
55
     * - Update position+1 done using afterSave event
56
     *
57
     * @param int|null $position The new position for the object. If not provided, the default behavior's position field will be used.
58
     * @param string|null $positionField The field on which the position is stored. If not provided, the default behavior's field will be used.
59
     *
60
     * @return bool Returns true if the reorder operation was successful, false otherwise.
61
     * @throws Exception
62
     */
63
    public function reorder(?int $position = null, ?string $positionField = null): bool
64
    {
65
        assert($this instanceof Model);
66
        
67
        $positionField ??= $this->getPositionBehavior()->getField();
68
        
69
        if ($this->fireEventCancel('beforeReorder') === false) {
70
            return false;
71
        }
72
        
73
        $this->assign([$positionField => $position], [$positionField]);
74
        $saved = $this->save() && (!$this->hasSnapshotData() || $this->hasUpdated($positionField));
75
        
76
        if ($saved) {
77
            $this->fireEvent('afterReorder');
78
        }
79
        
80
        return $saved;
81
    }
82
}
83