User::setSmsNumber()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\Model\LazyResource;
7
8
/**
9
 * A single User resource.
10
 */
11
class User extends LazyResource
12
{
13
    /**
14
     * The primary id or some other id that can be used to fetch user information.
15
     *
16
     * @var string
17
     */
18
    public $id;
19
20
    /**
21
     * @var UserIdentifiers
22
     */
23
    protected $_identifiers;
24
25
    /**
26
     * @var Loans
27
     */
28
    public $loans;
29
30
    /**
31
     * @var Fees
32
     */
33
    public $fees;
34
35
    /**
36
     * @var Requests
37
     */
38
    public $requests;
39
40
    /**
41
     * User constructor.
42
     *
43
     * @param Client $client
44
     * @param string $id
45
     */
46
    public function __construct(Client $client, $id)
47
    {
48
        parent::__construct($client);
49
        $this->id = $id;
50
        $this->loans = Loans::make($this->client, $this);
51
        $this->fees = Fees::make($this->client, $this);
52
        $this->requests = Requests::make($this->client, $this->url('/requests'));
53
    }
54
55
    /**
56
     * Get the user id the object was constructed with. This might or might not be the primary id.
57
     * The only usefulness of this method over getPrimaryId() is that it will not trigger loading of the full object.
58
     *
59
     * @return string
60
     */
61
    public function getUserId()
62
    {
63
        return $this->id;
64
    }
65
66
    /**
67
     * Get the primary id. No need to load the full record for this.
68
     *
69
     * @return string|null
70
     */
71
    public function getPrimaryId()
72
    {
73
        return $this->primary_id;
74
    }
75
76
    /**
77
     * Get the user identifiers.
78
     *
79
     * @return UserIdentifiers
80
     */
81
    public function getIdentifiers()
82
    {
83
        return $this->init()->_identifiers;
84
    }
85
86
    /**
87
     * Get the user's preferred SMS number.
88
     *
89
     * @return string|null
90
     */
91 View Code Duplication
    public function getSmsNumber()
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...
92
    {
93
        $this->init();
94
        if ($this->data->contact_info->phone) {
95
            foreach ($this->data->contact_info->phone as $phone) {
96
                if ($phone->preferred_sms) {
97
                    return $phone->phone_number;
98
                }
99
            }
100
        }
101
        return null;
102
    }
103
 
104
    /**
105
     * Remove the preferred SMS flag from any number.
106
     */
107 View Code Duplication
    public function unsetSmsNumber()
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...
108
    {
109
        $this->init();
110
        if ($this->data->contact_info->phone) {
111
            foreach ($this->data->contact_info->phone as $phone) {
112
                if ($phone->preferred_sms) {
113
                    $phone->preferred_sms = false;
114
                }
115
            }
116
        }
117
    }
118
119
    /**
120
     * Set the user's preferred SMS number, creating a new internal mobile number if needed
121
     * @param $number string The SMS-capable mobile phone number
122
     */
123
    public function setSmsNumber($number)
124
    {
125
        $currentNumber = $this->getSmsNumber();
126
        if ($number === $currentNumber) {
127
            return;
128
        }
129
        $this->unsetSmsNumber();
130
        $updated = false;
131
        if ($this->data->contact_info->phone) {
132
            foreach ($this->data->contact_info->phone as $phone) {
133
                if ($phone->phone_number === $number) {
134
                    $phone->preferred_sms = true;
135
                }
136
            }
137
        }
138
        if (!$updated) {
139
            $this->addSmsNumber($number);
140
        }
141
    }
142
143
    /**
144
     * Add the user's preferred SMS number as a new internal mobile number.
145
     * @param $number string The SMS-capable mobile phone number
146
     */
147
    public function addSmsNumber($number)
148
    {
149
        $currentNumber = $this->getSmsNumber();
150
        if ($currentNumber) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentNumber of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
151
            $this->unsetSmsNumber();
152
        }
153
        $phones = json_decode(json_encode($this->data->contact_info->phone), true);
154
        $phones[] = json_decode('{"phone_number":'.json_encode($number).',"preferred":false,"preferred_sms":true,"segment_type":"Internal","phone_type":[{"value":"mobile","desc":"Mobile"}]}', true);
155
        $this->data->contact_info->phone = json_decode(json_encode($phones));
156
    }
157
158
    /**
159
     * Save the user
160
     * 
161
     * @return string The API response body
162
     */
163
    public function save()
164
    {
165
        $this->init();
166
        return $this->client->put($this->url(), json_encode($this->jsonSerialize()));
167
    }
168
169
    /**
170
     * Check if we have the full representation of our data object.
171
     *
172
     * @param \stdClass $data
173
     *
174
     * @return bool
175
     */
176
    protected function isInitialized($data)
177
    {
178
        return isset($data->user_identifier);
179
    }
180
181
    /**
182
     * Called when data is available to be processed.
183
     *
184
     * @param mixed $data
185
     */
186
    protected function onData($data)
187
    {
188
        $this->_identifiers = UserIdentifiers::make($this->client, $data);
189
    }
190
191
    /**
192
     * Generate the base URL for this resource.
193
     *
194
     * @return string
195
     */
196
    protected function urlBase()
197
    {
198
        return sprintf('/users/%s', rawurlencode($this->id));
199
    }
200
201
    public function __get($key)
202
    {
203
        // If there's a getter method, call it.
204
        $method = 'get' . ucfirst($key);
205
        if (method_exists($this, $method)) {
206
            return $this->$method();
207
        }
208
209
        // If the property is defined in our data object, return it.
210
        if (isset($this->data->{$key})) {
211
            return $this->data->{$key};
212
        }
213
214
        // Load the full record if needed.
215
        $this->init();
216
217
        // If there's a getter method on the UserIdentifiers object
218
        // (getBarcode, getPrimaryId), call it.
219
        if (method_exists($this->identifiers, $method)) {
220
            return $this->identifiers->$method();
221
        }
222
223
        // Re-check if there's a property on our data object
224
        if (isset($this->data->{$key})) {
225
            return $this->data->{$key};
226
        }
227
    }
228
}
229