Completed
Push — master ( 25598f...82bcfc )
by Mārtiņš
02:18
created

OneTimeIdentity   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 49.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 44
loc 89
ccs 0
cts 58
cp 0
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() 18 18 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
use PDOStatement;
12
13
class OneTimeIdentity extends DataMapper
14
{
15
16
    /**
17
     * @param Entity\OneTimeIdentity $entity
18
     */
19 View Code Duplication
    public function fetch(Entity\OneTimeIdentity $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...
20
    {
21
        $status = Entity\Identity::STATUS_ACTIVE;
22
23
        $sql = "SELECT identity_id      AS id,
24
                       account_id       AS accountId,
25
                       hash             AS hash,
26
                       status           AS status
27
                  FROM {$this->table}
28
                 WHERE type = :type
29
                   AND status = {$status}
30
                   AND identifier = :nonce";
31
32
        $statement = $this->connection->prepare($sql);
33
34
        $statement->bindValue(':nonce', $entity->getEmailAddress());
0 ignored issues
show
Bug introduced by
The method getEmailAddress() does not seem to exist on object<Palladium\Entity\OneTimeIdentity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
        $statement->bindValue(':type', $entity->getType());
36
37
        $statement->execute();
38
39
        $data = $statement->fetch();
40
41
        if ($data) {
42
            $this->applyValues($entity, $data);
43
        }
44
    }
45
46
47
    /**
48
     * @param Entity\OneTimeIdentity $entity
49
     */
50
    public function store(Entity\OneTimeIdentity $entity)
51
    {
52
        if ($entity->getId() === null) {
53
            $this->createIdentity($entity);
54
            return;
55
        }
56
57
        $this->updateIdentity($entity);
58
    }
59
60
61
    private function createIdentity(Entity\OneTimeIdentity $entity)
62
    {
63
        $sql = "INSERT INTO {$this->table}
64
                       (account_id, type, status, identifier, fingerprint, hash, created_on, expires_on)
65
                VALUES (:account, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)";
66
67
        $statement = $this->connection->prepare($sql);
68
69
        $statement->bindValue(':account', $entity->getAccountId());
70
        $statement->bindValue(':type', $entity->getType());
71
        $statement->bindValue(':status', $entity->getStatus());
72
        $statement->bindValue(':identifier', $entity->getNonce());
73
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
0 ignored issues
show
Bug introduced by
The method getFingerprint() does not seem to exist on object<Palladium\Entity\OneTimeIdentity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
        $statement->bindValue(':hash', $entity->getHash());
75
        $statement->bindValue(':created', time());
76
        $statement->bindValue(':expires', $entity->getExpiresOn());
77
78
        $statement->execute();
79
    }
80
81
82 View Code Duplication
    private function updateIdentity(Entity\OneTimeIdentity $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...
83
    {
84
        $status = Entity\Identity::STATUS_DISCARDED;
85
86
        $sql = "UPDATE {$this->table}
87
                   SET status = :status,
88
                       used_on = :used,
89
                 WHERE identity_id = :id
90
                   AND status = {$status}";
91
92
        $statement = $this->connection->prepare($sql);
93
94
        $statement->bindValue(':id', $entity->getId());
95
        $statement->bindValue(':status', $entity->getStatus());
96
        $statement->bindValue(':used', time());
97
98
        $statement->execute();
99
    }
100
101
}
102