Completed
Push — master ( 734e62...750475 )
by Mārtiņš
03:48
created

NonceIdentity::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL logic for authentication attemps using username/password
7
 */
8
9
use Palladium\Component\DataMapper;
10
use Palladium\Entity as Entity;
11
use PDO;
12
13
class NonceIdentity extends DataMapper
14
{
15
16
    /**
17
     * @param Entity\NonceIdentity $entity
18
     */
19 View Code Duplication
    public function fetch(Entity\NonceIdentity $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...
20
    {
21
        $sql = "SELECT identity_id      AS id,
22
                       account_id       AS accountId,
23
                       hash             AS hash,
24
                       status           AS status,
25
                       expires_on       AS expiresOn
26
                  FROM {$this->table}
27
                 WHERE type = :type
28
                   AND status = :status
29
                   AND fingerprint = :fingerprint
30
                   AND identifier = :identifier";
31
32
        $statement = $this->connection->prepare($sql);
33
34
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
35
        $statement->bindValue(':identifier', $entity->getIdentifier());
36
        $statement->bindValue(':status', Entity\Identity::STATUS_ACTIVE);
37
        $statement->bindValue(':type', $entity->getType());
38
39
        $statement->execute();
40
41
        $data = $statement->fetch(PDO::FETCH_ASSOC);
42
43
        if ($data) {
44
            $this->applyValues($entity, $data);
45
        }
46
    }
47
48
49
    /**
50
     * @param Entity\NonceIdentity $entity
51
     */
52 2
    public function store(Entity\NonceIdentity $entity)
53
    {
54 2
        if ($entity->getId() === null) {
55 1
            $this->createIdentity($entity);
56 1
            return;
57
        }
58
59 1
        $this->updateIdentity($entity);
60 1
    }
61
62
63 1
    private function createIdentity(Entity\NonceIdentity $entity)
64
    {
65 1
        $sql = "INSERT INTO {$this->table}
66
                       (account_id, type, status, identifier, fingerprint, hash, created_on, expires_on)
67
                VALUES (:account, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)";
68
69 1
        $statement = $this->connection->prepare($sql);
70
71 1
        $statement->bindValue(':account', $entity->getAccountId());
72 1
        $statement->bindValue(':type', $entity->getType());
73 1
        $statement->bindValue(':status', $entity->getStatus());
74 1
        $statement->bindValue(':identifier', $entity->getIdentifier());
75 1
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
76 1
        $statement->bindValue(':hash', $entity->getHash());
77 1
        $statement->bindValue(':created', time());
78 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
79
80 1
        $statement->execute();
81 1
    }
82
83
84 1
    private function updateIdentity(Entity\NonceIdentity $entity)
85
    {
86 1
        $sql = "UPDATE {$this->table}
87
                   SET status = :status,
88
                       used_on = :used
89
                 WHERE identity_id = :id";
90
91 1
        $statement = $this->connection->prepare($sql);
92
93 1
        $statement->bindValue(':id', $entity->getId());
94 1
        $statement->bindValue(':status', $entity->getStatus());
95 1
        $statement->bindValue(':used', time());
96
97 1
        $statement->execute();
98 1
    }
99
100
}
101