Completed
Branch MagicUser (b8f336)
by Patrick
03:44
created

PendingUser::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * PendingUser class
4
 *
5
 * This file describes the PendingUser classes
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
namespace Auth;
15
16
/**
17
 * A class to abstract access to PendingUsers (users that have not completed registration) regardless of the Authentication type used.
18
 *
19
 * This class is the primary method to access pending user information.
20
 */
21
class PendingUser extends User
22
{
23
    protected $intData = array();
24
25
    public function getHash()
26
    {
27
        return false;
28
    }
29
30
    public function getRegistrationTime()
31
    {
32
        return false;
33
    }
34
35
    /**
36
     * Is this user in the Group or a child of that group?
37
     *
38
     * @param string $name The name of the group to check if the user is in
39
     *
40
     * @return boolean True if the user is in the group, false otherwise
41
     */
42
    public function isInGroupNamed($name)
43
    {
44
        return false;
45
    }
46
47
    /**
48
     * The email address for the user
49
     *
50
     * @return string The user's email address
51
     */
52
    public function getEmail()
53
    {
54
        if(isset($this->email))
55
        {
56
            return $this->email;
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<Auth\PendingUser>. 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...
57
        }
58
        return parent::getEmail();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::getEmail(); (boolean) is incompatible with the return type documented by Auth\PendingUser::getEmail of type string.

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...
59
    }
60
61
    public function __get($propName)
62
    {
63
        if(isset($this->intData[$propName]))
64
        {
65
            return $this->intData[$propName];
66
        }
67
        return parent::__get($propName);
68
    }
69
70
    public function __set($propName, $value)
71
    {
72
        $this->intData[$propName] = $value;
73
    }
74
75
    /**
76
     * The last name for the user
77
     *
78
     * @return string The user's last name
79
     */
80
    public function getLastName()
81
    {
82
        if(isset($this->sn))
83
        {
84
            return $this->sn;
0 ignored issues
show
Documentation introduced by
The property sn does not exist on object<Auth\PendingUser>. 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...
85
        }
86
        return parent::getLastName();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::getLastName(); (boolean) is incompatible with the return type documented by Auth\PendingUser::getLastName of type string.

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...
87
    }
88
89
    /**
90
     * Get the user's password as specified during registration
91
     *
92
     * We need the ability to obtain the user's unhashed plain text password to allow for it to be sent 
93
     * to the correct backend which will hash it
94
     *
95
     * @return boolean|string The current password
96
     */
97
    public function getPassword()
98
    {
99
        return false;
100
    }
101
102
    /**
103
     * The supplemental login types that the user can use to login
104
     *
105
     * @return array The user's login providers
106
     */
107
    public function getLoginProviders()
108
    {
109
        if(isset($this->host))
110
        {
111
            return $this->host;
0 ignored issues
show
Documentation introduced by
The property host does not exist on object<Auth\PendingUser>. 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...
112
        }
113
        return parent::getLoginProviders();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::getLoginProviders(); (boolean) is incompatible with the return type documented by Auth\PendingUser::getLoginProviders of type array.

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...
114
    }
115
116
    /**
117
     * Add a supplemental login type that the user can use to login
118
     *
119
     * @param string $provider The hostname for the provider
120
     *
121
     * @return true|false true if the addition worked, false otherwise
122
     */
123
    public function addLoginProvider($provider)
124
    {
125
        if(isset($this->host))
126
        {
127
            array_push($this->host, $provider);
0 ignored issues
show
Documentation introduced by
The property host does not exist on object<Auth\PendingUser>. 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
        }
129
        else
130
        {
131
            $this->host = array($provider);
0 ignored issues
show
Documentation introduced by
The property host does not exist on object<Auth\PendingUser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
132
        }
133
    }
134
135
    /**
136
     * Set the user's email address
137
     *
138
     * @param string $email The user's new email address
139
     *
140
     * @return boolean true if the user's email address was changed, false otherwise
141
     */
142
    public function setEmail($email)
143
    {
144
        $this->email = $email;
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<Auth\PendingUser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
145
        return true;
146
    }
147
148
    /**
149
     * Set the user's given (first) name
150
     *
151
     * @param string $givenName The user's new given name
152
     *
153
     * @return boolean true if the user's given name was changed, false otherwise
154
     */
155
    public function setGivenName($givenName)
156
    {
157
        $this->givenName = $givenName;
0 ignored issues
show
Documentation introduced by
The property givenName does not exist on object<Auth\PendingUser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
158
        return true;
159
    }
160
161
    /**
162
     * Set the user's last name
163
     *
164
     * @param string $sn The user's new last name
165
     *
166
     * @return boolean true if the user's last name was changed, false otherwise
167
     */
168
    public function setLastName($sn)
169
    {
170
        $this->sn = $sn;
0 ignored issues
show
Documentation introduced by
The property sn does not exist on object<Auth\PendingUser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
171
        return true;
172
    }
173
174
    /**
175
     * Serialize the user data into a format usable by the json_encode method
176
     *
177
     * @return array A simple keyed array representing the user
178
     */
179
    public function jsonSerialize()
180
    {
181
        $user = array();
182
        $user['hash'] = $this->getHash();
183
        $user['mail'] = $this->getEmail();
184
        $user['uid'] = $this->getUid();
185
        $time = $this->getRegistrationTime();
186
        if($time !== false)
187
        {
188
            $user['time'] = $time->format(\DateTime::RFC822);
0 ignored issues
show
Bug introduced by
The method format cannot be called on $time (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
189
        }
190
        $user['class'] = get_class($this);
191
        return $user; 
192
    }
193
194
    public function sendEmail()
195
    {
196
        $email_msg = new \Email\Email();
197
        $email_msg->addToAddress($this->getEmail());
198
        $email_msg->setTextBody('Thank you for signing up with Burning Flipside. Your registration is not complete until you goto the address below.
199
                https://profiles.burningflipside.com/finish.php?hash='.$this->getHash().'
200
                Thank you,
201
                Burning Flipside Technology Team');
202
        $email_msg->setHTMLBody('Thank you for signing up with Burning Flipside. Your registration is not complete until you follow the link below.<br/>
203
                <a href="https://profiles.burningflipside.com/finish.php?hash='.$this->getHash().'">Complete Registration</a><br/>
204
                Thank you,<br/>
205
                Burning Flipside Technology Team');
206
        $email_msg->setSubject('Burning Flipside Registration');
207
        $email_provider = \EmailProvider::getInstance();
208
        if($email_provider->sendEmail($email_msg) === false)
209
        {
210
            throw new \Exception('Unable to send email!');
211
        }
212
        return true;
213
    }
214
215
    public function delete()
216
    {
217
    }
218
}
219
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
220