slickframework /
telemetry
| 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
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 |