Passed
Push — main ( c84275...1d7014 )
by Gabriel
03:41
created

bootTimestampableManagerTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\DataObjects\Behaviors\Timestampable;
4
5
use Nip\Records\AbstractModels\RecordManager;
6
use Nip\Records\EventManager\Events\Event;
7
8
/**
9
 * Trait TimestampableManagerTrait
10
 * @package ByTIC\DataObjects\Behaviors\Timestampable
11
 */
12
trait TimestampableManagerTrait
13
{
14
    use TimestampableTrait;
15
16
    public function bootTimestampableManagerTrait()
17
    {
18
        $this->hookTimestampableIntoLifecycle();
19
    }
20
21
    protected function hookTimestampableIntoLifecycle()
22
    {
23
        $updateCallback = function ($record, $manager, $type) {
24
            if (method_exists($manager, 'getTimestampAttributes')) {
25
                $attributes = $this->getTimestampAttributes($type);
26
            } else {
27
                $attributes = $type;
28
            }
29
30
            /** @var TimestampableTrait $record */
31
            $record->updatedTimestamps($attributes);
32
        };
33
34
        $events = ['creating' => 'create', 'updating' => 'update'];
35
        foreach ($events as $event => $type) {
36
            if (is_callable('static::' . $event) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
37
                continue;
38
            }
39
            static::$event(
40
                function (Event $event) use ($updateCallback, $type) {
41
                    $record = $event->getRecord();
42
                    /** @var static|RecordManager $manager */
43
                    $manager = $event->getManager();
44
                    $updateCallback($record, $manager, $type);
45
                }
46
            );
47
        }
48
    }
49
}
50