Completed
Push — master ( f6c461...60f15f )
by Freek
14s queued 11s
created

StoredEvent::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Spatie\EventProjector\Facades\Projectionist;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Spatie\EventProjector\Projectionist.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Spatie\EventProjector\Exceptions\InvalidStoredEvent;
10
use Spatie\EventProjector\EventSerializers\EventSerializer;
11
12
class StoredEvent implements Arrayable
13
{
14
    /** @var int|null */
15
    public $id;
16
17
    /** @var string */
18
    public $event_properties;
19
20
    /** @var string */
21
    public $aggregate_uuid;
22
23
    /** @var string */
24
    public $event_class;
25
26
    /** @var array */
27
    public $meta_data;
28
29
    /** @var \Carbon\Carbon */
30
    public $created_at;
31
32
    /** @var \Spatie\EventProjector\ShouldBeStored|null */
33
    public $event;
34
35
    public function __construct(array $data)
36
    {
37
        $this->id = $data['id'] ?? null;
38
        $this->event_properties = $data['event_properties'];
39
        $this->aggregate_uuid = $data['aggregate_uuid'];
40
        $this->event_class = self::getActualClassForEvent($data['event_class']);
41
        $this->meta_data = $data['meta_data'];
42
        $this->created_at = $data['created_at'];
43
44
        try {
45
            $this->event = app(EventSerializer::class)->deserialize(
46
                self::getActualClassForEvent($this->event_class),
47
                json_encode($this->event_properties)
48
            );
49
        } catch (Exception $exception) {
50
            throw InvalidStoredEvent::couldNotUnserializeEvent($this, $exception);
51
        }
52
    }
53
54
    public function toArray()
55
    {
56
        return [
57
            'id' => $this->id,
58
            'event_properties' => $this->event_properties,
59
            'aggregate_uuid' => $this->aggregate_uuid,
60
            'event_class' => self::getEventClass($this->event_class),
61
            'meta_data' => $this->meta_data instanceof Arrayable ? $this->meta_data->toArray() : (array) $this->meta_data,
62
            'created_at' => $this->created_at,
63
        ];
64
    }
65
66
    public function handle()
67
    {
68
        Projectionist::handleWithSyncProjectors($this);
69
70
        if (method_exists($this->event, 'tags')) {
71
            $tags = $this->event->tags();
0 ignored issues
show
Bug introduced by
The method tags() does not seem to exist on object<Spatie\EventProjector\ShouldBeStored>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
        }
73
74
        $storedEventJob = call_user_func(
75
            [config('event-projector.stored_event_job'), 'createForEvent'],
76
            $this,
77
            $tags ?? []
78
        );
79
80
        dispatch($storedEventJob->onQueue($this->event->queue ?? config('event-projector.queue')));
0 ignored issues
show
Bug introduced by
Accessing queue on the interface Spatie\EventProjector\ShouldBeStored suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
    }
82
83
    protected static function getActualClassForEvent(string $class): string
84
    {
85
        return Arr::get(config('event-projector.event_class_map', []), $class, $class);
86
    }
87
88 View Code Duplication
    protected static function getEventClass(string $class): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $map = config('event-projector.event_class_map', []);
91
92
        if (! empty($map) && in_array($class, $map)) {
93
            return array_search($class, $map, true);
94
        }
95
96
        return $class;
97
    }
98
}
99