Authenticator::authenticate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.2
cc 4
eloc 9
nc 4
nop 2
1
<?php
2
3
namespace WZRD\Auth;
4
5
use Exception;
6
use WZRD\Contracts\Auth\Authenticator as AuthenticatorContract;
7
use WZRD\Contracts\Auth\Provider;
8
9
class Authenticator implements AuthenticatorContract
10
{
11
    /**
12
     * Authenticate data with providers.
13
     *
14
     * @param array                          $data
15
     * @param WZRD\Contracts\Auth\Provider[] $providers
16
     *
17
     * @return mixed
18
     */
19
    public function authenticate(array $data, array $providers)
20
    {
21
        // Initialize the exceptions stack
22
        $exceptions = [];
23
24
        // Try to authenticate
25
        foreach ($providers as $provider) {
26
            if ($provider instanceof Provider) {
27
                try {
28
                    return $provider->authenticate($data);
29
                } catch (Exception $e) {
30
                    $exceptions[] = $e;
31
                }
32
            }
33
        }
34
35
        // If no values are returned, throw the first exception
36
        throw array_shift($exceptions);
37
    }
38
}
39