Completed
Pull Request — master (#6)
by Christopher
15:40 queued 08:58
created

User::authoriser()   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
                    $extension = pathinfo($socialAuth->getMeta()['photo-file-name'], PATHINFO_EXTENSION);
116
                    if (empty($photoFingerprint)) {
117
                        continue;
118
                    }
119
                    switch ($size) {
120 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...
121
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/tn/'.$photoFingerprint.'.'.$extension;
122
                            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...
123 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...
124
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/small/'.$photoFingerprint.'.'.$extension;
125
                            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...
126 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...
127
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/medium/'.$photoFingerprint.'.'.$extension;
128
                            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...
129 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...
130
                            return $baseUrl.'/system/production/people/photos/'.$socialAuth->getSocialId().'/tn/'.$photoFingerprint.'.'.$extension;
131
                        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...
132
                    }
133
                }
134
            }
135
        }
136
137
        switch ($size) {
138 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...
139
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=50&d=mm';
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 'medium': // 200px x 200px
142
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=200&d=mm';
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 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...
145
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=500&d=mm';
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
            default:
148
                return '//www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=50&d=mm';
149
            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...
150
        }
151
    }
152
153
    /**
154
     * Get array of roles currently assigned to the user.
155
     *
156
     * @return array of Role() objects
157
     */
158
    public function getCurrentRoles()
159
    {
160
        $userRoles = $this->getUserRoles();
161
162
        $roles = [];
163
        foreach ($userRoles as $userRole) {
164
            $roles[] = $userRole->getRole();
165
        }
166
167
        return $roles;
168
    }
169
170
    /**
171
     * Get object of upcoming events currently assigned to the user.
172
     *
173
     * @return array of Event() objects
174
     */
175
    public function getUpcomingEvents()
176
    {
177
        return EventQuery::create()->filterByDate(['min' => new DateTime()])->useEventPersonQuery()->useUserRoleQuery()->filterByUser($this)->endUse()->endUse()->distinct()->find();
178
    }
179
180
    /**
181
     * Get object of upcoming events currently assigned to the user.
182
     *
183
     * @return array of Event() objects
184
     */
185
    public function getRolesInEvent(Event $event)
186
    {
187
        return RoleQuery::create()->useUserRoleQuery()->filterByUser($this)->useEventPersonQuery()->filterByEvent($event)->endUse()->endUse()->orderByName()->find();
188
    }
189
190
    public function getCurrentNotifications()
191
    {
192
        return NotificationQuery::create()->filterByUser($this)->filterByDismissed(false)->filterByArchived(false)->orderByTimestamp('desc')->find();
193
    }
194
195
    public function getUnreadNotifications()
196
    {
197
        return NotificationQuery::create()->filterBySeen(false)->filterByUser($this)->filterByDismissed(false)->filterByArchived(false)->orderByTimestamp('desc')->find();
198
    }
199
200
    /**
201
     * Determine if the user is marked as available for an event.
202
     *
203
     * @return bool if user is available
204
     */
205
    public function isAvailableForEvent(Event $event)
206
    {
207
        $availability = AvailabilityQuery::create()
208
            ->filterByUser($this)
209
            ->filterByEvent($event)
210
            ->findOne();
211
212
        if (is_null($availability)) {
213
            return;
214
        }
215
216
        return (bool) $availability->getAvailable();
217
    }
218
219
    /**
220
     * Determine if the user is marked as available for an event.
221
     *
222
     * @return \TechWilk\Rota\Availability
223
     */
224
    public function getAvailabilityForEvent(Event $event)
225
    {
226
        return AvailabilityQuery::create()
227
            ->filterByUser($this)
228
            ->filterByEvent($event)
229
            ->findOne();
230
    }
231
232
    public function getActiveCalendarTokens()
233
    {
234
        return CalendarTokenQuery::create()
235
            ->filterByUser($this)
236
            ->filterByRevoked(false)
237
            ->find();
238
    }
239
240 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...
241
    {
242
        return EventQuery::create()
243
            ->useAvailabilityQuery()
244
                ->filterByUser($this)
245
                ->filterByAvailable(true)
246
            ->endUse()
247
            ->filterByRemoved(false)
248
            ->filterByDate(['min' => new DateTime()])
249
            ->orderByDate('asc')
250
            ->find();
251
    }
252
253 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...
254
    {
255
        return EventQuery::create()
256
            ->useAvailabilityQuery()
257
                ->filterByUser($this)
258
                ->filterByAvailable(false)
259
            ->endUse()
260
            ->filterByRemoved(false)
261
            ->filterByDate(['min' => new DateTime()])
262
            ->orderByDate('asc')
263
            ->find();
264
    }
265
266
    public function upcomingEventsAwaitingResponse()
267
    {
268
        $eventsWithResponse = EventQuery::create()
269
            ->useAvailabilityQuery()
270
                ->filterByUser($this)
271
            ->endUse()
272
            ->find();
273
274
        $eventsWithResponseArray = [];
275
276
        foreach ($eventsWithResponse as $event) {
277
            $eventsWithResponseArray[] = $event->getId();
278
        }
279
280
        $events = EventQuery::create()
281
            ->useAvailabilityQuery(null, Criteria::LEFT_JOIN)
282
                ->filterByUser($this, Criteria::NOT_EQUAL)
283
                ->_or()
284
                ->filterById(null)
285
            ->endUse()
286
            ->filterByRemoved(false)
287
            ->filterByDate(['min' => new DateTime()])
288
            ->orderByDate('asc')
289
            ->distinct()
290
            ->find();
291
292
        $eventsArray = [];
293
294
        foreach ($events as $event) {
295
            if (!in_array($event->getId(), $eventsWithResponseArray)) {
296
                $eventsArray[] = $event;
297
            }
298
        }
299
300
        return $eventsArray;
301
    }
302
303
    public function getInitials()
304
    {
305
        $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...
306
        $names = explode(' ', $names);
307
        $initials = '';
308
        foreach ($names as $name) {
309
            $initials .= substr($name, 0, 1);
310
        }
311
312
        return $initials;
313
    }
314
315
    public function authoriser()
316
    {
317
        return new UserAuthoriser($this);
318
    }
319
}
320