Completed
Push — master ( 74e877...60d1ae )
by Christopher
02:05
created

Store::replayAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
crap 1
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\Support\Facades\Log;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Store extends Model implements StoreInterface
10
{
11
    protected static $queue = [];
12
13
    /**
14
     * @inheritDoc
15
     */
16
    protected $table = 'event_store';
17
18
    /**
19
     * @inheritDoc
20
     */
21
    protected $fillable = [
22
        'identifier',
23
        'sequence',
24
        'event',
25
        'payload'
26
    ];
27
28
    /**
29
     * @inheritDoc
30
     */
31 1
    public function enqueue(EventInterface $event)
32
    {
33 1
        $class = get_class($event);
34 1
        static::$queue[] = [
35 1
            'event'      => $class,
36 1
            'identifier' => $event->getId(),
37 1
            'payload'    => $event->serialize(),
38
            'raw'        => $event
39 1
        ];
40
41 1
        EventBus::fire('queue:' . $class, [$event]);
42 1
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 1
    public function getFor($identifier)
48
    {
49 1
        $events = new Collection;
50
51 1
        $recorded_events = $this->newQuery()->forEntity($identifier)->get();
52 1
        foreach ($recorded_events as $record) {
53 1
            $events->append($this->restoreEvent($record));
54 1
        }
55
56 1
        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...
57
    }
58
59 1
    public function replayAll()
60
    {
61 1
        $records = $this->all();
62
63 1
        $records->each(function ($record) {
64 1
            $event = $this->restoreEvent($record);
65 1
            EventBus::fire('replay:' . $record->event, [$event]);
66 1
        });
67 1
    }
68
69
    /**
70
     * Restore Event
71
     *
72
     * Unserialize saved record back into an Event.
73
     * @param  static         $record Event Store model record
74
     * @return EventInterface
75
     */
76 1
    protected function restoreEvent($record)
77
    {
78 1
        $class = $record->event;
0 ignored issues
show
Documentation introduced by
The property event does not exist on object<C4tech\RayEmitter\Event\Store>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
80 1
        return $class::unserialize($record);
81
    }
82
83
    /**
84
     * Save Queue
85
     *
86
     * Persist all queued Events into Event Store.
87
     * @return void
88
     */
89 1
    public function saveQueue()
90
    {
91 1
        foreach (static::$queue as $record) {
92 1
            $event = $record['raw'];
93 1
            unset($record['raw']);
94
95 1
            $record['sequence'] = $this->newQuery()->forEntity($record['identifier'])
96 1
                ->count();
97 1
            static::create($record);
98
99 1
            EventBus::fire('save:' . $record['event'], [$event]);
100 1
        }
101
102 1
        static::$queue = [];
103 1
    }
104
105
    /**
106
     * Scope: For Entity
107
     *
108
     * Query scope for Entity identifier.
109
     * @param  Query  $query      Query Builder
110
     * @param  string $identifier Entity identifier
111
     * @return Query
112
     */
113 1
    public function scopeForEntity($query, $identifier)
114
    {
115 1
        return $query->where('identifier', '=', $identifier);
116
    }
117
}
118