GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b21c26...dc2a23 )
by
unknown
21s queued 17s
created

Contact::setFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace LPTracker\models;
4
5
use LPTracker\exceptions\LPTrackerSDKException;
6
7
/**
8
 * Class Contact
9
 * @package LPTracker\models
10
 */
11
class Contact extends Model
12
{
13
14
    /**
15
     * @var integer
16
     */
17
    protected $id;
18
19
    /**
20
     * @var integer
21
     */
22
    protected $projectId;
23
24
    /**
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * @var string
31
     */
32
    protected $profession;
33
34
    /**
35
     * @var string
36
     */
37
    protected $site;
38
39
    /**
40
     * @var ContactDetail[]
41
     */
42
    protected $details = [];
43
44
    /**
45
     * @var ContactField[]
46
     */
47
    protected $fields = [];
48
49
50
    /**
51
     * Contact constructor.
52
     *
53
     * @param array $contactData
54
     */
55
    public function __construct(array $contactData = [])
56
    {
57
        if ( ! empty($contactData['id'])) {
58
            $this->id = intval($contactData['id']);
59
        }
60
        if ( ! empty($contactData['project_id'])) {
61
            $this->projectId = intval($contactData['project_id']);
62
        }
63
        if ( ! empty($contactData['name'])) {
64
            $this->name = $contactData['name'];
65
        }
66
        if ( ! empty($contactData['profession'])) {
67
            $this->profession = $contactData['profession'];
68
        }
69
        if ( ! empty($contactData['site'])) {
70
            $this->site = $contactData['site'];
71
        }
72 View Code Duplication
        if ( ! empty($contactData['details']) && is_array($contactData['details'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            foreach ($contactData['details'] as $detail) {
74
                $detail['contact_id'] = $this->id;
75
                $detailModel = new ContactDetail($detail);
76
                $this->addDetail($detailModel);
77
            }
78
        }
79 View Code Duplication
        if ( ! empty($contactData['fields']) && is_array($contactData['fields'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            foreach ($contactData['fields'] as $fieldData) {
81
                $fieldData['contact_id'] = $this->id;
82
                $fieldModel = new ContactField($fieldData);
83
                $this->addField($fieldModel);
84
            }
85
        }
86
    }
87
88
89
    /**
90
     * @return bool
91
     * @throws LPTrackerSDKException
92
     */
93
    public function validate()
94
    {
95
        if (empty($this->id) && empty($this->projectId)) {
96
            throw new LPTrackerSDKException('Project ID or Contact ID is required');
97
        }
98
        if (empty($this->details)) {
99
            throw new LPTrackerSDKException('The contact does not have valid detail');
100
        }
101
        foreach ($this->details as $detail) {
102
            if ( ! $detail->validate()) {
103
                throw new LPTrackerSDKException('The contact does not have valid detail');
104
            }
105
        }
106
107
        return true;
108
    }
109
110
111
    /**
112
     * @return array
113
     */
114
    public function toArray()
115
    {
116
        $result = [
117
            'project_id' => $this->getProjectId(),
118
            'details'    => [],
119
        ];
120
        if ( ! empty($this->id)) {
121
            $result['id'] = $this->getId();
122
        }
123
        if ( ! empty($this->name)) {
124
            $result['name'] = $this->getName();
125
        }
126
        if ( ! empty($this->profession)) {
127
            $result['profession'] = $this->getProfession();
128
        }
129
        if ( ! empty($this->site)) {
130
            $result['site'] = $this->getSite();
131
        }
132
        foreach ($this->getDetails() as $detail) {
133
            $result['details'][] = $detail->toArray();
134
        }
135
        foreach ($this->getFields() as $field) {
136
            $result['fields'][$field->getId()] = $field->getValue();
137
        }
138
139
        return $result;
140
    }
141
142
143
    /**
144
     * @return int
145
     */
146
    public function getId()
147
    {
148
        return intval($this->id);
149
    }
150
151
152
    /**
153
     * @return int
154
     */
155
    public function getProjectId()
156
    {
157
        return intval($this->projectId);
158
    }
159
160
161
    /**
162
     * @return string
163
     */
164
    public function getName()
165
    {
166
        return $this->name;
167
    }
168
169
170
    /**
171
     * @param string $name
172
     *
173
     * @return $this
174
     */
175
    public function setName($name)
176
    {
177
        $this->name = $name;
178
179
        return $this;
180
    }
181
182
183
    /**
184
     * @return string
185
     */
186
    public function getProfession()
187
    {
188
        return $this->profession;
189
    }
190
191
192
    /**
193
     * @param string $profession
194
     *
195
     * @return $this
196
     */
197
    public function setProfession($profession)
198
    {
199
        $this->profession = $profession;
200
201
        return $this;
202
    }
203
204
205
    /**
206
     * @return string
207
     */
208
    public function getSite()
209
    {
210
        return $this->site;
211
    }
212
213
214
    /**
215
     * @param string $site
216
     *
217
     * @return $this
218
     */
219
    public function setSite($site)
220
    {
221
        $this->site = $site;
222
223
        return $this;
224
    }
225
226
227
    /**
228
     * @return ContactDetail[]
229
     */
230
    public function getDetails()
231
    {
232
        return $this->details;
233
    }
234
235
236
    /**
237
     * @param ContactDetail[] $details
238
     *
239
     * @return $this
240
     */
241
    public function setDetails(array $details)
242
    {
243
        /** @var ContactDetail $detail */
244
        foreach ($details as $detail) {
245
            $detail->validate();
246
        }
247
248
        $this->details = $details;
249
250
        return $this;
251
    }
252
253
254
    /**
255
     * @param ContactDetail $detail
256
     *
257
     * @return $this
258
     */
259
    public function addDetail(ContactDetail $detail)
260
    {
261
        $detail->validate();
262
        $this->details[] = $detail;
263
264
        return $this;
265
    }
266
267
268
    /**
269
     * @return ContactField[]
270
     */
271
    public function getFields()
272
    {
273
        return $this->fields;
274
    }
275
276
277
    /**
278
     * @param ContactField[] $fields
279
     *
280
     * @return $this
281
     */
282
    public function setFields(array $fields)
283
    {
284
        /** @var ContactDetail $detail */
285
        foreach ($fields as $field) {
286
            $field->validate();
287
        }
288
289
        $this->fields = $fields;
290
291
        return $this;
292
    }
293
294
295
    /**
296
     * @param ContactField $field
297
     *
298
     * @return $this
299
     */
300
    public function addField(ContactField $field)
301
    {
302
        $field->validate();
303
        $this->fields[] = $field;
304
305
        return $this;
306
    }
307
}