Completed
Push — master ( f8f581...ddd2bd )
by Christopher
13s
created

User::setFirstName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TechWilk\Rota;
4
5
use DateTime;
6
use Propel\Runtime\ActiveQuery\Criteria;
7
use TechWilk\Rota\Authoriser\UserAuthoriser;
8
use TechWilk\Rota\Base\User as BaseUser;
9
use TechWilk\Rota\Map\UserTableMap;
10
11
/**
12
 * Skeleton subclass for representing a row from the 'users' table.
13
 *
14
 *
15
 *
16
 * You should add additional methods to this class to meet the
17
 * application requirements.  This class will only be generated as
18
 * long as it does not already exist in the output directory.
19
 */
20
class User extends BaseUser
21
{
22
    /**
23
     * Set the value of [firstname] column.
24
     *
25
     * @param string $v new value
26
     *
27
     * @return $this|\TechWilk\Rota\User The current object (for fluent API support)
28
     */
29
    public function setFirstName($v)
30
    {
31
        if ($v !== null) {
32
            $v = (string) trim($v);
33
        }
34
35
        return parent::setFirstName($v);
36
    }
37
38
    // setFirstName()
39
40
    /**
41
     * Set the value of [lastname] column.
42
     *
43
     * @param string $v new value
44
     *
45
     * @return $this|\TechWilk\Rota\User The current object (for fluent API support)
46
     */
47
    public function setLastName($v)
48
    {
49
        if ($v !== null) {
50
            $v = (string) trim($v);
51
        }
52
53
        return parent::setLastName($v);
54
    }
55
56
    // setLastName()
57
58
    /**
59
     * Set the value of [password] column.
60
     *
61
     * @param string $v new value
62
     *
63
     * @return $this|\User The current object (for fluent API support)
64
     */
65
    public function setPassword($v)
66
    {
67
        if ($v !== null) {
68
            $v = (string) $v;
69
        }
70
71
        if (!password_verify($v, $this->password)) {
72
            $bcrypt_options = [
73
        'cost' => 12,
74
      ];
75
            $this->password = password_hash($v, PASSWORD_BCRYPT, $bcrypt_options);
76
77
            $this->modifiedColumns[UserTableMap::COL_PASSWORD] = true;
78
        }
79
80
        return $this;
81
    }
82
83
    // setPassword()
84
85
    /**
86
     * Check a plain text password against the value of [password] column.
87
     *
88
     * @param string $v plain text password
89
     *
90
     * @return $this|\User The current object (for fluent API support)
91
     */
92
    public function checkPassword($v)
93
    {
94
        if ($v !== null) {
95
            $v = (string) $v;
96
        } else {
97
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by TechWilk\Rota\User::checkPassword of type TechWilk\Rota\User|User.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
98
        }
99
100
        return password_verify($v, $this->password);
101
    }
102
103
    // checkPassword()
104
105
    public function isAdmin()
106
    {
107
        return $this->isadmin;
108
    }
109
110
    /**
111
     * Get the [firstname] and [lastname] column value concatenated with a space.
112
     *
113
     * @return string
114
     */
115
    public function getName()
116
    {
117
        return $this->firstname.' '.$this->lastname;
118
    }
119
120
    /**
121
     * Get the URL for the user's profile image.
122
     *
123
     * @param string $size either 'small' or 'large'
124
     *
125
     * @return string
126
     */
127
    public function getProfileImage($size)
128
    {
129
        $socialAuths = $this->getSocialAuths();
130
131
        if (isset($socialAuths)) {
132
            foreach ($socialAuths as $socialAuth) {
133
                if ($socialAuth->getPlatform() == 'facebook') {
134
                    switch ($size) {
135
                        case 'small': // 50px x 50px
136
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture?type=square';
137
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
138
                        case 'medium': // 200px x 200px
139
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture?type=large';
140
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
141
                        case 'large': // 200px x 200px
142
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture?type=large';
143
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
144
                        default:
145
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture';
146
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
147
                    }
148
                } elseif ($socialAuth->getPlatform() == 'onebody') {
149
                    $baseUrl = getConfig()['auth']['onebody']['url'];
150
                    $photoFingerprint = $socialAuth->getMeta()['photo-fingerprint'];
151
                    if (empty($photoFingerprint) || !is_string($photoFingerprint)) {
152
                        // OneBody doesn't actually have a photo (or we don't know its URL)
153
                        continue;
154
                    }
155
                    $extension = pathinfo($socialAuth->getMeta()['photo-file-name'], PATHINFO_EXTENSION);
156
                    switch ($size) {
157 View Code Duplication
                        case 'small': // 50px x 50px
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...
158
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/tn/'.$photoFingerprint.'.'.$extension;
159
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
160 View Code Duplication
                        case 'medium': // 150px x 150px
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...
161
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/small/'.$photoFingerprint.'.'.$extension;
162
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
163 View Code Duplication
                        case 'large': // 500px x 500px
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...
164
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/medium/'.$photoFingerprint.'.'.$extension;
165
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
166 View Code Duplication
                        default:
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...
167
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/tn/'.$photoFingerprint.'.'.$extension;
168
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
169
                    }
170
                }
171
            }
172
        }
173
174
        switch ($size) {
175 View Code Duplication
            case 'small': // 50px x 50px
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...
176
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=50&d=mm';
177
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
178
            case 'medium': // 200px x 200px
179
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=200&d=mm';
180
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
181 View Code Duplication
            case 'large': // 500px x 500px
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...
182
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=500&d=mm';
183
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
184
            default:
185
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=50&d=mm';
186
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
187
        }
188
    }
189
190
    /**
191
     * Get array of roles currently assigned to the user.
192
     *
193
     * @return array of Role() objects
194
     */
195
    public function getCurrentRoles()
196
    {
197
        $userRoles = $this->getUserRoles();
198
199
        $roles = [];
200
        foreach ($userRoles as $userRole) {
201
            $roles[] = $userRole->getRole();
202
        }
203
204
        return $roles;
205
    }
206
207
    /**
208
     * Get object of upcoming events currently assigned to the user.
209
     *
210
     * @return array of Event() objects
211
     */
212
    public function getUpcomingEvents()
213
    {
214
        return EventQuery::create()->filterByDate(['min' => new DateTime()])->useEventPersonQuery()->useUserRoleQuery()->filterByUser($this)->endUse()->endUse()->distinct()->find();
215
    }
216
217
    /**
218
     * Get object of upcoming events currently assigned to the user.
219
     *
220
     * @return array of Event() objects
221
     */
222
    public function getRolesInEvent(Event $event)
223
    {
224
        return RoleQuery::create()->useUserRoleQuery()->filterByUser($this)->useEventPersonQuery()->filterByEvent($event)->endUse()->endUse()->orderByName()->find();
225
    }
226
227
    public function getCurrentNotifications()
228
    {
229
        return NotificationQuery::create()->filterByUser($this)->filterByDismissed(false)->filterByArchived(false)->orderByTimestamp('desc')->find();
230
    }
231
232
    public function getUnreadNotifications()
233
    {
234
        return NotificationQuery::create()->filterBySeen(false)->filterByUser($this)->filterByDismissed(false)->filterByArchived(false)->orderByTimestamp('desc')->find();
235
    }
236
237
    /**
238
     * Determine if the user is marked as available for an event.
239
     *
240
     * @return bool if user is available
241
     */
242
    public function isAvailableForEvent(Event $event)
243
    {
244
        $availability = AvailabilityQuery::create()
245
            ->filterByUser($this)
246
            ->filterByEvent($event)
247
            ->findOne();
248
249
        if (is_null($availability)) {
250
            return;
251
        }
252
253
        return (bool) $availability->getAvailable();
254
    }
255
256
    /**
257
     * Determine if the user is marked as available for an event.
258
     *
259
     * @return \TechWilk\Rota\Availability
260
     */
261
    public function getAvailabilityForEvent(Event $event)
262
    {
263
        return AvailabilityQuery::create()
264
            ->filterByUser($this)
265
            ->filterByEvent($event)
266
            ->findOne();
267
    }
268
269
    public function getActiveCalendarTokens()
270
    {
271
        return CalendarTokenQuery::create()
272
            ->filterByUser($this)
273
            ->filterByRevoked(false)
274
            ->find();
275
    }
276
277 View Code Duplication
    public function upcomingEventsAvailable()
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...
278
    {
279
        return EventQuery::create()
280
            ->useAvailabilityQuery()
281
                ->filterByUser($this)
282
                ->filterByAvailable(true)
283
            ->endUse()
284
            ->filterByRemoved(false)
285
            ->filterByDate(['min' => new DateTime()])
286
            ->orderByDate('asc')
287
            ->find();
288
    }
289
290 View Code Duplication
    public function upcomingEventsUnavailable()
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...
291
    {
292
        return EventQuery::create()
293
            ->useAvailabilityQuery()
294
                ->filterByUser($this)
295
                ->filterByAvailable(false)
296
            ->endUse()
297
            ->filterByRemoved(false)
298
            ->filterByDate(['min' => new DateTime()])
299
            ->orderByDate('asc')
300
            ->find();
301
    }
302
303
    public function upcomingEventsAwaitingResponse()
304
    {
305
        $eventsWithResponse = EventQuery::create()
306
            ->useAvailabilityQuery()
307
                ->filterByUser($this)
308
            ->endUse()
309
            ->find();
310
311
        $eventsWithResponseArray = [];
312
313
        foreach ($eventsWithResponse as $event) {
314
            $eventsWithResponseArray[] = $event->getId();
315
        }
316
317
        $events = EventQuery::create()
318
            ->useAvailabilityQuery(null, Criteria::LEFT_JOIN)
319
                ->filterByUser($this, Criteria::NOT_EQUAL)
320
                ->_or()
321
                ->filterById(null)
322
            ->endUse()
323
            ->filterByRemoved(false)
324
            ->filterByDate(['min' => new DateTime()])
325
            ->orderByDate('asc')
326
            ->distinct()
327
            ->find();
328
329
        $eventsArray = [];
330
331
        foreach ($events as $event) {
332
            if (!in_array($event->getId(), $eventsWithResponseArray)) {
333
                $eventsArray[] = $event;
334
            }
335
        }
336
337
        return $eventsArray;
338
    }
339
340
    public function getInitials()
341
    {
342
        $names = str_replace('-', ' ', $this->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
343
        $names = explode(' ', $names);
344
        $initials = '';
345
        foreach ($names as $name) {
346
            $initials .= substr($name, 0, 1);
347
        }
348
349
        return $initials;
350
    }
351
352
    public function getSocialAuthForPlatform($platform)
353
    {
354
        return SocialAuthQuery::create()
355
            ->filterByUser($this)
356
            ->filterByPlatform($platform)
357
            ->findOne();
358
    }
359
360
    public function authoriser()
361
    {
362
        return new UserAuthoriser($this);
363
    }
364
}
365