IdentityCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 26
dl 0
loc 76
c 0
b 0
f 0
ccs 32
cts 32
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A populateCollection() 0 8 2
A store() 0 11 2
A fetch() 0 8 2
A fetchByParent() 0 12 1
A fetchByAccount() 0 14 1
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL code responsible for locating all of the identities, that have been associated
7
 * to a given account and discarding them in bulk.
8
 * Used mostly in case of password reset or, if cookie has been compromised.
9
 */
10
11
12
use Palladium\Component\DataMapper;
13
use Palladium\Entity as Entity;
14
use PDOStatement;
15
use PDO;
16
17
class IdentityCollection extends DataMapper
18
{
19
20
    /**
21
     * @param Entity\IdentityCollection $collection
22
     */
23 1
    public function store(Entity\IdentityCollection $collection)
24
    {
25 1
        $sql = "UPDATE {$this->table}
26
                   SET status = :status
27
                 WHERE identity_id = :id";
28 1
        $statement = $this->connection->prepare($sql);
29
30 1
        foreach ($collection as $entity) {
31 1
            $statement->execute([
32 1
                ':id' => $entity->getId(),
33 1
                ':status' => $entity->getStatus(),
34
            ]);
35
        }
36 1
    }
37
38
39
    /**
40
     * @param Entity\IdentityCollection $collection
41
     */
42 2
    public function fetch(Entity\IdentityCollection $collection)
43
    {
44 2
        if ($collection->getParentId() !== null) {
45 1
            $this->fetchByParent($collection);
46 1
            return;
47
        }
48
49 1
        $this->fetchByAccount($collection);
50 1
    }
51
52
53 1
    private function fetchByAccount(Entity\IdentityCollection $collection)
54
    {
55
        $sql = "SELECT identity_id  AS id
56 1
                  FROM {$this->table}
57
                 WHERE status = :status
58
                   AND account_id = :account
59
                   AND type = :type";
60
61 1
        $statement = $this->connection->prepare($sql);
62
63 1
        $statement->bindValue(':account', $collection->getAccountId());
64 1
        $statement->bindValue(':type', $collection->getType());
65
66 1
        $this->populateCollection($collection, $statement);
0 ignored issues
show
Bug introduced by
It seems like $statement can also be of type boolean; however, parameter $statement of Palladium\Mapper\Identit...n::populateCollection() 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

66
        $this->populateCollection($collection, /** @scrutinizer ignore-type */ $statement);
Loading history...
67 1
    }
68
69
70 1
    private function fetchByParent(Entity\IdentityCollection $collection)
71
    {
72
        $sql = "SELECT identity_id  AS id
73 1
                  FROM {$this->table}
74
                 WHERE status = :status
75
                   AND parent_id = :parent";
76
77 1
        $statement = $this->connection->prepare($sql);
78
79 1
        $statement->bindValue(':parent', $collection->getParentId());
80
81 1
        $this->populateCollection($collection, $statement);
0 ignored issues
show
Bug introduced by
It seems like $statement can also be of type boolean; however, parameter $statement of Palladium\Mapper\Identit...n::populateCollection() 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

81
        $this->populateCollection($collection, /** @scrutinizer ignore-type */ $statement);
Loading history...
82 1
    }
83
84
85 2
    private function populateCollection(Entity\IdentityCollection $collection, PDOStatement $statement)
86
    {
87 2
        $statement->bindValue(':status', $collection->getStatus());
88
89 2
        $statement->execute();
90
91 2
        foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $parameters) {
92 2
            $collection->addBlueprint($parameters);
93
        }
94 2
    }
95
}
96