Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

Hybridauth::authenticate()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 6
1
<?php
2
3
namespace ByTIC\Hello\Library\Hybridauth;
4
5
use Exception;
6
use Hybrid_Auth;
7
use Nip\Records\Locator\ModelLocator;
8
use Nip\Utility\Traits\SingletonTrait;
9
10
/**
11
 * Class Hybridauth
12
 * @package KM42\Hello\Library\Hybridauth
13
 */
14
class Hybridauth
15
{
16
    use SingletonTrait;
17
18
    protected $hybridauth = null;
19
20
    /**
21
     * @param $provider
22
     * @return Exception
23
     */
24
    public function authenticate($provider)
25
    {
26
        try {
27
            // try to authenticate with the selected provider
28
            $adapter = $this->getAuth()::authenticate($provider);
29
            // then grab the user profile
30
            return $adapter->getUserProfile();
31
        } catch (Exception $e) {
32
            return $e;
33
        }
34
    }
35
36
    /**
37
     * @return null|Hybrid_Auth
38
     */
39
    public function getAuth()
40
    {
41
        if ($this->hybridauth === null) {
42
            $this->initAuth();
43
        }
44
45
        return $this->hybridauth;
46
    }
47
48
    protected function initAuth()
49
    {
50
        $config = config('hybridauth');
51
        $config['base_url'] = ModelLocator::get('users')->getOAuthURL();
52
        $this->hybridauth = new Hybrid_Auth($config->toArray());
53
    }
54
}
55