ElegantUserProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieveByToken() 0 4 1
A __construct() 0 5 1
A removeToken() 0 4 1
A retrieveByCredentials() 0 9 1
A createModel() 0 6 1
1
<?php
2
3
namespace Magister\Services\Auth;
4
5
use GuzzleHttp\Client;
6
use Magister\Services\Contracts\Auth\UserProvider;
7
8
/**
9
 * Class ElegantUserProvider.
10
 */
11
class ElegantUserProvider implements UserProvider
12
{
13
    /**
14
     * The Elegant user model.
15
     *
16
     * @var string
17
     */
18
    protected $model;
19
20
    /**
21
     * The active connection.
22
     *
23
     * @var \GuzzleHttp\Client
24
     */
25
    protected $client;
26
27
    /**
28
     * Create a new elegant user provider instance.
29
     *
30
     * @param \GuzzleHttp\Client $client
31
     * @param string             $model
32
     */
33
    public function __construct(Client $client, $model)
34
    {
35
        $this->client = $client;
36
        $this->model = $model;
37
    }
38
39
    /**
40
     * Retrieve a user by their unique token.
41
     *
42
     * @return \Magister\Services\Database\Elegant\Model|null
43
     */
44
    public function retrieveByToken()
45
    {
46
        return $this->createModel()->newQuery()->first();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createModel()->newQuery()->first(); of type Magister\Services\Databa...se\Elegant\Builder|null adds the type Magister\Services\Database\Elegant\Builder to the return on line 46 which is incompatible with the return type declared by the interface Magister\Services\Contra...ovider::retrieveByToken of type Magister\Services\Database\Elegant\Model|null.
Loading history...
47
    }
48
49
    /**
50
     * Remove the token for the given user in storage.
51
     *
52
     * @return void
53
     */
54
    public function removeToken()
55
    {
56
        $this->client->getDefaultOption('cookies')->clear();
57
    }
58
59
    /**
60
     * Retrieve a user by the given credentials.
61
     *
62
     * @param array $credentials
63
     *
64
     * @return \Magister\Services\Database\Elegant\Model|null
65
     */
66
    public function retrieveByCredentials(array $credentials)
67
    {
68
        $body = ['json' => $credentials];
69
70
        $this->client->delete('sessies/huidige');
71
        $this->client->post('sessies', $body);
72
73
        return $this->retrieveByToken();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->retrieveByToken(); of type Magister\Services\Databa...se\Elegant\Builder|null adds the type Magister\Services\Database\Elegant\Builder to the return on line 73 which is incompatible with the return type declared by the interface Magister\Services\Contra...::retrieveByCredentials of type Magister\Services\Database\Elegant\Model|null.
Loading history...
74
    }
75
76
    /**
77
     * Create a new instance of the model.
78
     *
79
     * @return \Magister\Services\Database\Elegant\Model
80
     */
81
    public function createModel()
82
    {
83
        $class = '\\'.ltrim($this->model, '\\');
84
85
        return new $class();
86
    }
87
}
88