CookieIdentity::updateCookie()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.9332
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * All of the SQL code related to the storage and retrieval of cookie-based identities.
7
 * Probably need some cleanup in `store()` method
8
 */
9
10
use Palladium\Component\DataMapper;
11
use Palladium\Entity as Entity;
12
use PDOStatement;
13
use PDO;
14
15
class CookieIdentity extends DataMapper
16
{
17
18
    /**
19
     * @param Entity\CookieIdentity $entity
20
     */
21 7
    public function fetch(Entity\CookieIdentity $entity)
22
    {
23
        $sql = "SELECT identity_id  AS id,
24
                       parent_id    AS parentId,
25
                       hash         AS hash,
26
                       expires_on   AS expiresOn
27 7
                  FROM {$this->table} AS Identities
28
                 WHERE type = :type
29
                   AND account_id = :account
30
                   AND identifier = :identifier
31
                   AND fingerprint = :fingerprint
32
                   AND status = :status";
33
34 7
        $statement = $this->connection->prepare($sql);
35 7
        $this->bindCommonParameters($statement, $entity);
0 ignored issues
show
Bug introduced by
It seems like $statement can also be of type boolean; however, parameter $statement of Palladium\Mapper\CookieI...:bindCommonParameters() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $this->bindCommonParameters(/** @scrutinizer ignore-type */ $statement, $entity);
Loading history...
36
37 7
        $statement->execute();
38
39 7
        $data = $statement->fetch(PDO::FETCH_ASSOC);
40
41 7
        if ($data) {
42 7
            $this->applyValues($entity, $data);
43
        }
44 7
    }
45
46
47 13
    private function bindCommonParameters(PDOStatement $statement, Entity\CookieIdentity $entity)
48
    {
49 13
        $statement->bindValue(':type', $entity->getType());
50 13
        $statement->bindValue(':status', $entity->getStatus());
51 13
        $statement->bindValue(':account', $entity->getAccountId());
52 13
        $statement->bindValue(':identifier', $entity->getSeries());
53 13
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
54 13
    }
55
56
57
58 12
    public function store(Entity\CookieIdentity $entity)
59
    {
60 12
        if ($entity->getId() === null) {
61 10
            $this->createCookie($entity);
62 10
            return;
63
        }
64
65 3
        $this->updateCookie($entity);
66 3
    }
67
68
69 10
    private function createCookie(Entity\CookieIdentity $entity)
70
    {
71 10
        $sql = "INSERT INTO {$this->table}
72
                       (account_id, parent_id, type, status, identifier, fingerprint, hash, created_on, expires_on)
73
                VALUES (:account, :parent, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)";
74
75 10
        $statement = $this->connection->prepare($sql);
76
77 10
        $statement->bindValue(':parent', $entity->getParentId());
78 10
        $statement->bindValue(':hash', $entity->getHash());
79 10
        $statement->bindValue(':expires', $entity->getExpiresOn());
80 10
        $statement->bindValue(':created', time());
81
82 10
        $this->bindCommonParameters($statement, $entity);
0 ignored issues
show
Bug introduced by
It seems like $statement can also be of type boolean; however, parameter $statement of Palladium\Mapper\CookieI...:bindCommonParameters() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $this->bindCommonParameters(/** @scrutinizer ignore-type */ $statement, $entity);
Loading history...
83
84 10
        $statement->execute();
85
86 10
        $entity->setId($this->connection->lastInsertId());
0 ignored issues
show
Bug introduced by
$this->connection->lastInsertId() of type string is incompatible with the type integer expected by parameter $identityId of Palladium\Entity\Identity::setId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $entity->setId(/** @scrutinizer ignore-type */ $this->connection->lastInsertId());
Loading history...
87 10
    }
88
89
90 3
    private function updateCookie(Entity\CookieIdentity $entity)
91
    {
92 3
        $active = Entity\Identity::STATUS_ACTIVE;
93
94 3
        $sql = "UPDATE {$this->table}
95
                   SET status = :status,
96
                       hash = :hash,
97
                       used_on = :used,
98
                       expires_on = :expires
99
                 WHERE identity_id = :id
100 3
                   AND status = {$active}";
101
102 3
        $statement = $this->connection->prepare($sql);
103
104 3
        $statement->bindValue(':id', $entity->getId());
105 3
        $statement->bindValue(':status', $entity->getStatus());
106 3
        $statement->bindValue(':hash', $entity->getHash());
107 3
        $statement->bindValue(':expires', $entity->getExpiresOn());
108 3
        $statement->bindValue(':used', $entity->getLastUsed());
109
110 3
        $statement->execute();
111 3
    }
112
}
113