ClientsManager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
eloc 23
c 2
b 1
f 0
dl 0
loc 49
rs 10
ccs 24
cts 25
cp 0.96

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resetClient() 0 3 1
A create() 0 10 2
A get() 0 21 5
1
<?php
2
3
namespace ByTIC\Hello\Models\Clients\PersonalAccess;
4
5
use ByTIC\Hello\Models\Clients\Client;
6
use ByTIC\Hello\Utility\ClientsHelper;
7
use ByTIC\Hello\Utility\GrantsHelper;
8
use ByTIC\Hello\Utility\ModelsHelper;
9
10
/**
11
 * Class ClientsManager
12
 * @package ByTIC\Hello\Models\Clients\PersonalAccess
13
 */
14
class ClientsManager
15
{
16
    protected static $client = null;
17
18
    /**
19
     * @return \ByTIC\Hello\Models\Clients\Client
20
     */
21 3
    public static function get()
22
    {
23 3
        if (static::$client !== null) {
24
            return static::$client;
25
        }
26
27 3
        if (ClientsHelper::$personalAccessClientId > 0) {
28 1
            $client = ModelsHelper::clients()->findOne(ClientsHelper::$personalAccessClientId);
29 1
            if ($client instanceof Client) {
30 1
                static::$client = $client;
31 1
                return $client;
32
            }
33
        }
34 2
        $clients = ModelsHelper::clients()->findByRedirect(ClientsHelper::PERSONAL_ACCESS_REDIRECT_URI);
35 2
        if (count($clients) === 1) {
36 1
            static::$client = $clients->current();
37 1
            return $clients->current();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $clients->current() also could return the type Nip\Records\AbstractMode...ections\Collection|true which is incompatible with the documented return type ByTIC\Hello\Models\Clients\Client.
Loading history...
38
        }
39
40 1
        static::$client = static::create();
41 1
        return static::$client;
42
    }
43
44 6
    public static function resetClient()
45
    {
46 6
        static::$client = null;
47 6
    }
48
49
    /**
50
     * @param string $name
51
     * @return Client
52
     */
53 4
    public static function create($name = null)
54
    {
55 4
        $client = ModelsHelper::clients()->getNew();
56
57 4
        $name = empty($name) ? 'Personal Access Client' : $name;
58 4
        $client->setName($name);
59 4
        $client->addGrants(GrantsHelper::GRANT_TYPE_PERSONAL_ACCESS);
60 4
        $client->setRedirectUri(ClientsHelper::PERSONAL_ACCESS_REDIRECT_URI);
61 4
        $client->save();
62 4
        return $client;
63
    }
64
}
65