Test Failed
Push — master ( a835f3...4c7cec )
by Julien
04:50
created

Snapshots::_setSnapshots()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 10
cc 3
nc 2
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\Model;
13
14
use Zemit\Mvc\Model\AbstractTrait\AbstractEventsManager;
15
16
trait Snapshots
17
{
18
    use AbstractEventsManager;
19
    
20
    abstract protected function keepSnapshots(bool $keepSnapshot): void;
21
    
22
    public function initializeSnapshots(): void
23
    {
24
        $this->keepSnapshots(true);
25
        $this->attachSnapshotEvent(true);
26
    }
27
    
28
    protected function attachSnapshotEvent(bool $keepSnapshots = true): void
29
    {
30
        if ($keepSnapshots) {
31
            $this->getEventsManager()->attach('model', function ($event, $entity) {
32
                
33
                switch ($event->getType()) {
34
                    case 'beforeCreate':
35
                        $entity->setSnapshotData($entity->toArray());
36
                        break;
37
                }
38
                
39
                return true;
40
            });
41
        }
42
    }
43
}
44