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

IdentityCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 53.16 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 42
loc 79
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 14 14 2
A fetch() 0 9 2
A fetchByAccount() 15 15 1
A fetchByParent() 13 13 1
A populateCollection() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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