Completed
Push — master ( 9ce72a...1ceaf8 )
by Mārtiņš
02:28
created

OneTimeIdentity::fetch()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 26
loc 26
ccs 0
cts 12
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 6
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 PDOStatement;
12
13
class OneTimeIdentity extends DataMapper
14
{
15
16
    /**
17
     * @param Entity\OneTimeIdentity $entity
18
     */
19 View Code Duplication
    public function fetch(Entity\OneTimeIdentity $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
        $status = Entity\Identity::STATUS_ACTIVE;
22
23
        $sql = "SELECT identity_id      AS id,
24
                       account_id       AS accountId,
25
                       hash             AS hash,
26
                       status           AS status
27
                  FROM {$this->table}
28
                 WHERE type = :type
29
                   AND status = {$status}
30
                   AND identifier = :nonce";
31
32
        $statement = $this->connection->prepare($sql);
33
34
        $statement->bindValue(':nonce', $entity->getNonce());
35
        $statement->bindValue(':type', $entity->getType());
36
37
        $statement->execute();
38
39
        $data = $statement->fetch();
40
41
        if ($data) {
42
            $this->applyValues($entity, $data);
43
        }
44
    }
45
46
47
    /**
48
     * @param Entity\OneTimeIdentity $entity
49
     */
50 2
    public function store(Entity\OneTimeIdentity $entity)
51
    {
52 2
        if ($entity->getId() === null) {
53 1
            $this->createIdentity($entity);
54 1
            return;
55
        }
56
57 1
        $this->updateIdentity($entity);
58 1
    }
59
60
61 1
    private function createIdentity(Entity\OneTimeIdentity $entity)
62
    {
63 1
        $sql = "INSERT INTO {$this->table}
64
                       (account_id, type, status, identifier, fingerprint, hash, created_on, expires_on)
65
                VALUES (:account, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)";
66
67 1
        $statement = $this->connection->prepare($sql);
68
69 1
        $statement->bindValue(':account', $entity->getAccountId());
70 1
        $statement->bindValue(':type', $entity->getType());
71 1
        $statement->bindValue(':status', $entity->getStatus());
72 1
        $statement->bindValue(':identifier', $entity->getNonce());
73 1
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
74 1
        $statement->bindValue(':hash', $entity->getHash());
75 1
        $statement->bindValue(':created', time());
76 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
77
78 1
        $statement->execute();
79 1
    }
80
81
82 1
    private function updateIdentity(Entity\OneTimeIdentity $entity)
83
    {
84 1
        $status = Entity\Identity::STATUS_DISCARDED;
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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