Completed
Push — master ( c1932e...0de009 )
by Freek
27s queued 11s
created

StoredEvent   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 11.49 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 10
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A toArray() 0 11 2
A handle() 0 16 2
A getActualClassForEvent() 0 4 1
A getEventClass() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\EventProjector\Models;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Spatie\EventProjector\Facades\Projectionist;
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