EventCreator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createForTrackable() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Logistiq\Tracking\Services;
6
7
use Illuminate\Contracts\Container\BindingResolutionException;
8
use Illuminate\Contracts\Foundation\Application;
9
use ReflectionClass;
10
use ReliqArts\Logistiq\Tracking\Contracts\EventCreator as EventCreatorContract;
11
use ReliqArts\Logistiq\Tracking\Contracts\Trackable;
12
use ReliqArts\Logistiq\Tracking\Exceptions\TrackableEventCreationFailed;
13
14
final class EventCreator implements EventCreatorContract
15
{
16
    private const ARGUMENT = 'argument';
17
18
    /**
19
     * @var Application
20
     */
21
    private $application;
22
23
    /**
24
     * EventCreator constructor.
25
     *
26
     * @param Application $application
27
     */
28
    public function __construct(Application $application)
29
    {
30
        $this->application = $application;
31
    }
32
33
    /**
34
     * @param string    $eventClassName
35
     * @param Trackable $trackable
36
     *
37
     * @throws TrackableEventCreationFailed
38
     *
39
     * @return mixed
40
     */
41
    public function createForTrackable(string $eventClassName, Trackable $trackable)
42
    {
43
        try {
44
            $eventClass = $this->application->make(ReflectionClass::class, [self::ARGUMENT => $eventClassName]);
45
46
            return $eventClass->newInstanceArgs([$trackable]);
47
        } catch (BindingResolutionException $exception) {
48
            throw TrackableEventCreationFailed::forEventClass($eventClassName, $trackable, $exception);
49
        }
50
    }
51
}
52