Completed
Push — master ( e45e47...d15cf4 )
by Valentyn
03:38
created

ConfirmationToken::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 3
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Entity;
5
6
use App\Users\Entity\User;
7
use Doctrine\ORM\Mapping as ORM;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
10
/**
11
 * @ORM\Entity(repositoryClass="App\Users\Repository\ConfirmationTokenRepository")
12
 * @ORM\Table(name="users_confirmation_tokens")
13
 * @UniqueEntity(fields="token", message="This token already taken")
14
 */
15
class ConfirmationToken
16
{
17
    const TYPE_CONFIRM_EMAIl = 'confirm_email';
18
19
    /**
20
     * @ORM\Id()
21
     * @ORM\GeneratedValue()
22
     * @ORM\Column(type="integer")
23
     */
24
    private $id;
25
26
    /**
27
     * @var $user User
28
     * @ORM\ManyToOne(targetEntity="App\Users\Entity\User")
29
     */
30
    private $user;
31
32
    /**
33
     * @ORM\Column(type="string", length=32, unique=true)
34
     */
35
    private $token;
36
37
    /**
38
     * @ORM\Column(type="string", length=256)
39
     */
40
    private $type;
41
42
    /**
43
     * @ORM\Column(type="datetime", nullable=true)
44
     * @var \DateTimeImmutable
45
     */
46
    private $expires_at;
47
48 10
    public function __construct(User $user, $type, \DateTimeInterface $expires_at = null)
49
    {
50 10
        if (in_array($type, $this->getValidTypes()) === false) {
51 1
            throw new \InvalidArgumentException(sprintf('$type should be valid type! Instead %s given', $type));
52
        }
53
54 9
        if ($expires_at !== null) {
55 7
            $now = new \DateTimeImmutable();
56 7
            if ($expires_at <= $now) {
57 1
                throw new \InvalidArgumentException(sprintf('You can not create already expired token'));
58
            }
59
        }
60
61
62 8
        $this->type = $type;
63 8
        $this->token = bin2hex(openssl_random_pseudo_bytes(16));
64 8
        $this->user = $user;
65 8
        $this->expires_at = $expires_at;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expires_at can also be of type object<DateTimeInterface>. However, the property $expires_at is declared as type object<DateTimeImmutable>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66 8
    }
67
68 10
    public function getValidTypes(): array
69
    {
70 10
        return [self::TYPE_CONFIRM_EMAIl];
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getId()
77
    {
78
        return $this->id;
79
    }
80
81
    /**
82
     * @return User
83
     */
84 1
    public function getUser(): User
85
    {
86 1
        return $this->user;
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92 5
    public function getToken()
93
    {
94 5
        return $this->token;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getType()
101
    {
102
        return $this->type;
103
    }
104
105 2
    public function isValid(): bool
106
    {
107 2
        if (!$this->expires_at) {
108
            return true;
109
        }
110
111 2
        $now = new \DateTimeImmutable();
112 2
        return $this->expires_at > $now;
113
    }
114
}