Completed
Branch master (53fdb9)
by Mārtiņš
04:03 queued 01:53
created

Identity   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 51.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 28
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 14 1
B fetch() 28 28 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\SqlMapper;
10
use Palladium\Entity\Authentication as Entity;
11
12
class Identity extends SqlMapper
13
{
14
15
    /**
16
     * @param Entity\Identity $entity
17
     */
18
    public function store(Entity\Identity $entity)
19
    {
20
        $table = $this->config['accounts']['identities'];
21
22
        $sql = "UPDATE $table
23
                   SET used_on = FROM_UNIXTIME(:used)
24
                 WHERE identity_id = :id";
25
26
        $statement = $this->connection->prepare($sql);
27
28
        $statement->bindValue(':id', $entity->getId());
29
        $statement->bindValue(':used', $entity->getLastUsed());
30
        $statement->execute();
31
    }
32
33
34
    /**
35
     * @param Entity\Identity $entity
36
     */
37 View Code Duplication
    public function fetch(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...
38
    {
39
        $table = $this->config['accounts']['identities'];
40
41
        $sql = "SELECT identity_id                      AS id,
42
                       user_id                          AS userId,
43
                       status                           AS status,
44
                       hash                             AS hash,
45
                       UNIX_TIMESTAMP(token_expires_on) AS tokenEndOfLife
46
                  FROM $table
47
                 WHERE token = :token
48
                   AND token_action = :action
49
                   AND token_expires_on > FROM_UNIXTIME(:expires)";
50
51
        $statement = $this->connection->prepare($sql);
52
53
        $statement->bindValue(':token', $entity->getToken());
54
        $statement->bindValue(':action', $entity->getTokenAction());
55
        $statement->bindValue(':expires', $entity->getTokenEndOfLife());
56
57
        $statement->execute();
58
59
        $data = $statement->fetch();
60
61
        if ($data) {
62
            $this->applyValues($entity, $data);
63
        }
64
    }
65
}
66