UserInvitation::setInitialState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\Users\Models;
4
5
use App\User;
6
use Illuminate\Database\Eloquent\Model;
7
8
use Acacha\Stateful\Traits\StatefulTrait;
9
use Acacha\Stateful\Contracts\Stateful;
10
use Venturecraft\Revisionable\RevisionableTrait;
11
12
/**
13
 * Class UserInvitation.
14
 *
15
 * @package Acacha\Users\Models
16
 */
17
class UserInvitation extends Model implements Stateful
18
{
19
    use StatefulTrait, RevisionableTrait;
20
21
    public static function boot()
22
    {
23
        parent::boot();
24
    }
25
26
    /**
27
     * The attributes that are mass assignable.
28
     *
29
     * @var array
30
     */
31
    protected $fillable = ['email','state','token'];
32
33
    /**
34
     * Enable revisions on create.
35
     *
36
     * @var array
37
     */
38
    protected $revisionCreationsEnabled = true;
39
40
    /**
41
     * Transaction States
42
     *
43
     * @var array
44
     */
45
    protected $states = [
46
        'pending'  => ['initial' => true],
47
        'accepted' => ['final' => true]
48
    ];
49
50
    /**
51
     * Transaction State Transitions
52
     *
53
     * @var array
54
     */
55
    protected $transitions = [
56
        'accept' => [
57
            'from' => ['pending'],
58
            'to' => 'accepted'
59
        ]
60
    ];
61
62
    /**
63
     * Set the initial state.
64
     *
65
     * @return void
66
     */
67
    public function setInitialState()
68
    {
69
        $this->setAttribute($this->getStateColumn(), $this->getInitialState());
70
        $this->token = hash_hmac('sha256', str_random(40), env('APP_KEY'));
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Acacha\Users\Models\UserInvitation>. 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...
71
    }
72
73
    /**
74
     * Get the user record associated with the invitation.
75
     */
76
    public function user()
77
    {
78
        return $this->belongsTo(User::class);
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    protected function validateAccept()
85
    {
86
        if ($this->user_id != null && $this->user()) return true;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Acacha\Users\Models\UserInvitation>. 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...
87
        return false;
88
    }
89
}
90
91