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

Identity::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
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 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