Passed
Push — master ( c7459e...5c3821 )
by Arthur
22:05
created

DispatchedGeneratorEvents   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertAndReturnDispatchedEvents() 0 8 1
A getDispatchedEvents() 0 10 3
A getFirstDispatchedEvent() 0 6 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 11.03.19
6
 * Time: 17:08
7
 */
8
9
namespace Foundation\Generator\Traits;
10
11
12
use Foundation\Generator\Events\FileGeneratedEvent;
13
use Illuminate\Support\Facades\Event;
14
15
trait DispatchedGeneratorEvents
16
{
17
18
    /**
19
     * @return FileGeneratedEvent[]
20
     */
21
    protected function assertAndReturnDispatchedEvents()
22
    {
23
        $events = [];
24
        Event::assertDispatched(FileGeneratedEvent::class, function ($event) use (&$events) {
25
            $events[] = $event;
26
            return true;
27
        });
28
        return $events;
29
    }
30
31
    /**
32
     * @param string $class
33
     * @return FileGeneratedEvent[]
34
     */
35
    protected function getDispatchedEvents(string $class)
36
    {
37
        $generationEvents = [];
38
        $events = $this->assertAndReturnDispatchedEvents();
39
        foreach ($events as $event) {
40
            if ($event->getGenerationClass() === $class) {
41
                $generationEvents[] = $event;
42
            }
43
        }
44
        return $generationEvents;
45
    }
46
47
    /**
48
     * @param string $class
49
     * @return FileGeneratedEvent
50
     */
51
    protected function getFirstDispatchedEvent(string $class)
52
    {
53
        $events = $this->getDispatchedEvents($class);
54
        if (empty($events))
55
            return null;
56
        return $events[0];
57
    }
58
}
59