Completed
Push — master ( b75f88...ce2824 )
by Mārtiņš
02:02
created

EmailIdentity   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 173
Duplicated Lines 45.66 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 48.61%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 79
loc 173
ccs 35
cts 72
cp 0.4861
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 21 1
A fetch() 0 9 2
B fetchByEmailAddress() 28 29 2
B fetchById() 27 27 2
A store() 0 9 2
A createIdentity() 0 21 1
B updateIdentity() 24 24 1
A bindToken() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL logic for authentication attempts using username/password
7
 */
8
9
use Palladium\Component\DataMapper;
10
use Palladium\Entity as Entity;
11
use PDOStatement;
12
use PDO;
13
14
class EmailIdentity extends DataMapper
15
{
16
17
    /**
18
     * @param Entity\EmailIdentity $entity
19
     */
20
    public function exists(Entity\EmailIdentity $entity)
21
    {
22
        $sql = "SELECT 1
23
                  FROM {$this->table}
24
                 WHERE type = :type
25
                   AND fingerprint = :fingerprint
26
                   AND identifier = :email
27
                   AND (expires_on IS NULL OR expires_on > :now)";
28
29
        $statement = $this->connection->prepare($sql);
30
31
        $statement->bindValue(':type', Entity\EmailIdentity::TYPE_EMAIL);
32
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
33
        $statement->bindValue(':email', $entity->getEmailAddress());
34
        $statement->bindValue(':now', time());
35
36
        $statement->execute();
37
        $data = $statement->fetch(PDO::FETCH_ASSOC);
38
39
        return empty($data) === false;
40
    }
41
42
43
    /**
44
     * @param Entity\EmailIdentity $entity
45
     */
46
    public function fetch(Entity\EmailIdentity $entity)
47
    {
48
        if ($entity->getId()) {
49
            $this->fetchById($entity);
50
            return;
51
        }
52
53
        $this->fetchByEmailAddress($entity);
54
    }
55
56
57 View Code Duplication
    private function fetchByEmailAddress(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...
58
    {
59
        $sql = "SELECT identity_id      AS id,
60
                       account_id       AS accountId,
61
                       hash             AS hash,
62
                       status           AS status,
63
                       used_on          AS lastUsed,
64
                       token            AS token,
65
                       token_action     AS tokenAction,
66
                       token_expires_on AS tokenEndOfLife
67
                  FROM {$this->table}
68
                 WHERE type = :type
69
                   AND fingerprint = :fingerprint
70
                   AND identifier = :email";
71
72
        $statement = $this->connection->prepare($sql);
73
74
        $statement->bindValue(':type', $entity->getType());
75
        $statement->bindValue(':email', $entity->getEmailAddress());
76
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
77
78
        $statement->execute();
79
80
        $data = $statement->fetch(PDO::FETCH_ASSOC);
81
82
        if ($data) {
83
            $this->applyValues($entity, $data);
84
        }
85
    }
86
87
88 View Code Duplication
    private function fetchById(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 = "SELECT identity_id      AS id,
91
                       account_id       AS accountId,
92
                       hash             AS hash,
93
                       status           AS status,
94
                       used_on          AS lastUsed,
95
                       token            AS token,
96
                       token_action     AS tokenAction,
97
                       token_expires_on AS tokenEndOfLife
98
                  FROM {$this->table}
99
                 WHERE type = :type
100
                   AND identity_id = :id";
101
102
        $statement = $this->connection->prepare($sql);
103
104
        $statement->bindValue(':type', $entity->getType());
105
        $statement->bindValue(':id', $entity->getId());
106
107
        $statement->execute();
108
109
        $data = $statement->fetch(PDO::FETCH_ASSOC);
110
111
        if ($data) {
112
            $this->applyValues($entity, $data);
113
        }
114
    }
115
116
117
    /**
118
     * @param Entity\EmailIdentity $entity
119
     */
120 2
    public function store(Entity\EmailIdentity $entity)
121
    {
122 2
        if ($entity->getId() === null) {
123 1
            $this->createIdentity($entity);
124 1
            return;
125
        }
126
127 1
        $this->updateIdentity($entity);
128 1
    }
129
130
131 1
    private function createIdentity(Entity\EmailIdentity $entity)
132
    {
133 1
        $sql = "INSERT INTO {$this->table}
134
                       (type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on )
135
                VALUES (:type, :status, :email, :fingerprint, :hash, :created, :token, :action, :token_eol)";
136
137 1
        $statement = $this->connection->prepare($sql);
138
139 1
        $statement->bindValue(':type', Entity\EmailIdentity::TYPE_EMAIL);
140 1
        $statement->bindValue(':status', Entity\EmailIdentity::STATUS_NEW);
141 1
        $statement->bindValue(':email', $entity->getEmailAddress());
142 1
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
143 1
        $statement->bindValue(':hash', $entity->getHash());
144 1
        $statement->bindValue(':created', time());
145
146 1
        $this->bindToken($statement, $entity);
147
148 1
        $statement->execute();
149
150 1
        $entity->setId($this->connection->lastInsertId());
151 1
    }
152
153
154 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...
155
    {
156 1
        $sql = "UPDATE {$this->table}
157
                   SET hash = :hash,
158
                       status = :status,
159
                       used_on = :used,
160
                       expires_on = :expires,
161
                       token = :token,
162
                       token_action = :action,
163
                       token_expires_on = :token_eol
164
                 WHERE identity_id = :id";
165
166 1
        $statement = $this->connection->prepare($sql);
167
168 1
        $statement->bindValue(':id', $entity->getId());
169 1
        $statement->bindValue(':hash', $entity->getHash());
170 1
        $statement->bindValue(':status', $entity->getStatus());
171 1
        $statement->bindValue(':used', $entity->getLastUsed());
172 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
173
174 1
        $this->bindToken($statement, $entity);
175
176 1
        $statement->execute();
177 1
    }
178
179
180 2
    private function bindToken(PDOStatement $statement, Entity\EmailIdentity $entity)
181
    {
182 2
        $statement->bindValue(':token', $entity->getToken());
183 2
        $statement->bindValue(':action', $entity->getTokenAction());
184 2
        $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
185 2
    }
186
}
187