Completed
Push — master ( 9ce72a...1ceaf8 )
by Mārtiņš
02:28
created

EmailIdentity::createIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL logic for authentication attemps using username/password
7
 */
8
9
use Palladium\Component\DataMapper;
10
use Palladium\Entity as Entity;
11
use PDOStatement;
12
13
class EmailIdentity extends DataMapper
14
{
15
16
    /**
17
     * @param Entity\EmailIdentity $entity
18
     */
19
    public function exists(Entity\EmailIdentity $entity)
20
    {
21
        $sql = "SELECT 1
22
                  FROM {$this->table}
23
                 WHERE type = :type
24
                   AND fingerprint = :fingerprint
25
                   AND identifier = :email
26
                   AND (expires_on IS NULL OR expires_on > :now)";
27
28
        $statement = $this->connection->prepare($sql);
29
30
        $statement->bindValue(':type', Entity\EmailIdentity::TYPE_EMAIL);
31
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
32
        $statement->bindValue(':email', $entity->getEmailAddress());
33
        $statement->bindValue(':now', time());
34
35
        $statement->execute();
36
        $data = $statement->fetch();
37
38
        return empty($data) === false;
39
    }
40
41
42
    /**
43
     * @param Entity\EmailIdentity $entity
44
     */
45 View Code Duplication
    public function fetch(Entity\EmailIdentity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $sql = "SELECT identity_id      AS id,
48
                       account_id       AS accountId,
49
                       hash             AS hash,
50
                       status           AS status,
51
                       token            AS token,
52
                       token_action     AS tokenAction,
53
                       token_expires_on AS tokenEndOfLife
54
                  FROM {$this->table}
55
                 WHERE type = :type
56
                   AND fingerprint = :fingerprint
57
                   AND identifier = :email";
58
59
        $statement = $this->connection->prepare($sql);
60
61
        $statement->bindValue(':type', $entity->getType());
62
        $statement->bindValue(':email', $entity->getEmailAddress());
63
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
64
65
        $statement->execute();
66
67
        $data = $statement->fetch();
68
69
        if ($data) {
70
            $this->applyValues($entity, $data);
71
        }
72
    }
73
74
75
    /**
76
     * @param Entity\EmailIdentity $entity
77
     */
78 2
    public function store(Entity\EmailIdentity $entity)
79
    {
80 2
        if ($entity->getId() === null) {
81 1
            $this->createIdentity($entity);
82 1
            return;
83
        }
84
85 1
        $this->updateIdentity($entity);
86 1
    }
87
88
89 1
    private function createIdentity(Entity\EmailIdentity $entity)
90
    {
91 1
        $sql = "INSERT INTO {$this->table}
92
                       (type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on )
93
                VALUES (:type, :status, :email, :fingerprint, :hash, :created, :token, :action, :token_eol)";
94
95 1
        $statement = $this->connection->prepare($sql);
96
97 1
        $statement->bindValue(':type', Entity\EmailIdentity::TYPE_EMAIL);
98 1
        $statement->bindValue(':status', Entity\EmailIdentity::STATUS_NEW);
99 1
        $statement->bindValue(':email', $entity->getEmailAddress());
100 1
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
101 1
        $statement->bindValue(':hash', $entity->getHash());
102 1
        $statement->bindValue(':created', time());
103
104 1
        $this->bindToken($statement, $entity);
105
106 1
        $statement->execute();
107
108 1
        $entity->setId($this->connection->lastInsertId());
109 1
    }
110
111
112 1 View Code Duplication
    private function updateIdentity(Entity\EmailIdentity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114 1
        $sql = "UPDATE {$this->table}
115
                   SET hash = :hash,
116
                       status = :status,
117
                       expires_on = :expires,
118
                       token = :token,
119
                       token_action = :action,
120
                       token_expires_on = :token_eol
121
                 WHERE identity_id = :id";
122
123 1
        $statement = $this->connection->prepare($sql);
124
125 1
        $statement->bindValue(':id', $entity->getId());
126 1
        $statement->bindValue(':hash', $entity->getHash());
127 1
        $statement->bindValue(':status', $entity->getStatus());
128 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
129
130 1
        $this->bindToken($statement, $entity);
131
132 1
        $statement->execute();
133 1
    }
134
135
136 2
    private function bindToken(PDOStatement $statement, Entity\EmailIdentity $entity)
137
    {
138 2
        $statement->bindValue(':token', $entity->getToken());
139 2
        $statement->bindValue(':action', $entity->getTokenAction());
140 2
        $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
141 2
    }
142
}
143