ClientsManager::resetClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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