Completed
Push — master ( afc88d...d0e6e6 )
by Mārtiņš
02:23
created

Locator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 15.71 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 11
loc 70
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A discardAllUserCookies() 0 15 2
A retrieveIdenitiesByUserId() 0 12 1
A retrieveIdenityByToken() 11 11 1
A retrieveEmailIdenityByIdentifier() 0 11 1

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\Service;
4
5
/**
6
 * Purely written dumping grownd for shared methods of Serive\Authentication namespace
7
 */
8
9
use Palladium\Component\CanCreateMapper;
10
use Palladium\Mapper\Authentication as Mapper;
11
use Palladium\Entity\Authentication as Entity;
12
13
use Monolog\Logger;
14
15
16
abstract class Locator
17
{
18
19
    protected $mapperFactory;
20
    protected $logger;
21
22
23
    public function __construct(CanCreateMapper $mapperFactory, Psr\Log\LoggerInterface $logger)
24
    {
25
        $this->mapperFactory = $mapperFactory;
26
        $this->logger = $logger;
27
    }
28
29
30 View Code Duplication
    protected function retrieveIdenityByToken(Entity\Identity $identity, $token, $action = Entity\Identity::ACTION_ANY)
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...
31
    {
32
        $identity->setToken($token);
33
        $identity->setTokenAction($action);
34
        $identity->setTokenEndOfLife(time());
35
36
        $mapper = $this->mapperFactory->create(Mapper\Identity::class);
37
        $mapper->fetch($identity);
38
39
        return $identity;
40
    }
41
42
43
    protected function retrieveEmailIdenityByIdentifier($identifier)
44
    {
45
        $identity = new Entity\EmailIdentity;
46
        $mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class);
47
48
        $identity->setIdentifier($identifier);
49
50
        $mapper->fetch($identity);
51
52
        return $identity;
53
    }
54
55
56
    protected function discardAllUserCookies($userId)
57
    {
58
        /**
59
         * @NOTE: this operation might require transaction
60
         * or a change in how store() is implemnted in IdentityCollection mapper
61
         */
62
        $list = $this->retrieveIdenitiesByUserId($userId, Entity\Identity::TYPE_COOKIE);
63
64
        foreach ($list as $identity) {
65
            $identity->setStatus(Entity\Identity::STATUS_DISCARDED);
66
        }
67
68
        $mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class);
69
        $mapper->store($list);
70
    }
71
72
73
    protected function retrieveIdenitiesByUserId($userId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE)
74
    {
75
        $collection = new Entity\IdentityCollection;
76
        $collection->forUserId($userId);
77
        $collection->forType($type);
78
        $collection->forStatus($status);
79
80
        $mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class);
81
        $mapper->fetch($collection);
82
83
        return $collection;
84
    }
85
}
86