Completed
Pull Request — master (#20)
by Fredrik
03:15
created

User::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 58
rs 9.639
cc 1
eloc 43
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Anax\Users;
3
4
/**
5
 * Model for Users.
6
 *
7
 */
8
class User extends \Anax\MVC\CDatabaseModel
9
{
10
11
    /**
12
     * Initialize model.
13
     *
14
     * @return array
15
     */
16
    public function init()
17
    {
18
        // $this->db->setVerbose();
19
        $this->db->dropTableIfExists('user')->execute();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\Users\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...
20
21
        $this->db->createTable(
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\Users\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...
22
            'user',
23
            [
24
               'id' => ['integer', 'primary key', 'not null', 'auto_increment'],
25
               'acronym' => ['varchar(20)', 'unique', 'not null'],
26
               'email' => ['varchar(80)'],
27
               'name' => ['varchar(80)'],
28
               'points' => ['int'],
29
               'password' => ['varchar(255)'],
30
               'created' => ['datetime'],
31
               'updated' => ['datetime'],
32
               'deleted' => ['datetime'],
33
               'active' => ['datetime'],
34
            ]
35
        )->execute();
36
        $this->db->insert(
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\Users\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...
37
            'user',
38
            ['acronym', 'email', 'name', 'points', 'password', 'created', 'active']
39
        );
40
41
        $now = gmdate('Y-m-d H:i:s');
42
43
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\Users\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...
44
            'admin',
45
            '[email protected]',
46
            'Administrator',
47
            0,
48
            md5('admin'),
49
            $now,
50
            $now
51
        ]);
52
53
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\Users\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...
54
            'bob',
55
            '[email protected]',
56
            'Byggare Bob',
57
            0,
58
            md5('bob'),
59
            $now,
60
            $now
61
        ]);
62
63
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\Users\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...
64
            'fnlive',
65
            '[email protected]',
66
            'Fredrik Nilsson',
67
            0,
68
            md5('qwerty'),
69
            $now,
70
            $now
71
        ]);
72
73
    }
74
75
    public function find($id)
76
    {
77
        $user = parent::find($id);
78
        // Add Gravatar
79
        $gravatarSize = 80;
80
        $user->gravatar = $this->getGravatar($user->email, $gravatarSize);
81
        return $user;
82
    }
83
84
    /**
85
     * Get either a Gravatar URL or complete image tag for a specified email address.
86
     *
87
     * @param string $email The email address
88
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
89
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
90
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
91
     * @param boole $img True to return a complete IMG tag False for just the URL
92
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
93
     * @return String containing either just a URL or a complete image tag
94
     * @source http://gravatar.com/site/implement/images/php/
95
     */
96
    public static function getGravatar($email, $s = 80, $d = 'monsterid', $r = 'g', $img = false, $atts = array())
97
    {
98
        $url = 'http://www.gravatar.com/avatar/';
99
        $url .= md5(strtolower(trim($email)));
100
        $url .= "?s=$s&d=$d&r=$r";
101
        if ($img) {
102
            $url = '<img src="' . $url . '"';
103
            foreach ($atts as $key => $val) {
104
                $url .= ' ' . $key . '="' . $val . '"';
105
            }
106
            $url .= ' />';
107
        }
108
        return $url;
109
    }
110
111
    public function loggedIn()
112
    {
113
        if (null!==$this->session->get('user_logged_in')) {
0 ignored issues
show
Documentation introduced by
The property session does not exist on object<Anax\Users\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...
114
            // echo "You are logged in, {$this->session->get('user')}";
115
            return true;
116
        }
117
        // echo "You are logged out";
118
        return false;
119
    }
120
    /**
121
     * Get user with acronym.
122
     *
123
     * @return array $user
124
     */
125
    public function loggedInUser()
126
    {
127
        $userAcronym = $this->session->get('user_logged_in');
0 ignored issues
show
Documentation introduced by
The property session does not exist on object<Anax\Users\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...
128
        $user = $this->query()
129
        ->where('acronym =' . "'$userAcronym'")
130
        ->execute()[0];
131
        return $user;
132
    }
133
}
134