Completed
Push — master ( cd27e7...dbd271 )
by Mārtiņš
04:09
created

IdentityCollection::populateCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL code repsonsible 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\SqlMapper;
13
use Palladium\Entity as Entity;
14
use PDOStatement;
15
16
class IdentityCollection extends SqlMapper
17
{
18
19
    /**
20
     * @param Entity\IdentityCollection $collection
21
     */
22 1 View Code Duplication
    public function store(Entity\IdentityCollection $collection)
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...
23
    {
24 1
        $sql = "UPDATE {$this->table}
25
                   SET status = :status
26
                 WHERE identity_id = :id";
27 1
        $statement = $this->connection->prepare($sql);
28
29 1
        foreach ($collection as $entity) {
30 1
            $statement->execute([
31 1
                ':id' => $entity->getId(),
32 1
                ':status' => $entity->getStatus(),
33
            ]);
34
        }
35 1
    }
36
37
38
    /**
39
     * @param Entity\IdentityCollection $collection
40
     */
41 2
    public function fetch(Entity\IdentityCollection $collection)
42
    {
43 2
        if ($collection->getParentId() !== null) {
44 1
            $this->fetchByParent($collection);
45 1
            return;
46
        }
47
48 1
        $this->fetchByAccount($collection);
49 1
    }
50
51
52 1 View Code Duplication
    private function fetchByAccount(Entity\IdentityCollection $collection)
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...
53
    {
54
        $sql = "SELECT identity_id  AS id
55 1
                  FROM {$this->table}
56
                 WHERE status = :status
57
                   AND account_id = :account
58
                   AND type = :type";
59
60 1
        $statement = $this->connection->prepare($sql);
61
62 1
        $statement->bindValue(':account', $collection->getAccountId());
63 1
        $statement->bindValue(':type', $collection->getType());
64
65 1
        $this->populateCollection($collection, $statement);
66 1
    }
67
68
69 1 View Code Duplication
    private function fetchByParent(Entity\IdentityCollection $collection)
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...
70
    {
71
        $sql = "SELECT identity_id  AS id
72 1
                  FROM {$this->table}
73
                 WHERE status = :status
74
                   AND parent_id = :parent";
75
76 1
        $statement = $this->connection->prepare($sql);
77
78 1
        $statement->bindValue(':parent', $collection->getParentId());
79
80 1
        $this->populateCollection($collection, $statement);
81 1
    }
82
83
84 2
    private function populateCollection(Entity\IdentityCollection $collection, PDOStatement $statement)
85
    {
86 2
        $statement->bindValue(':status', $collection->getStatus());
87
88 2
        $statement->execute();
89
90 2
        foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $parameters) {
91 2
            $collection->addBlueprint($parameters);
92
        }
93 2
    }
94
}
95