Completed
Push — master ( 994d94...7ba070 )
by Mārtiņš
02:13
created

NonceIdentity   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 47.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 41
loc 86
ccs 27
cts 39
cp 0.6923
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B fetch() 26 26 2
A store() 0 9 2
A createIdentity() 0 19 1
A updateIdentity() 15 15 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 attemps using username/password
7
 */
8
9
use Palladium\Component\DataMapper;
10
use Palladium\Entity as Entity;
11
12
class NonceIdentity extends DataMapper
13
{
14
15
    /**
16
     * @param Entity\NonceIdentity $entity
17
     */
18 View Code Duplication
    public function fetch(Entity\NonceIdentity $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...
19
    {
20
        $status = Entity\Identity::STATUS_ACTIVE;
21
22
        $sql = "SELECT identity_id      AS id,
23
                       account_id       AS accountId,
24
                       hash             AS hash,
25
                       status           AS status
26
                  FROM {$this->table}
27
                 WHERE type = :type
28
                   AND status = {$status}
29
                   AND identifier = :nonce";
30
31
        $statement = $this->connection->prepare($sql);
32
33
        $statement->bindValue(':nonce', $entity->getIdentifier());
34
        $statement->bindValue(':type', $entity->getType());
35
36
        $statement->execute();
37
38
        $data = $statement->fetch();
39
40
        if ($data) {
41
            $this->applyValues($entity, $data);
42
        }
43
    }
44
45
46
    /**
47
     * @param Entity\NonceIdentity $entity
48
     */
49 2
    public function store(Entity\NonceIdentity $entity)
50
    {
51 2
        if ($entity->getId() === null) {
52 1
            $this->createIdentity($entity);
53 1
            return;
54
        }
55
56 1
        $this->updateIdentity($entity);
57 1
    }
58
59
60 1
    private function createIdentity(Entity\NonceIdentity $entity)
61
    {
62 1
        $sql = "INSERT INTO {$this->table}
63
                       (account_id, type, status, identifier, fingerprint, hash, created_on, expires_on)
64
                VALUES (:account, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)";
65
66 1
        $statement = $this->connection->prepare($sql);
67
68 1
        $statement->bindValue(':account', $entity->getAccountId());
69 1
        $statement->bindValue(':type', $entity->getType());
70 1
        $statement->bindValue(':status', $entity->getStatus());
71 1
        $statement->bindValue(':identifier', $entity->getIdentifier());
72 1
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
73 1
        $statement->bindValue(':hash', $entity->getHash());
74 1
        $statement->bindValue(':created', time());
75 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
76
77 1
        $statement->execute();
78 1
    }
79
80
81 1 View Code Duplication
    private function updateIdentity(Entity\NonceIdentity $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...
82
    {
83 1
        $sql = "UPDATE {$this->table}
84
                   SET status = :status,
85
                       used_on = :used
86
                 WHERE identity_id = :id";
87
88 1
        $statement = $this->connection->prepare($sql);
89
90 1
        $statement->bindValue(':id', $entity->getId());
91 1
        $statement->bindValue(':status', $entity->getStatus());
92 1
        $statement->bindValue(':used', time());
93
94 1
        $statement->execute();
95 1
    }
96
97
}
98