Completed
Push — master ( 5d2ad9...5d2ad9 )
by Mārtiņš
06:44 queued 04:39
created

Identity::fetchById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 9
cts 9
cp 1
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 9
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
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