Completed
Push — master ( 0d5f24...6df70d )
by Luka
04:20
created

functions.php ➔ authorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Service;
14
use Baguette\Mastodon\Service\Scope;
15
16
/**
17
 * @param  string   $instance
18
 * @param  string   $client_id
19
 * @param  string   $client_secret
20
 * @param  array    $options
21
 * @return Mastodon
22
 */
23
function session($instance, $client_id, $client_secret, array $options)
24
{
25 1
    $scope = null;
26 1
    $credential = null;
27 1
    $authorization = null;
28
29 1
    $client = new Client($instance);
30
31 1
    if (isset($options['scope'])) {
32 1
        $scope = scope($options['scope']);
33
    }
34
35 1
    if (isset($options['credential'])) {
36 1
        $credential = credential($options['credential']);
37
    }
38
39 1
    if (isset($options['authorization'])) {
40 1
        $authorization = authorization($options['authorization']);
41
    }
42
43
    //throw new \LogicException('"scope" is not set.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
44
45 1
    $auth_factory = new Service\AuthFactory($client, $client_id, $client_secret);
46 1
    if ($credential !== null) {
47 1
        $auth_factory->setCredential($credential);
48
    }
49
50 1
    $session = new Service\SessionStorage($auth_factory, $scope);
51 1
    if ($authorization !== null) {
52 1
        $session->setAuthorization($authorization);
53
    }
54
55 1
    return new Mastodon($client, $session);
56
}
57
58
/**
59
 * @param  Scope|string|string[]
60
 * @return Scope
61
 */
62
function scope($scope)
63
{
64 6
    if (is_array($scope)) {
65 1
        return new Scope($scope);
66
    } elseif ($scope instanceof Scope) {
67 1
        return $scope;
68
    }
69
70 4
    return new Scope(explode(' ', $scope));
71
}
72
73
/**
74
 * @param  string $toot_string
75
 * @param  array  $options
76
 * @return Service\Toot
77
 */
78
function toot($toot_string, array $options = [])
79
{
80 1
    return new Service\Toot($toot_string, $options);
81
}
82
83
/**
84
 * @return Service\Credential
85
 */
86
function credential(array $data)
87
{
88 5
    if (isset($data['username'], $data['password'])) {
89 5
        return new Service\PasswordCredential($data['username'], $data['password']);
90
    }
91
}
92
93
/**
94
 * @return Service\Authorization
95
 */
96
function authorization(array $data)
97
{
98 1
    return Service\Authorization::fromObject((object)$data);
99
}
100
101
/**
102
 * @return \GuzzleHttp\ClientInterface
103
 */
104
function http(\GuzzleHttp\ClientInterface $client = null)
105
{
106
    /** @var \GuzzleHttp\ClientInterface */
107 1
    static $cached_client;
108
109 1
    if ($client !== null) {
110
        $cached_client = $client;
111 1
    } elseif ($cached_client === null) {
112 1
        $cached_client = new \GuzzleHttp\Client;
113
    }
114
115 1
    return $cached_client;
116
}
117