Completed
Push — master ( cd56a0...429bb1 )
by Mārtiņš
02:05
created

Identity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 47.12 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 2
dl 49
loc 104
ccs 34
cts 40
cp 0.85
rs 10

5 Methods

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

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