1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Mastodon API functions |
5
|
|
|
* |
6
|
|
|
* @author USAMI Kenta <[email protected]> |
7
|
|
|
* @copyright 2017 Baguette HQ |
8
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Baguette\Mastodon; |
12
|
|
|
|
13
|
|
|
use Baguette\Mastodon\Grant; |
14
|
|
|
use Baguette\Mastodon\Service; |
15
|
|
|
use Baguette\Mastodon\Service\Scope; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $instance |
19
|
|
|
* @param string $client_id |
20
|
|
|
* @param string $client_secret |
21
|
|
|
* @param array $options |
22
|
|
|
* @return Mastodon |
23
|
|
|
*/ |
24
|
|
|
function session($instance, $client_id, $client_secret, array $options) |
25
|
1 |
|
{ |
26
|
1 |
|
$scope = null; |
27
|
1 |
|
$grant = null; |
28
|
|
|
$authorization = null; |
29
|
1 |
|
|
30
|
|
|
$client = new Client($instance); |
31
|
1 |
|
|
32
|
1 |
|
if (isset($options['scope'])) { |
33
|
|
|
$scope = scope($options['scope']); |
34
|
|
|
} |
35
|
1 |
|
|
36
|
1 |
|
if (isset($options['credential'])) { |
37
|
|
|
trigger_error('`credential` is obsolete option. Use `grant` instead.', E_USER_DEPRECATED); |
38
|
|
|
$grant = credential($options['credential']); |
39
|
1 |
|
} |
40
|
1 |
|
|
41
|
|
|
if (isset($options['grant'])) { |
42
|
|
|
$grant = grant($options['grant']); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if (isset($options['authorization'])) { |
46
|
1 |
|
$authorization = authorization($options['authorization']); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
//throw new \LogicException('"scope" is not set.'); |
|
|
|
|
50
|
1 |
|
|
51
|
1 |
|
$auth_factory = new Service\AuthFactory($client, $client_id, $client_secret); |
52
|
1 |
|
if ($grant !== null) { |
53
|
|
|
$auth_factory->setGrant($grant); |
54
|
|
|
} |
55
|
1 |
|
|
56
|
|
|
$session = new Service\SessionStorage($auth_factory, $scope); |
57
|
|
|
if ($authorization !== null) { |
58
|
|
|
$session->setAuthorization($authorization); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new Mastodon($client, $session); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
/** |
65
|
1 |
|
* @param Scope|string|string[] |
66
|
|
|
* @return Scope |
67
|
1 |
|
*/ |
68
|
|
|
function scope($scope) |
69
|
|
|
{ |
70
|
4 |
|
if (is_array($scope)) { |
71
|
|
|
return new Scope($scope); |
72
|
|
|
} elseif ($scope instanceof Scope) { |
73
|
|
|
return $scope; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new Scope(explode(' ', $scope)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
1 |
|
* @param string $toot_string |
81
|
|
|
* @param array $options |
82
|
|
|
* @return Service\Toot |
83
|
|
|
*/ |
84
|
|
|
function toot($toot_string, array $options = []) |
85
|
|
|
{ |
86
|
|
|
return new Service\Toot($toot_string, $options); |
87
|
|
|
} |
88
|
5 |
|
|
89
|
5 |
|
/** |
90
|
|
|
* @deprecated |
91
|
|
|
*/ |
92
|
|
|
function credential(array $data) |
93
|
|
|
{ |
94
|
|
|
trigger_error('credential() is obsolete function. Use grant() instead.', E_USER_DEPRECATED); |
95
|
|
|
return grant($data); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
/** |
99
|
|
|
* @return Grant\Grant |
100
|
|
|
*/ |
101
|
|
|
function grant(array $data) |
102
|
|
|
{ |
103
|
|
|
if (isset($data['username'], $data['password'])) { |
104
|
|
|
return new Grant\PasswordCredential($data['username'], $data['password']); |
105
|
|
|
} |
106
|
|
|
} |
107
|
1 |
|
|
108
|
|
|
/** |
109
|
1 |
|
* @return Service\Authorization |
110
|
|
|
*/ |
111
|
1 |
|
function authorization(array $data) |
112
|
1 |
|
{ |
113
|
|
|
return Service\Authorization::fromObject((object)$data); |
114
|
|
|
} |
115
|
1 |
|
|
116
|
|
|
/** |
117
|
|
|
* @return \GuzzleHttp\ClientInterface |
118
|
|
|
*/ |
119
|
|
|
function http(\GuzzleHttp\ClientInterface $client = null) |
120
|
|
|
{ |
121
|
|
|
/** @var \GuzzleHttp\ClientInterface */ |
122
|
|
|
static $cached_client; |
123
|
|
|
|
124
|
|
|
if ($client !== null) { |
125
|
|
|
$cached_client = $client; |
126
|
|
|
} elseif ($cached_client === null) { |
127
|
|
|
$cached_client = new \GuzzleHttp\Client; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $cached_client; |
131
|
|
|
} |
132
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.