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