Test Setup Failed
Pull Request — master (#24)
by
unknown
02:19
created

ContactInfo::removeAddress()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
use Scriptotek\Alma\Model\Model;
6
7
class ContactInfo extends Model
8
{
9
    
10
    /**
11
     * Get the user's preferred SMS number.
12
     *
13
     * @return Phone|null
14
     */
15
    public function getSmsNumber()
16
    {
17
        if ($this->data->phone) {
18
            foreach ($this->data->phone as $phone) {
19
                if ($phone->preferred_sms) {
20
                    return Phone::make($this->client, $phone);
21
                }
22
            }
23
        }
24
        return null;
25
    }
26
 
27
    /**
28
     * Remove the preferred SMS flag from any number.
29
     */
30
    public function unsetSmsNumber()
31
    {
32
        if ($this->data->phone) {
33
            foreach ($this->data->phone as $phone) {
34
                if ($phone->preferred_sms) {
35
                    $phone->preferred_sms = false;
36
                }
37
            }
38
        }
39
    }
40
41
    /**
42
     * Set the user's preferred SMS number, creating a new internal mobile number if needed
43
     * @param $number string The SMS-capable mobile phone number
44
     */
45
    public function setSmsNumber($number)
46
    {
47
        $currentNumber = $this->getSmsNumber();
48
        if ($currentNumber && $number === $currentNumber->phone_number) {
49
            return;
50
        }
51
        $this->unsetSmsNumber();
52
        if ($this->data->phone) {
53
            foreach ($this->data->phone as $phone) {
54
                if ($phone->phone_number === $number) {
55
                    $phone->preferred_sms = true;
56
                    return;
57
                }
58
            }
59
        }
60
        $this->addSmsNumber($number);
61
    }
62
63
    /**
64
     * Add the user's preferred SMS number as a new internal mobile number.
65
     * @param $number string The SMS-capable mobile phone number
66
     * 
67
     * @return Phone
68
     */
69
    public function addSmsNumber($number)
70
    {
71
        $currentNumber = $this->getSmsNumber();
72
        if ($currentNumber) {
73
            $this->unsetSmsNumber();
74
        }
75
        if (!$this->data->phone) {
76
            $this->data->phone = [];
77
        }
78
        $phone_obj = (object) [
79
            'phone_number' => $number,
80
            'preferred' => false,
81
            'preferred_sms' => true,
82
            'segment_type' => 'Internal',
83
            'phone_type' => [(object) [
84
                'value' => 'mobile',
85
                'desc' => 'Mobile'
86
            ]]
87
        ];
88
        $this->data->phone[] = $phone_obj;
89
        return Phone::make($this->client, $phone_obj);
90
    }
91
92
    /**
93
     * Adds a new internal phone number to the user
94
     * 
95
     * @param string $phone_number The phone number
96
     * @param string $phone_type Type of the phone number (home, mobile, etc.)
97
     * @param bool $preferred Whether this should be the user's preferred phone number
98
     * 
99
     * @return Phone
100
     */
101 View Code Duplication
    public function addPhone($phone_number, $phone_type, $preferred = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
102
    {
103
        if (!$this->data->phone) {
104
            $this->data->phone = [];
105
        }
106
        if ($preferred) {
107
            $this->unsetPreferredPhone();
108
        }
109
        $phone_obj = (object) [
110
            'phone_number' => $phone_number,
111
            'preferred' => $preferred,
112
            'preferred_sms' => false,
113
            'segment_type' => 'Internal',
114
            'phone_type' => [(object) [
115
                'value' => $phone_type
116
            ]]
117
        ];
118
        $this->data->phone[] = $phone_obj;
119
        return Phone::make($this->client, $phone_obj);
120
    }
121
122
    /**
123
     * Gets the user's preferred phone number, or null if none are preferred
124
     * 
125
     * @return Phone|null
126
     */
127
    public function getPreferredPhone()
128
    {
129
        foreach ($this->data->phone as $phone) {
130
            if ($phone->preferred) {
131
                return Phone::make($this->client, $phone);
132
            }
133
        }
134
        return null;
135
    }
136
137
    /**
138
     * Remove the preferred flag from all phone numbers
139
     */
140
    public function unsetPreferredPhone()
141
    {
142
        foreach ($this->data->phone as $phone) {
143
            if ($phone->preferred) {
144
                $phone->preferred = false;
145
            }
146
        }
147
    }
148
149
    /**
150
     * Sets the given phone number as the user's preferred number, adding it as an internal home phone if necessary
151
     * 
152
     * @param string $phone_number The phone number
153
     * 
154
     * @throws Exception when the given phone number is not found in the user
155
     */
156
    public function setPreferredPhone($phone_number)
157
    {
158
        $current_phone = $this->getPreferredPhone();
159
        if ($current_phone && $phone_number === $current_phone->phone_number) {
160
            return;
161
        }
162
        $this->unsetPreferredPhone();
163
        foreach ($this->data->phone as $phone) {
164
            if ($phone->phone_number === $phone_number) {
165
                $phone->preferred = true;
166
                return;
167
            }
168
        }
169
        throw new Exception('Phone number ' . $phone_number . ' not found in user');
170
    }
171
172
    /**
173
     * Removes a phone number from the user
174
     * 
175
     * @param string The phone number
176
     */
177
    public function removePhone($phone_number)
178
    {
179
        foreach ($this->data->phone as $key => $phone) {
180
            if ($phone->phone_number === $phone_number) {
181
                array_splice($this->data->phone, $key, 1);
182
                return;
183
            }
184
        }
185
    }
186
187
    /**
188
     * Returns an array of all phone numbers associated with the user
189
     * 
190
     * @return array An array of Phone objects
191
     */
192
    public function allPhones()
193
    {
194
        $phones = [];
195
        foreach ($this->data->phone as $phone) {
196
            $phones[] = Phone::make($this->client, $phone);
197
        }
198
        return $phones;
199
    }
200
201
    /**
202
     * Gets the user's preferred email address
203
     * @return Email The email address
204
     */
205
    public function getEmail()
206
    {
207
        if ($this->data->email) {
208
            foreach ($this->data->email as $email) {
209
                if ($email->preferred) {
210
                    return Email::make($this->client, $email);
211
                }
212
            }
213
        }
214
        return null;
215
    }
216
217
    /**
218
     * Sets the user's preferred email address, adding a new email address if needed.
219
     * @param string $email_address The email address
220
     * 
221
     * @throws Exception when the given email address is not found in the user
222
     */
223
    public function setEmail($email_address)
224
    {
225
        $current_email = $this->getEmail();
226
        if ($current_email && $email_address === $current_email->email_address) {
227
            return;
228
        }
229
        $this->unsetEmail();
230 View Code Duplication
        if ($this->data->email) {
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...
231
            foreach ($this->data->email as $email) {
232
                if ($email->email_address === $email_address) {
233
                    $email->preferred = true;
234
                    return;
235
                }
236
            }
237
        }
238
        throw new Exception('Email address ' . $email_address . ' not found in user');
239
    }
240
241
    /**
242
     * Removes the preferred flag from all email addresses
243
     */
244
    public function unsetEmail()
245
    {
246 View Code Duplication
        if ($this->data->email) {
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...
247
            foreach ($this->data->email as $email) {
248
                if ($email->preferred) {
249
                    $email->preferred = false;
250
                }
251
            }
252
        }
253
    }
254
255
    /**
256
     * Adds a new email address
257
     * @param string $email_address The email address
258
     * @param string $email_type The email type, defaults to 'personal'
259
     * @param bool $preferred True if this should be the preferred email
260
     * 
261
     * @return Email
262
     */
263 View Code Duplication
    public function addEmail($email_address, $email_type = 'personal', $preferred = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
264
    {
265
        if (!$this->data->email) {
266
            $this->data->email = [];
267
        }
268
        if ($preferred) {
269
            $this->unsetEmail();
270
        }
271
        $email_obj = (object) [
272
            'preferred' => $preferred,
273
            'segment_type' => 'Internal',
274
            'email_address' => $email_address,
275
            'description' => '',
276
            'email_type' =>[(object) [
277
                'value' => $email_type,
278
            ]]
279
        ];
280
        $this->data->email[] = $email_obj;
281
        return Email::make($this->client, $email_obj);
282
    }
283
284
    /**
285
     * Removes the given email address from the user
286
     * 
287
     * @param string $email_address The email address to remove
288
     */
289
    public function removeEmail($email_address)
290
    {
291
        foreach ($this->data->email as $key => $email) {
292
            if ($email->email_address === $email_address) {
293
                array_splice($this->data->email, $key, 1);
294
                return;
295
            }
296
        }
297
    }
298
299
    /**
300
     * Returns an array of all email addresses associated with the user
301
     * 
302
     * @return array An array of Email objects
303
     */
304
    public function allEmails()
305
    {
306
        $emails = [];
307
        foreach ($this->data->email as $email) {
308
            $emails[] = Email::make($this->client, $email);
309
        }
310
        return $emails;
311
    }
312
313
    /**
314
     * Get an array of objects representing the user's addresses
315
     * 
316
     * @return array An array of Address objects
317
     */
318
    public function getAddresses() {
319
        $addresses = [];
320
        foreach ($this->address as $address) {
321
            $addresses[] = Address::make($this->client, $address);
322
        }
323
        return $addresses;
324
    }
325
326
    /**
327
     * Adds a new address.
328
     * 
329
     * @param array $address The address' properties
330
     * @return Address A new address object based on the given values
331
     */
332
    public function addAddress($address)
333
    {
334
        if (isset($address['country']) && is_string($address['country'])) {
335
            $address['country'] = (object) ['value' => $address['country']];
336
        }
337
        if (isset($address['address_type'])) {
338
            if (is_string($address['address_type'])) {
339
                $address['address_type'] = [(object)['value' => $address['address_type']]];
340
            } elseif (is_object($address['address_type'])) {
341
                $address['address_type'] = [$address['address_type']];
342
            }
343
        }
344
        if (!$this->data->address) {
345
            $this->data->address = [];
346
        }
347
        $address = (object) $address;
348
        $this->data->address[] = $address;
349
        return Address::make($this->client, $address);
350
    }
351
352
    /**
353
     * Removes an address from the user
354
     * 
355
     * @param Address|stdClass $address Either the Address object to be removed or its underlying stdClass object
356
     */
357
    public function removeAddress($address)
358
    {
359
        if ($address instanceof Address) {
360
            $address = $address->data;
361
        }
362
        if (($key = array_search($address, $this->data->address)) !== false) {
363
            array_splice($this->data->address, $key, 1);
364
        }
365
    }
366
367
    /**
368
     * Returns the user's preferred address
369
     * 
370
     * @return Address|null The address object or null if none are preferred
371
     */
372
    public function getPreferredAddress()
373
    {
374
        foreach ($this->data->address as $address) {
375
            if ($address->preferred) {
376
                return Address::make($this->client, $address);
377
            }
378
        }
379
        return null;
380
    }
381
382
    /**
383
     * Removes the preferred flag from all addresses
384
     */
385
    public function unsetPreferredAddress()
386
    {
387
        foreach ($this->data->address as $address) {
388
            if ($address->preferred) {
389
                $address->preferred = false;
390
            }
391
        }
392
    }
393
}
394