Completed
Push — master ( cc3347...ae7860 )
by Adrien
01:57
created

User::getStatistics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 \mQueue\Model\User
17
     */
18
    private static $currentUser = -1;
19
20
    /**
21
     * Returns the user currently logged in or null
22
     *
23
     * @return null|\mQueue\Model\User
24
     */
25 13
    public static function getCurrent()
26
    {
27 13
        if (is_int(self::$currentUser)) {
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 \mQueue\Model\User $user
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user not be null|User?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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
Documentation introduced by
The property id does not exist on object<mQueue\Model\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<*,integer>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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