Authentication   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 57
ccs 12
cts 12
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A error() 0 3 1
A validate() 0 5 2
A authenticate() 0 9 3
A success() 0 3 1
1
<?php
2
3
namespace Rougin\Authsum;
4
5
use Rougin\Authsum\Checker\CheckerInterface;
6
7
/**
8
 * Authentication
9
 *
10
 * @package Authsum
11
 * @author  Rougin Gutib <[email protected]>
12
 */
13
class Authentication
14
{
15
    const NOT_FOUND = 0;
16
17
    const INVALID = 1;
18
19
    /**
20
     * Authenticates the given credentials.
21
     *
22
     * @param  \Rougin\Authsum\Checker\CheckerInterface $checker
23
     * @param  array                                    $credentials
24
     * @return mixed
25
     */
26 49
    public function authenticate(CheckerInterface $checker, array $credentials)
27
    {
28 49
        if ($this->validate($credentials) === true) {
29 38
            $matched = ($match = $checker->check($credentials)) !== false;
30
31 38
            return ($matched) ? $this->success($match) : $this->error();
32
        }
33
34 11
        return $this->error(self::INVALID);
35
    }
36
37
    /**
38
     * Checks if the authentication has an occured error.
39
     *
40
     * @param  integer $type
41
     * @return mixed
42
     */
43 24
    protected function error($type = self::NOT_FOUND)
44
    {
45 24
        return $type;
46
    }
47
48
    /**
49
     * Checks if the authentication is successful.
50
     *
51
     * @param  mixed $match
52
     * @return mixed
53
     */
54 25
    protected function success($match)
55
    {
56 25
        return $match;
57
    }
58
59
    /**
60
     * Validates the given credentials.
61
     *
62
     * @param  array  $credentials
63
     * @return boolean
64
     */
65 27
    protected function validate(array $credentials)
66
    {
67 27
        $keys = array_keys($credentials);
68
69 27
        return isset($keys[0]) && isset($keys[1]);
70
    }
71
}
72