Completed
Push — master ( ffb215...f806ab )
by Anton
11s
created

Auth   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.75%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 55
ccs 15
cts 16
cp 0.9375
rs 10
c 2
b 0
f 1
wmc 5
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setIdentity() 0 9 1
A clearIdentity() 0 6 1
A getIdentity() 0 12 3
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Auth;
13
14
use Bluz\Common\Options;
15
use Bluz\Proxy\Request;
16
use Bluz\Proxy\Session;
17
18
/**
19
 * Auth class
20
 *
21
 * @package  Bluz\Auth
22
 * @author   Anton Shevchuk
23
 * @link     https://github.com/bluzphp/framework/wiki/Auth
24
 */
25
class Auth
26
{
27
    use Options;
28
29
    /**
30
     * @var EntityInterface Instance of EntityInterface
31
     */
32
    protected $identity;
33
34
    /**
35
     * Setup identity
36
     *
37
     * @param  EntityInterface $identity
38
     * @return void
39
     */
40 3
    public function setIdentity(EntityInterface $identity)
41
    {
42
        // save identity to Auth
43 3
        $this->identity = $identity;
44
        // save identity to session
45 3
        Session::set('auth:identity', $identity);
46
        // save user agent to session
47 3
        Session::set('auth:agent', Request::getServer('HTTP_USER_AGENT'));
48 3
    }
49
50
    /**
51
     * Return identity if user agent is correct
52
     *
53
     * @return EntityInterface|null
54
     */
55 6
    public function getIdentity()
56
    {
57 6
        if (!$this->identity) {
58
            // check user agent
59 3
            if (Session::get('auth:agent') == Request::getServer('HTTP_USER_AGENT')) {
60
                $this->identity = Session::get('auth:identity');
61
            } else {
62 3
                $this->clearIdentity();
63
            }
64
        }
65 6
        return $this->identity;
66
    }
67
68
    /**
69
     * Clear identity and user agent information
70
     *
71
     * @return void
72
     */
73 635
    public function clearIdentity()
74
    {
75 635
        $this->identity = null;
76 635
        Session::delete('auth:identity');
77 635
        Session::delete('auth:agent');
78 635
    }
79
}
80