CustomEvent   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 108
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A discardArraysExceptReserved() 0 9 4
A handleReservedKey() 0 17 3
A setProperties() 0 11 2
A getAppName() 0 3 1
A getEvenType() 0 3 1
A getProperties() 0 3 1
1
<?php
2
3
namespace ErsoyInsider\NewrelicCustomEvent\Events;
4
5
/**
6
 * Class CustomEvent
7
 * @package NewRelicCustomEvent
8
 */
9
class CustomEvent extends Event
10
{
11
    const RESERVED_ARRAY_KEY = 'users';
12
13
    /**
14
     * @var array
15
     */
16
    private $properties;
17
18
    /**
19
     * @return string
20
     */
21
    public function getEvenType(): string
22
    {
23
        return $this->evenType;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public function getAppName(): string
30
    {
31
        return $this->appName;
32
    }
33
    /**
34
     * @var string
35
     */
36
    private $evenType;
37
    /**
38
     * @var string
39
     */
40
    private $appName;
41
42
    /**
43
     * CustomEvent constructor.
44
     * @param array $properties
45
     * @param string $evenType
46
     * @param string $appName
47
     */
48
    public function __construct(array $properties, string $evenType, string $appName)
49
    {
50
        $this->evenType = $evenType;
51
        $this->appName = $appName;
52
        $this->setProperties($properties);
53
    }
54
55
    /**
56
     * @param array $properties
57
     */
58
    private function setProperties(array $properties)
59
    {
60
        if (!array_key_exists('eventType', $properties)) {
61
            $properties['eventType'] = $this->getEvenType();
62
        }
63
64
        $properties['appName'] = $this->getAppName();
65
        $properties = $this->discardArraysExceptReserved($properties);
66
        $properties = $this->handleReservedKey($properties);
67
68
        $this->properties = $properties;
69
    }
70
71
    /**
72
     * Discards keys of which has arrays except reserved one
73
     *
74
     * @param array $properties
75
     * @return array
76
     */
77
    private function discardArraysExceptReserved(array $properties)
78
    {
79
        foreach ($properties as $key => $property) {
80
            if (is_array($property) && $key !== self::RESERVED_ARRAY_KEY) {
81
                unset($properties[$key]);
82
            }
83
        }
84
85
        return $properties;
86
    }
87
88
    /**
89
     * @param array $properties
90
     * @return array
91
     */
92
    private function handleReservedKey(array $properties)
93
    {
94
        $reservedKey = self::RESERVED_ARRAY_KEY;
95
96
        if (array_key_exists($reservedKey, $properties)) {
97
            $users = $properties[$reservedKey];
98
            unset($properties[$reservedKey]);
99
            $properties = array_map(function ($user) use ($properties) {
100
                if (is_array($user)) {
101
                    return $user + $properties;
102
                }
103
104
                return $properties;
105
            }, $users);
106
        }
107
108
        return $properties;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getProperties(): array
115
    {
116
        return $this->properties;
117
    }
118
}
119