Completed
Branch master (53fdb9)
by Mārtiņš
04:03 queued 01:53
created

IdentityCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 38.6 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 22
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 6 2
A updateStatus() 0 15 2
A fetch() 22 22 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 user 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\Authentication as Entity;
14
15
class IdentityCollection extends SqlMapper
16
{
17
18
    /**
19
     * @param Entity\IdentityCollection $collection
20
     */
21
    public function store(Entity\IdentityCollection $collection)
22
    {
23
        if ($collection->getUserId() !== null) {
24
            $this->updateStatus($collection);
25
        }
26
    }
27
28
29
    private function updateStatus(Entity\IdentityCollection $collection)
30
    {
31
        $table = $this->config['accounts']['identities'];
32
33
        $sql = "UPDATE {$table}
34
                   SET status = :status
35
                 WHERE identity_id = :id";
36
        $statement = $this->connection->prepare($sql);
37
38
        foreach ($collection as $entity) {
39
            $statement->bindValue(':id', $entity->getId());
40
            $statement->bindValue(':status', $entity->getStatus());
41
            $statement->execute();
42
        }
43
    }
44
45
46
    /**
47
     * @param Entity\IdentityCollection $collection
48
     */
49 View Code Duplication
    public function fetch(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...
50
    {
51
        $table = $this->config['accounts']['identities'];
52
53
        $sql = "SELECT identity_id  AS id
54
                  FROM {$table}
55
                 WHERE status = :status
56
                   AND user_id = :user
57
                   AND type = :type";
58
59
       $statement = $this->connection->prepare($sql);
60
61
       $statement->bindValue(':user', $collection->getUserId());
62
       $statement->bindValue(':status', $collection->getStatus());
63
       $statement->bindValue(':type', $collection->getType());
64
65
       $statement->execute();
66
67
       foreach ($statement as $parameters) {
68
           $collection->addBlueprint($parameters);
69
       }
70
    }
71
}
72