Completed
Push — master ( 32af66...6df8b6 )
by Kirill
02:42
created

AbstractResource::currentUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace Gitter\Resources;
9
10
use Gitter\Route;
11
use Gitter\Client;
12
use Serafim\Properties\Properties;
13
use Gitter\ClientAdapter\AdapterInterface;
14
use Gitter\ClientAdapter\SyncAdapterInterface;
15
16
/**
17
 * Class AbstractResource
18
 * @package Gitter\Resources
19
 *
20
 * @property-read $this|AbstractResource $sync
21
 * @property-read $this|AbstractResource $async
22
 * @property-read $this|AbstractResource $stream
23
 */
24
abstract class AbstractResource implements ResourceInterface
25
{
26
    use Properties;
27
28
    /**
29
     * @var Client
30
     */
31
    private $client;
32
33
    /**
34
     * @var array
35
     */
36
    private static $currentUserId = [];
37
38
    /**
39
     * @var AdapterInterface|null
40
     */
41
    private $adapter;
42
43
    /**
44
     * AbstractResource constructor.
45
     * @param Client $client
46
     */
47
    public function __construct(Client $client)
48
    {
49
        $this->client = $client;
50
    }
51
52
    /**
53
     * @return array
54
     * @throws \InvalidArgumentException
55
     */
56
    protected function currentUser(): array
57
    {
58
        if (!array_key_exists($this->client->token, self::$currentUserId)) {
59
            $response = $this->using(AdapterInterface::TYPE_SYNC)->request(Route::get('user')->toApi());
60
            $userId   = $response[0] ?? null;
61
62
            if ($userId === null) {
63
                throw new \InvalidArgumentException('Broken request. Can not fetch current authenticated user');
64
            }
65
66
            self::$currentUserId[$this->client->token] = $userId;
67
        }
68
69
        return self::$currentUserId[$this->client->token];
70
    }
71
72
    /**
73
     * @return string
74
     * @throws \InvalidArgumentException
75
     */
76
    protected function currentUserId(): string
77
    {
78
        return $this->currentUser()['id'];
79
    }
80
81
    /**
82
     * @return $this
83
     * @throws \InvalidArgumentException
84
     */
85
    protected function getSync()
86
    {
87
        $this->adapter = $this->using(AdapterInterface::TYPE_SYNC);
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return $this
94
     * @throws \InvalidArgumentException
95
     */
96
    protected function getAsync()
97
    {
98
        $this->adapter = $this->using(AdapterInterface::TYPE_ASYNC);
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return $this
105
     * @throws \InvalidArgumentException
106
     */
107
    protected function getStream()
108
    {
109
        $this->adapter = $this->using(AdapterInterface::TYPE_STREAM);
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param Route $route
116
     * @return mixed
117
     */
118
    protected function fetch(Route $route)
119
    {
120
        $adapter = $this->adapter;
121
122
        if (!($this->adapter instanceof SyncAdapterInterface)) {
123
            $this->resetAdapter();
124
        }
125
126
        if ($adapter === null) {
127
            $adapter = $this->adapter;
128
        }
129
130
        return $adapter->request($route);
131
    }
132
133
    /**
134
     * @param string $type
135
     * @return AdapterInterface
136
     * @throws \InvalidArgumentException
137
     */
138
    protected function using(string $type)
139
    {
140
        return $this->client->adapters->using($type);
141
    }
142
143
    /**
144
     * @return AdapterInterface
145
     */
146
    private function resetAdapter(): AdapterInterface
147
    {
148
        try {
149
            $this->adapter = $this->client->adapters->using(AdapterInterface::TYPE_SYNC);
150
        } catch (\Throwable $e) {
151
            // Adapters valid ever
152
        }
153
154
        return $this->adapter;
155
    }
156
}