CallbackAuthenticator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vectorface\SnappyRouter\Authentication;
4
5
use \Closure;
6
7
/**
8
 * Authenticator implementation that leaves the authentication up to a Closure.
9
 *
10
 * For example:
11
 * \code{.php}
12
 * $auth = new CallbackAuthenticator(function($credentials) use ($externalAuth) {
13
 *     return $externalAuth->login($credentials['username'], $credentials['password']);
14
 * });
15
 * \endcode
16
 *
17
 * @copyright Copyright (c) 2014, VectorFace, Inc.
18
 * @author J. Anderson <[email protected]>
19
 */
20
class CallbackAuthenticator extends AbstractAuthenticator
21
{
22
    /** Stores the callback to be used for authentication.*/
23
    private $callback;
24
25
    /**
26
     * Wrap another authentication mechanism via a callback.
27
     *
28
     * @param Closure $callback The callback, which is expected to have the same signature as $this->authenticate.
29
     */
30 3
    public function __construct(Closure $callback)
31
    {
32 3
        $this->callback = $callback;
33 3
    }
34
35
    /**
36
     * Authenticate a set of credentials using a callback.
37
     *
38
     * @param mixed $credentials One or more credentials; A string password, or an array for multi-factor auth.
39
     * @return bool Returns true if the identity was authenticated, or false otherwise.
40
     */
41 3
    public function authenticate($credentials)
42
    {
43 3
        return (bool)call_user_func($this->callback, $credentials);
44
    }
45
}
46