Completed
Push — master ( 1a2fe9...5d2ad9 )
by Mārtiņš
05:09
created

Identity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 28.7 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 31
loc 108
ccs 35
cts 42
cp 0.8333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 10 2
A fetchById() 0 23 2
A store() 0 15 1
A remove() 0 8 1
B fetchByToken() 31 31 3

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 code for locating identity data by token and updating last usage.
7
 */
8
9
use Palladium\Component\DataMapper;
10
use Palladium\Entity as Entity;
11
use PDO;
12
13
class Identity extends DataMapper
14
{
15
16
    /**
17
     * @param Entity\Identity $entity
18
     */
19 1
    public function store(Entity\Identity $entity)
20
    {
21 1
        $sql = "UPDATE {$this->table}
22
                   SET used_on = :used,
23
                       status = :status
24
                 WHERE identity_id = :id";
25
26 1
        $statement = $this->connection->prepare($sql);
27
28 1
        $statement->bindValue(':id', $entity->getId());
29 1
        $statement->bindValue(':used', $entity->getLastUsed());
30 1
        $statement->bindValue(':status', $entity->getStatus());
31
32 1
        $statement->execute();
33 1
    }
34
35
36
    /**
37
     * @param Entity\Identity $entity
38
     */
39
    public function remove(Entity\Identity $entity)
40
    {
41
        $sql = "DELETE FROM {$this->table} WHERE identity_id = :id";
42
        $statement = $this->connection->prepare($sql);
43
44
        $statement->bindValue(':id', $entity->getId());
45
        $statement->execute();
46
    }
47
48
49
    /**
50
     * @param Entity\Identity $entity
51
     */
52 2
    public function fetch(Entity\Identity $entity)
53
54
    {
55 2
        if ($entity->getId()) {
56 1
            $this->fetchById($entity);
57 1
            return;
58
        }
59
60 1
        $this->fetchByToken($entity);
61 1
    }
62
63
64 1
    private function fetchById(Entity\Identity $entity)
65
    {
66
        $sql = "SELECT identity_id      AS id,
67
                       parent_id        AS parentId,
68
                       account_id       AS accountId,
69
                       status           AS status,
70
                       hash             AS hash,
71
                       token_expires_on AS tokenEndOfLife
72 1
                  FROM {$this->table}
73
                 WHERE identity_id = :id";
74
75 1
        $statement = $this->connection->prepare($sql);
76
77 1
        $statement->bindValue(':id', $entity->getId());
78
79 1
        $statement->execute();
80
81 1
        $data = $statement->fetch(PDO::FETCH_ASSOC);
82
83 1
        if ($data) {
84 1
            $this->applyValues($entity, $data);
85
        }
86 1
    }
87
88
89 1 View Code Duplication
    private function fetchByToken(Entity\Identity $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...
90
    {
91
        $sql = "SELECT identity_id      AS id,
92
                       parent_id        AS parentId,
93
                       account_id       AS accountId,
94
                       status           AS status,
95
                       hash             AS hash,
96
                       token_expires_on AS tokenEndOfLife,
97
                       token_payload    AS tokenPayload
98 1
                  FROM {$this->table}
99
                 WHERE token = :token
100
                   AND token_action = :action
101
                   AND token_expires_on > :expires";
102
103 1
        $statement = $this->connection->prepare($sql);
104
105 1
        $statement->bindValue(':token', $entity->getToken());
106 1
        $statement->bindValue(':action', $entity->getTokenAction());
107 1
        $statement->bindValue(':expires', $entity->getTokenEndOfLife());
108
109 1
        $statement->execute();
110
111 1
        $data = $statement->fetch(PDO::FETCH_ASSOC);
112
113 1
        if ($data) {
114 1
            if ($data['tokenPayload'] !== null) {
115
                $data['tokenPayload'] = json_decode($data['tokenPayload'], true);
116
            }
117 1
            $this->applyValues($entity, $data);
118
        }
119 1
    }
120
}
121