Passed
Push — master ( 0f8e83...edb234 )
by Philippe
02:11 queued 12s
created

createTokenRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.9332
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Authorization;
4
5
use Illuminate\Auth\Passwords\DatabaseTokenRepository;
6
use Illuminate\Auth\Passwords\PasswordBroker;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
10
/** @inheritDoc */
11
class ChiefPasswordBrokerResolver
12
{
13
    private $app;
14
15 2
    public function __construct($app)
16
    {
17 2
        $this->app = $app;
18 2
    }
19
20 2
    public function resolve()
21
    {
22 2
        $config = $this->getConfig('chief');
23
24 2
        if (is_null($config)) {
25
            throw new InvalidArgumentException("Password resetter [chief] is not defined.");
26
        }
27
28 2
        return new ChiefPasswordBroker(
29 2
            $this->createTokenRepository($config),
30 2
            $this->app['auth']->createUserProvider($config['provider'] ?? null)
31
        );
32
    }
33
34 2
    protected function createTokenRepository(array $config)
35
    {
36 2
        $key = $this->app['config']['app.key'];
37
38 2
        if (Str::startsWith($key, 'base64:')) {
39 2
            $key = base64_decode(substr($key, 7));
40
        }
41
42 2
        $connection = $config['connection'] ?? null;
43
44 2
        return new DatabaseTokenRepository(
45 2
            $this->app['db']->connection($connection),
46 2
            $this->app['hash'],
47 2
            $config['table'],
48 2
            $key,
49 2
            $config['expire']
50
        );
51
    }
52
53 2
    private function getConfig($name)
54
    {
55 2
        return $this->app['config']["auth.passwords.{$name}"];
56
    }
57
}
58