Completed
Push — master ( 8b4cd1...6cce77 )
by Mārtiņš
04:56
created

Identity::fetchByToken()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 11

Duplication

Lines 26
Ratio 96.3 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 26
loc 27
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2
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
    public function store(Entity\Identity $entity)
19
    {
20 1
        $sql = "UPDATE {$this->table}
21
                   SET used_on = :used
22 1
                 WHERE identity_id = :id";
23
24 1
        $statement = $this->connection->prepare($sql);
25
26 1
        $statement->bindValue(':id', $entity->getId());
27 1
        $statement->bindValue(':used', $entity->getLastUsed());
28 1
        $statement->execute();
29 1
    }
30
31
32
    /**
33
     * @param Entity\Identity $entity
34
     */
35 2
    public function fetch(Entity\Identity $entity)
36
    {
37 2
        if ($entity->getId()) {
38 1
            $this->fetchById($entity);
39 1
            return;
40
        }
41
42 1
        $this->fetchByToken($entity);
43 1
    }
44
45
46 1
    private function fetchById(Entity\Identity $entity)
47
    {
48
        $sql = "SELECT identity_id      AS id,
49
                       parent_id        AS parentId,
50
                       account_id       AS accountId,
51
                       status           AS status,
52
                       hash             AS hash,
53
                       token_expires_on AS tokenEndOfLife
54 1
                  FROM {$this->table}
55 1
                 WHERE identity_id = :id";
56
57 1
        $statement = $this->connection->prepare($sql);
58
59 1
        $statement->bindValue(':id', $entity->getId());
60
61 1
        $statement->execute();
62
63 1
        $data = $statement->fetch();
64
65 1
        if ($data) {
66 1
            $this->applyValues($entity, $data);
67
        }
68 1
    }
69
70
71 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...
72
    {
73
        $sql = "SELECT identity_id      AS id,
74
                       parent_id        AS parentId,
75
                       account_id       AS accountId,
76
                       status           AS status,
77
                       hash             AS hash,
78
                       token_expires_on AS tokenEndOfLife
79 1
                  FROM {$this->table}
80
                 WHERE token = :token
81
                   AND token_action = :action
82 1
                   AND token_expires_on > :expires";
83
84 1
        $statement = $this->connection->prepare($sql);
85
86 1
        $statement->bindValue(':token', $entity->getToken());
87 1
        $statement->bindValue(':action', $entity->getTokenAction());
88 1
        $statement->bindValue(':expires', $entity->getTokenEndOfLife());
89
90 1
        $statement->execute();
91
92 1
        $data = $statement->fetch();
93
94 1
        if ($data) {
95 1
            $this->applyValues($entity, $data);
96
        }
97 1
    }
98
}
99