Service::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common;
4
5
use ZoiloMora\ElasticAPM\Events\Common\Service\Agent;
6
use ZoiloMora\ElasticAPM\Events\Common\Service\Framework;
7
use ZoiloMora\ElasticAPM\Events\Common\Service\Language;
8
use ZoiloMora\ElasticAPM\Events\Common\Service\Node;
9
use ZoiloMora\ElasticAPM\Events\Common\Service\Runtime;
10
use ZoiloMora\ElasticAPM\Helper\Encoding;
11
12
class Service implements \JsonSerializable
13
{
14
    /**
15
     * Name and version of the Elastic APM agent
16
     *
17
     * @var Agent|null
18
     */
19
    private $agent;
20
21
    /**
22
     * Name and version of the web framework used
23
     *
24
     * @var Framework|null
25
     */
26
    private $framework;
27
28
    /**
29
     * Name and version of the programming language used
30
     *
31
     * @var Language|null
32
     */
33
    private $language;
34
35
    /**
36
     * Immutable name of the service emitting this event
37
     *
38
     * @var string|null
39
     */
40
    private $name;
41
42
    /**
43
     * Environment name of the service, e.g. "production" or "staging"
44
     *
45
     * @var string|null
46
     */
47
    private $environment;
48
49
    /**
50
     * Name and version of the language runtime running this service
51
     *
52
     * @var Runtime|null
53
     */
54
    private $runtime;
55
56
    /**
57
     * Version of the service emitting this event
58
     *
59
     * @var string|null
60
     */
61
    private $version;
62
63
    /**
64
     * Unique meaningful name of the service node.
65
     *
66
     * @var Node|null
67
     */
68
    private $node;
69
70
    /**
71
     * @param Agent|null $agent
72
     * @param Framework|null $framework
73
     * @param Language|null $language
74
     * @param string|null $name
75
     * @param string|null $environment
76
     * @param Runtime|null $runtime
77
     * @param string|null $version
78
     * @param Node|null $node
79
     */
80 19
    public function __construct(
81
        Agent $agent = null,
82
        Framework $framework = null,
83
        Language $language = null,
84
        $name = null,
85
        $environment = null,
86
        Runtime $runtime = null,
87
        $version = null,
88
        Node $node = null
89
    ) {
90 19
        $this->agent = $this->validateAgent($agent);
91 19
        $this->framework = $framework;
92 19
        $this->language = $language;
93 19
        $this->name = $this->validateName($name);
94 19
        $this->environment = $environment;
95 19
        $this->runtime = $runtime;
96 19
        $this->version = $version;
97 19
        $this->node = $node;
98 19
    }
99
100
    /**
101
     * @return Agent|null
102
     */
103 1
    public function agent()
104
    {
105 1
        return $this->agent;
106
    }
107
108
    /**
109
     * @return Framework|null
110
     */
111 1
    public function framework()
112
    {
113 1
        return $this->framework;
114
    }
115
116
    /**
117
     * @return Language|null
118
     */
119 17
    public function language()
120
    {
121 17
        return $this->language;
122
    }
123
124
    /**
125
     * @return string|null
126
     */
127 1
    public function name()
128
    {
129 1
        return $this->name;
130
    }
131
132
    /**
133
     * @return string|null
134
     */
135 1
    public function environment()
136
    {
137 1
        return $this->environment;
138
    }
139
140
    /**
141
     * @return Runtime|null
142
     */
143 16
    public function runtime()
144
    {
145 16
        return $this->runtime;
146
    }
147
148
    /**
149
     * @return string|null
150
     */
151 1
    public function version()
152
    {
153 1
        return $this->version;
154
    }
155
156
    /**
157
     * @return Node|null
158
     */
159 1
    public function node()
160
    {
161 1
        return $this->node;
162
    }
163
164
    /**
165
     * @param Agent|null $agent
166
     *
167
     * @return Agent
168
     */
169 19
    private function validateAgent($agent)
170
    {
171 19
        return null === $agent
172 19
            ? Agent::discover()
173 19
            : $agent;
174
    }
175
176
    /**
177
     * @param string|null $name
178
     *
179
     * @return string|null
180
     */
181 19
    private function validateName($name)
182
    {
183 19
        return 1 === preg_match('/^[a-zA-Z0-9 _-]+$/', $name)
184 19
            ? $name
185 19
            : null;
186
    }
187
188
    /**
189
     * @return array
190
     */
191 1
    public function jsonSerialize()
192
    {
193
        return [
194 1
            'agent' => $this->agent,
195 1
            'framework' => $this->framework,
196 1
            'language' => $this->language,
197 1
            'name' => Encoding::keywordField($this->name),
198 1
            'environment' => Encoding::keywordField($this->environment),
199 1
            'runtime' => $this->runtime,
200 1
            'version' => Encoding::keywordField($this->version),
201 1
            'node' => $this->node,
202 1
        ];
203
    }
204
}
205