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

DispatchedGeneratorEvents::getDispatchedEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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