Completed
Push — master ( 33aec4...b9a5c4 )
by Mārtiņš
02:06
created

EmailIdentity::updateIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 12
cp 0
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL logic for authentication attemps using username/password
7
 */
8
9
use Palladium\Component\SqlMapper;
10
use Palladium\Entity as Entity;
11
12
class EmailIdentity extends SqlMapper
13
{
14
15
    /**
16
     * @param Entity\EmailIdentity $entity
17
     */
18
    public function exists(Entity\EmailIdentity $entity)
19
    {
20
        $sql = "SELECT 1
21
                  FROM {$this->table}
22
                 WHERE type = :type
23
                   AND fingerprint = :fingerprint
24
                   AND identifier = :identifier
25
                   AND (expires_on IS NULL OR expires_on > :now)";
26
27
        $statement = $this->connection->prepare($sql);
28
29
        $statement->bindValue(':type', Entity\EmailIdentity::TYPE_PASSWORD);
30
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
31
        $statement->bindValue(':identifier', $entity->getIdentifier());
32
        $statement->bindValue(':now', time());
33
34
        $statement->execute();
35
        $data = $statement->fetch();
36
37
        return empty($data) === false;
38
    }
39
40
41
    /**
42
     * @param Entity\EmailIdentity $entity
43
     */
44 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...
45
    {
46
        $sql = "SELECT identity_id      AS id,
47
                       user_id          AS userId,
48
                       hash             AS hash,
49
                       status           AS status,
50
                       token            AS token,
51
                       token_action     AS tokenAction,
52
                       token_expires_on AS tokenEndOfLife
53
                  FROM {$this->table}
54
                 WHERE type = :type
55
                   AND fingerprint = :fingerprint
56
                   AND identifier = :identifier";
57
58
        $statement = $this->connection->prepare($sql);
59
60
        $statement->bindValue(':type', $entity->getType());
61
        $statement->bindValue(':identifier', $entity->getIdentifier());
62
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
63
64
        $statement->execute();
65
66
        $data = $statement->fetch();
67
68
        if ($data) {
69
            $this->applyValues($entity, $data);
70
        }
71
    }
72
73
74
    /**
75
     * @param Entity\EmailIdentity $entity
76
     */
77
    public function store(Entity\EmailIdentity $entity)
78
    {
79
        if ($entity->getId() === null) {
80
            $this->createIdentity($entity);
81
            return;
82
        }
83
84
        $this->updateIdentity($entity);
85
    }
86
87
88 View Code Duplication
    private function createIdentity(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...
89
    {
90
        $sql = "INSERT INTO {$this->table}
91
                       (type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on )
92
                VALUES (:type, :status, :identifier, :fingerprint, :hash, :created, :token, :action, :token_eol)";
93
94
        $statement = $this->connection->prepare($sql);
95
96
        $statement->bindValue(':type', Entity\EmailIdentity::TYPE_PASSWORD);
97
        $statement->bindValue(':status', Entity\EmailIdentity::STATUS_NEW);
98
        $statement->bindValue(':identifier', $entity->getIdentifier());
99
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
100
        $statement->bindValue(':hash', $entity->getHash());
101
        $statement->bindValue(':token', $entity->getToken());
102
        $statement->bindValue(':action', $entity->getTokenAction());
103
        $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
104
        $statement->bindValue(':created', time());
105
106
107
        $statement->execute();
108
109
        $entity->setId($this->connection->lastInsertId());
110
    }
111
112
113
    private function updateIdentity(Entity\EmailIdentity $entity)
114
    {
115
        $sql = "UPDATE {$this->table}
116
                   SET hash = :hash,
117
                       status = :status,
118
                       expires_on = :expires,
119
                       token = :token,
120
                       token_action = :action,
121
                       token_expires_on = :token_eol
122
                 WHERE identity_id = :id";
123
124
        $statement = $this->connection->prepare($sql);
125
126
        $statement->bindValue(':id', $entity->getId());
127
        $statement->bindValue(':hash', $entity->getHash());
128
        $statement->bindValue(':status', $entity->getStatus());
129
        $statement->bindValue(':expires', $entity->getExpiresOn());
130
        $statement->bindValue(':token', $entity->getToken());
131
        $statement->bindValue(':action', $entity->getTokenAction());
132
        $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
133
134
        $statement->execute();
135
    }
136
}
137