Passed
Push — master ( 37cafd...a8b392 )
by
unknown
07:18
created

SsoBackend   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 7 2
A open() 0 11 3
A initBackend() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Files\Backend\Seafile\Model;
4
5
use Datamate\SeafileApi\SeafileApi;
6
use Files\Backend\Seafile\Backend;
7
8
class SsoBackend
9
{
10
    /**
11
     * @var Config of the Backends files account
12
     */
13
    private ?Config $backendConfig = null;
14
15
    /**
16
     * Bind an SsoBackend
17
     *
18
     * @param ?SsoBackend $self
19
     * @psalm-param-out SsoBackend $self
20
     * @return SsoBackend
21
     */
22
    public static function bind(?SsoBackend &$self = null): SsoBackend
23
    {
24
        if ($self === null) {
25
            $self = new self();
26
        }
27
28
        return $self;
29
    }
30
31
    /**
32
     * hook for {@see Backend::init_backend()}
33
     *
34
     * @param Config $config
35
     * @return void
36
     */
37
    public function initBackend(Config $config): void
38
    {
39
        if ($this->backendConfig instanceof Config) {
40
            throw new \BadMethodCallException('backend is already configured');
41
        }
42
43
        $this->backendConfig = $config;
44
    }
45
46
    /**
47
     * hook for {@see Backend::open()}
48
     *
49
     * @return void
50
     */
51
    public function open(): void
52
    {
53
        if (!($this->backendConfig instanceof Config)) {
54
            // won't work w/o having the backend config for open()
55
            return;
56
        }
57
58
        if (isset($this->backendConfig['sso_auth_user_token'])) {
59
            $this->backendConfig->importConfigArray([
60
                'user' => SeafileApi::USER_PREFIX_AUTH_TOKEN . ConfigUtil::loadSmtpAddress(),
61
                'password' => $this->backendConfig['sso_auth_user_token'],
62
            ]);
63
        }
64
    }
65
}
66