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

Locator::retrieveIdenityByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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\MapperFactory;
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(MapperFactory $mapperFactory, Psr\Log\LoggerInterface $logger)
24
    {
25
        $this->mapperFactory = $mapperFactory;
26
        $this->logger = $logger;
27
    }
28
29
30
    protected function retrieveIdenityByToken(Entity\Identity $identity, $token, $action = Entity\Identity::ACTION_ANY)
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 retrievePasswordIdenityByIdentifier($identifier)
44
    {
45
        $identity = new Entity\PasswordIdentity;
46
        $mapper = $this->mapperFactory->create(Mapper\PasswordIdentity::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