ClientAuthentication::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 35
ccs 14
cts 14
cp 1
crap 3
rs 9.36
c 0
b 0
f 0
1
<?php
2
namespace Germania\PermanentAuth;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
8
/**
9
 * This Callable reads and parses a "permanent login" cookie and returns parsed cookie values.
10
 *
11
 * Returns the parser result, if invoked, or FALSE if cookie missing or persing failed.
12
 *
13
 * The constructor expects two Callabes; one for getting the cookie value and one for parsing.
14
 */
15
class ClientAuthentication
16
{
17
18
    /**
19
     * @var Callable
20
     */
21
    public $cookie_getter;
22
23
    /**
24
     * @var Callable
25
     */
26
    public $cookie_parser;
27
28
    /**
29
     * @var string
30
     */
31
    public $cookie_name;
32
33
    /**
34
     * @var LoggerInterface
35
     */
36
    public $logger;
37
38
39
    /**
40
     * Added to $status when cookie was found
41
     */
42
    CONST FOUND   = 1;
43
44
    /**
45
     * Added to $status when parsing succeeded
46
     */
47
    CONST VALID    = 2;
48
49
    /**
50
     * Hold status information
51
     * @var integer
52
     */
53
    public $status = 0;
54
55
    /**
56
     * @param Callable             $cookie_getter  Any callable that accepts a cookie name and returns its value
57
     * @param Callable             $cookie_parser  Any callable that parses a cookie value into a selector and token pair
58
     * @param string               $cookie_name    The permanent login cookie name
59
     * @param LoggerInterface|null $logger         Optional: PSR-3 Logger
60
     */
61 15
    public function __construct( Callable $cookie_getter, Callable $cookie_parser, $cookie_name, LoggerInterface $logger = null)
62
    {
63 15
        $this->cookie_getter = $cookie_getter;
64 15
        $this->cookie_name   = $cookie_name;
65 15
        $this->cookie_parser = $cookie_parser;
66 15
        $this->logger        = $logger ?: new NullLogger;
67 15
    }
68
69
70
71
    /**
72
     * Returns a selector and token pair like this:
73
     *
74
     *     <?php
75
     *     $result = (object) [
76
     *         'selector' => 'foo',
77
     *         'token'    => 'fdskfkqrqgqfi4t2iq89fu3uf8fhq'
78
     *     ];
79
     *     ?>
80
     *
81
     * If no cookie was sent along, or parsing failed, FALSE will be returned.
82
     *
83
     * @return mixed Success: StdClass object with selector and token or FALSE
84
     */
85 15
    public function __invoke()
86
    {
87
88
        // Read Cookie
89 15
        $cookie_getter = $this->cookie_getter;
90 15
        if (!$cookie_value = $cookie_getter( $this->cookie_name )):
91
            // Also change message in unit test assertions!
92 5
            $this->logger->debug("No Permanent Login cookie sent along");
93 5
            return false;
94
        endif;
95
96
        // Set status
97 10
        $this->status = $this->status | static::FOUND;
98
99
        // Also change message in unit test assertions!
100 10
        $this->logger->info("Received Permanent Login cookie");
101
102
103
        // Parse Cookie
104 10
        $cookie_parser = $this->cookie_parser;
105 10
        $selector_token_pair = $cookie_parser( $cookie_value );
106 10
        if (!$selector_token_pair) {
107
            // Also change message in unit test assertions!
108 5
            $this->logger->debug("Could not decrypt selector and token pair");
109 5
            return false;
110
        }
111
112
        // Set status
113 5
        $this->status = $this->status | static::VALID;
114
115
        // Return parsed value
116 5
        return $selector_token_pair;
117
118
119
    }
120
}
121