Event   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 12
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A parseEventName() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of slick/telemetry package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Telemetry\Model;
13
14
use JsonSerializable;
15
use Slick\Event\Event as SlickEvent;
16
use Slick\Telemetry\Trackable;
17
18
/**
19
 * Event
20
 *
21
 * @package Slick\Telemetry\Model
22
 */
23
final class Event implements Trackable
24
{
25
    use TrackableMethods;
26
27
    /**
28
     * Creates a Event
29
     *
30
     * @param SlickEvent $event
31
     * @param iterable|null $context
32
     */
33
    public function __construct(SlickEvent $event, ?iterable $context = [])
34
    {
35
        $this->message = $this->parseEventName($event);
36
        $this->label = Trackable::LABEL_EVENT;
37
        $this->context = array_merge($context, [
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type iterable and null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $this->context = array_merge(/** @scrutinizer ignore-type */ $context, [
Loading history...
38
            'label' => $this->label,
39
            'occurredOn' => $event->occurredOn()->format(\DateTime::W3C)
40
        ]);
41
42
        if ($event instanceof JsonSerializable) {
43
            $this->context['data'] = json_encode($event);
44
        }
45
    }
46
47
    /**
48
     * Parse event name from its class name
49
     *
50
     * @param SlickEvent $event
51
     * @return string
52
     */
53
    private function parseEventName(SlickEvent $event): string
54
    {
55
        $parts = explode("\\", get_class($event));
56
        $name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', end($parts)));
57
        return ucfirst(str_replace('_', ' ', $name));
58
    }
59
}
60