AbstractAdapter.php 2.69 KB
Newer Older
1
2
3
4
<?php

namespace UnicaenAuth\Authentication\Adapter;

5
use UnicaenAuth\Authentication\SessionIdentity;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
6
7
8
9
10
use Laminas\Authentication\Storage\StorageInterface;
use Laminas\EventManager\EventInterface;
use Laminas\EventManager\EventManagerInterface;
use Laminas\EventManager\ListenerAggregateInterface;
use Laminas\EventManager\ListenerAggregateTrait;
11
12
use ZfcUser\Authentication\Adapter\ChainableAdapter;

13
abstract class AbstractAdapter implements ChainableAdapter, ListenerAggregateInterface
14
{
15
16
    use ListenerAggregateTrait;

17
18
19
20
21
22
    /**
     * @var string
     */
    protected $type;

    /**
23
     * @var StorageInterface
24
25
26
27
28
29
30
31
32
33
34
35
36
     */
    protected $storage;

    /**
     * @param string $type
     * @return self
     */
    public function setType(string $type): self
    {
        $this->type = $type;
        return $this;
    }

37
38
39
40
41
42
43
44
    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

45
46
47
    /**
     * Returns the persistent storage handler
     *
48
     * @return StorageInterface
49
     */
50
    public function getStorage(): StorageInterface
51
52
53
54
55
56
57
    {
        return $this->storage;
    }

    /**
     * Sets the persistent storage handler
     *
58
59
     * @param  StorageInterface $storage
     * @return self Provides a fluent interface
60
     */
61
    public function setStorage(StorageInterface $storage): self
62
63
64
65
66
67
68
69
70
71
    {
        $this->storage = $storage;
        return $this;
    }

    /**
     * Check if this adapter is satisfied or not
     *
     * @return bool
     */
72
    public function isSatisfied(): bool
73
74
75
76
77
78
79
80
81
    {
        $storage = $this->getStorage()->read();
        return (isset($storage['is_satisfied']) && true === $storage['is_satisfied']);
    }

    /**
     * Set if this adapter is satisfied or not
     *
     * @param bool $bool
82
     * @return self
83
     */
84
    public function setSatisfied($bool = true): self
85
86
87
88
89
90
    {
        $storage = $this->getStorage()->read() ?: array();
        $storage['is_satisfied'] = $bool;
        $this->getStorage()->write($storage);
        return $this;
    }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

    /**
     * @param string $username
     * @return SessionIdentity
     */
    protected function createSessionIdentity(string $username): SessionIdentity
    {
        return SessionIdentity::newInstance($username, $this->type);
    }

    /**
     * Called when user id logged out
     *
     * @param EventInterface $e
     */
    public function logout(EventInterface $e)
    {
        $this->getStorage()->clear();
    }

    /**
     * @inheritDoc
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $events->attach('authenticate', [$this, 'authenticate'], $priority);
        $events->attach('logout', [$this, 'logout'], $priority);
    }
119
}