EventCreator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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