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

Snapshots   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 11
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A attachSnapshotEvent() 0 12 3
A initializeSnapshots() 0 4 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 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