Completed
Push — master ( 4cb1ea...24446f )
by Christopher
13:20 queued 04:57
created

User::isAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 'cr_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 [password] column.
24
     *
25
     * @param string $v new value
26
     *
27
     * @return $this|\User The current object (for fluent API support)
28
     */
29
    public function setPassword($v)
30
    {
31
        if ($v !== null) {
32
            $v = (string) $v;
33
        }
34
35
        if (!password_verify($v, $this->password)) {
36
            $bcrypt_options = [
37
        'cost' => 12,
38
      ];
39
            $this->password = password_hash($v, PASSWORD_BCRYPT, $bcrypt_options);
40
41
            $this->modifiedColumns[UserTableMap::COL_PASSWORD] = true;
42
        }
43
44
        return $this;
45
    }
46
47
    // setPassword()
48
49
    /**
50
     * Check a plain text password against the value of [password] column.
51
     *
52
     * @param string $v plain text password
53
     *
54
     * @return $this|\User The current object (for fluent API support)
55
     */
56
    public function checkPassword($v)
57
    {
58
        if ($v !== null) {
59
            $v = (string) $v;
60
        } else {
61
            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...
62
        }
63
64
        return password_verify($v, $this->password);
65
    }
66
67
    // checkPassword()
68
69
    public function isAdmin()
70
    {
71
        return $this->isadmin;
72
    }
73
74
    /**
75
     * Get the [firstname] and [lastname] column value concatenated with a space.
76
     *
77
     * @return string
78
     */
79
    public function getName()
80
    {
81
        return $this->firstname.' '.$this->lastname;
82
    }
83
84
    /**
85
     * Get the URL for the user's profile image.
86
     *
87
     * @param string $size either 'small' or 'large'
88
     *
89
     * @return string
90
     */
91
    public function getProfileImage($size)
92
    {
93
        $socialAuths = $this->getSocialAuths();
94
95
        if (isset($socialAuths)) {
96
            foreach ($socialAuths as $socialAuth) {
97
                if ($socialAuth->getPlatform() == 'facebook') {
98
                    switch ($size) {
99
                        case 'small': // 50px x 50px
100
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture?type=square';
101
                            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...
102
                        case 'medium': // 200px x 200px
103
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture?type=large';
104
                            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...
105
                        case 'large': // 200px x 200px
106
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture?type=large';
107
                            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...
108
                        default:
109
                            return '//graph.facebook.com/'.$socialAuth->getSocialId().'/picture';
110
                            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...
111
                    }
112
                } elseif ($socialAuth->getPlatform() == 'onebody') {
113
                    $baseUrl = getConfig()['auth']['onebody']['url'];
114
                    $photoFingerprint = $socialAuth->getMeta()['photo-fingerprint'];
115
                    if (empty($photoFingerprint) || !is_string($photoFingerprint)) {
116
                        // OneBody doesn't actually have a photo (or we don't know its URL)
117
                        continue;
118
                    }
119
                    $extension = pathinfo($socialAuth->getMeta()['photo-file-name'], PATHINFO_EXTENSION);
120
                    switch ($size) {
121 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...
122
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/tn/'.$photoFingerprint.'.'.$extension;
123
                            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...
124 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...
125
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/small/'.$photoFingerprint.'.'.$extension;
126
                            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...
127 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...
128
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/medium/'.$photoFingerprint.'.'.$extension;
129
                            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...
130 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...
131
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/tn/'.$photoFingerprint.'.'.$extension;
132
                        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...
133
                    }
134
                }
135
            }
136
        }
137
138
        switch ($size) {
139 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...
140
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=50&d=mm';
141
                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...
142
            case 'medium': // 200px x 200px
143
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=200&d=mm';
144
                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...
145 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...
146
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=500&d=mm';
147
                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...
148
            default:
149
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=50&d=mm';
150
            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...
151
        }
152
    }
153
154
    /**
155
     * Get array of roles currently assigned to the user.
156
     *
157
     * @return array of Role() objects
158
     */
159
    public function getCurrentRoles()
160
    {
161
        $userRoles = $this->getUserRoles();
162
163
        $roles = [];
164
        foreach ($userRoles as $userRole) {
165
            $roles[] = $userRole->getRole();
166
        }
167
168
        return $roles;
169
    }
170
171
    /**
172
     * Get object of upcoming events currently assigned to the user.
173
     *
174
     * @return array of Event() objects
175
     */
176
    public function getUpcomingEvents()
177
    {
178
        return EventQuery::create()->filterByDate(['min' => new DateTime()])->useEventPersonQuery()->useUserRoleQuery()->filterByUser($this)->endUse()->endUse()->distinct()->find();
179
    }
180
181
    /**
182
     * Get object of upcoming events currently assigned to the user.
183
     *
184
     * @return array of Event() objects
185
     */
186
    public function getRolesInEvent(Event $event)
187
    {
188
        return RoleQuery::create()->useUserRoleQuery()->filterByUser($this)->useEventPersonQuery()->filterByEvent($event)->endUse()->endUse()->orderByName()->find();
189
    }
190
191
    public function getCurrentNotifications()
192
    {
193
        return NotificationQuery::create()->filterByUser($this)->filterByDismissed(false)->filterByArchived(false)->orderByTimestamp('desc')->find();
194
    }
195
196
    public function getUnreadNotifications()
197
    {
198
        return NotificationQuery::create()->filterBySeen(false)->filterByUser($this)->filterByDismissed(false)->filterByArchived(false)->orderByTimestamp('desc')->find();
199
    }
200
201
    /**
202
     * Determine if the user is marked as available for an event.
203
     *
204
     * @return bool if user is available
205
     */
206
    public function isAvailableForEvent(Event $event)
207
    {
208
        $availability = AvailabilityQuery::create()
209
            ->filterByUser($this)
210
            ->filterByEvent($event)
211
            ->findOne();
212
213
        if (is_null($availability)) {
214
            return;
215
        }
216
217
        return (bool) $availability->getAvailable();
218
    }
219
220
    /**
221
     * Determine if the user is marked as available for an event.
222
     *
223
     * @return \TechWilk\Rota\Availability
224
     */
225
    public function getAvailabilityForEvent(Event $event)
226
    {
227
        return AvailabilityQuery::create()
228
            ->filterByUser($this)
229
            ->filterByEvent($event)
230
            ->findOne();
231
    }
232
233
    public function getActiveCalendarTokens()
234
    {
235
        return CalendarTokenQuery::create()
236
            ->filterByUser($this)
237
            ->filterByRevoked(false)
238
            ->find();
239
    }
240
241 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...
242
    {
243
        return EventQuery::create()
244
            ->useAvailabilityQuery()
245
                ->filterByUser($this)
246
                ->filterByAvailable(true)
247
            ->endUse()
248
            ->filterByRemoved(false)
249
            ->filterByDate(['min' => new DateTime()])
250
            ->orderByDate('asc')
251
            ->find();
252
    }
253
254 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...
255
    {
256
        return EventQuery::create()
257
            ->useAvailabilityQuery()
258
                ->filterByUser($this)
259
                ->filterByAvailable(false)
260
            ->endUse()
261
            ->filterByRemoved(false)
262
            ->filterByDate(['min' => new DateTime()])
263
            ->orderByDate('asc')
264
            ->find();
265
    }
266
267
    public function upcomingEventsAwaitingResponse()
268
    {
269
        $eventsWithResponse = EventQuery::create()
270
            ->useAvailabilityQuery()
271
                ->filterByUser($this)
272
            ->endUse()
273
            ->find();
274
275
        $eventsWithResponseArray = [];
276
277
        foreach ($eventsWithResponse as $event) {
278
            $eventsWithResponseArray[] = $event->getId();
279
        }
280
281
        $events = EventQuery::create()
282
            ->useAvailabilityQuery(null, Criteria::LEFT_JOIN)
283
                ->filterByUser($this, Criteria::NOT_EQUAL)
284
                ->_or()
285
                ->filterById(null)
286
            ->endUse()
287
            ->filterByRemoved(false)
288
            ->filterByDate(['min' => new DateTime()])
289
            ->orderByDate('asc')
290
            ->distinct()
291
            ->find();
292
293
        $eventsArray = [];
294
295
        foreach ($events as $event) {
296
            if (!in_array($event->getId(), $eventsWithResponseArray)) {
297
                $eventsArray[] = $event;
298
            }
299
        }
300
301
        return $eventsArray;
302
    }
303
304
    public function getInitials()
305
    {
306
        $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...
307
        $names = explode(' ', $names);
308
        $initials = '';
309
        foreach ($names as $name) {
310
            $initials .= substr($name, 0, 1);
311
        }
312
313
        return $initials;
314
    }
315
316
    public function getSocialAuthForPlatform($platform)
317
    {
318
        return SocialAuthQuery::create()
319
            ->filterByUser($this)
320
            ->filterByPlatform($platform)
321
            ->findOne();
322
    }
323
324
    public function authoriser()
325
    {
326
        return new UserAuthoriser($this);
327
    }
328
}
329