Completed
Push — master ( 4b9100...31bc02 )
by Christopher
05:45
created

Store::scopeForEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php namespace C4tech\RayEmitter\Event;
2
3
use C4tech\RayEmitter\Contracts\Domain\Event as EventInterface;
4
use C4tech\RayEmitter\Contracts\Event\Store as StoreInterface;
5
use Illuminate\Support\Facades\Event as EventBus;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Store extends Model implements StoreInterface
9
{
10
    protected static $queue = [];
11
12
    /**
13
     * @inheritDoc
14
     */
15
    protected $fillable = [
16
        'identifier',
17
        'sequence',
18
        'event',
19
        'payload'
20
    ];
21
22 1
    public function enqueue(EventInterface $event)
23
    {
24 1
        static::$queue[] = [
25 1
            'event'      => get_class($event),
26 1
            'identifier' => $event->getId(),
27 1
            'payload'    => $event->serialize()
28 1
        ];
29
30 1
        EventBus::fire($event);
31 1
    }
32
33 1
    public function getFor($identifier)
34
    {
35 1
        $events = new Collection;
36
37 1
        $recorded_events = $this->newQuery()->forEntity($identifier)->get();
38
        foreach ($recorded_events as $record) {
39
            $events->append($this->restoreEvent($record));
40
        }
41
42
        return $events;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $events; (C4tech\RayEmitter\Event\Collection) is incompatible with the return type declared by the interface C4tech\RayEmitter\Contracts\Event\Store::getFor of type C4tech\RayEmitter\Contra...tracts\Event\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
43
    }
44
45 1
    protected function restoreEvent($record)
46
    {
47 1
        $class = $record->event;
48
49 1
        return $class::unserialize($record);
50
    }
51
52
    public static function saveQueue()
53
    {
54
        foreach (static::$queue as $record) {
55
            $record['sequence'] = static::forEntity($record['identifier'])
0 ignored issues
show
Bug introduced by
The method forEntity() does not exist on C4tech\RayEmitter\Event\Store. Did you maybe mean scopeForEntity()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
                ->count();
57
            static::create($record);
58
        }
59
60
        static::$queue = [];
61
    }
62
63
    public function scopeForEntity($query, $identifier)
64
    {
65
        return $query->where('identifier', '=', $identifier);
66
    }
67
}
68