PowerKiKi /
mqueue
| 1 | <?php |
||
| 2 | |||
| 3 | namespace mQueue\Model; |
||
| 4 | |||
| 5 | use mQueue\Model\User as DefaultModelUser; |
||
| 6 | use Zend_Session_Namespace; |
||
| 7 | |||
| 8 | class User extends AbstractModel |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The current user logged in. |
||
| 12 | * -1 before initialization |
||
| 13 | * null if no user logged in |
||
| 14 | * \mQueue\Model\User if logged in |
||
| 15 | * |
||
| 16 | * @var User |
||
| 17 | */ |
||
| 18 | private static $currentUser = -1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Returns the user currently logged in or null |
||
| 22 | * |
||
| 23 | * @return null|User |
||
| 24 | */ |
||
| 25 | 13 | public static function getCurrent() |
|
| 26 | { |
||
| 27 | 13 | if (is_int(self::$currentUser)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 28 | 1 | $session = new Zend_Session_Namespace(); |
|
| 29 | 1 | if (isset($session->idUser)) { |
|
| 30 | self::$currentUser = UserMapper::find($session->idUser); |
||
| 31 | } else { |
||
| 32 | 1 | self::$currentUser = null; |
|
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | 13 | return self::$currentUser; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Set the user currently logged in, or log him out |
||
| 41 | * |
||
| 42 | * @param User $user |
||
| 43 | */ |
||
| 44 | 2 | public static function setCurrent(DefaultModelUser $user = null): void |
|
| 45 | { |
||
| 46 | 2 | $session = new Zend_Session_Namespace(); |
|
| 47 | 2 | $session->idUser = $user ? $user->id : null; |
|
|
0 ignored issues
–
show
The property
id does not exist on mQueue\Model\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 48 | 2 | self::$currentUser = $user; |
|
| 49 | 2 | } |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Returns movie ratings statistics |
||
| 53 | * |
||
| 54 | * @return array of count of movies per ratings |
||
| 55 | */ |
||
| 56 | 1 | public function getStatistics() |
|
| 57 | { |
||
| 58 | 1 | return StatusMapper::getStatistics($this); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Override parent to auto-logout when deleting logged in user |
||
| 63 | */ |
||
| 64 | 2 | public function delete() |
|
| 65 | { |
||
| 66 | 2 | if (DefaultModelUser::getCurrent() == $this) { |
|
| 67 | 2 | DefaultModelUser::setCurrent(null); |
|
| 68 | } |
||
| 69 | |||
| 70 | 2 | return parent::delete(); |
|
| 71 | } |
||
| 72 | } |
||
| 73 |