Completed
Push — master ( 61dfdd...7faec7 )
by Luka
02:26
created

functions.php ➔ http()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
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\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
{
26 1
    $scope = null;
27 1
    $grant = null;
28 1
    $authorization = null;
29
30 1
    $client = new Client($instance);
31
32 1
    if (isset($options['scope'])) {
33 1
        $scope = scope($options['scope']);
34
    }
35
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
    }
40
41 1
    if (isset($options['grant'])) {
42 1
        $grant = grant($options['grant']);
43
    }
44
45 1
    if (isset($options['authorization'])) {
46 1
        $authorization = authorization($options['authorization']);
47
    }
48
49
    //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...
50
51 1
    $auth_factory = new Service\AuthFactory($client, $client_id, $client_secret);
52 1
    if ($grant !== null) {
53 1
        $auth_factory->setGrant($grant);
54
    }
55
56 1
    $session = new Service\SessionStorage($auth_factory, $scope);
57 1
    if ($authorization !== null) {
58 1
        $session->setAuthorization($authorization);
59
    }
60
61 1
    return new Mastodon($client, $session);
62
}
63
64
/**
65
 * @param  Scope|string|string[]
66
 * @return Scope
67
 */
68
function scope($scope)
69
{
70 6
    if (is_array($scope)) {
71 1
        return new Scope($scope);
72
    } elseif ($scope instanceof Scope) {
73 1
        return $scope;
74
    }
75
76 4
    return new Scope(explode(' ', $scope));
77
}
78
79
/**
80
 * @param  string $toot_string
81
 * @param  array  $options
82
 * @return Service\Toot
83
 */
84
function toot($toot_string, array $options = [])
85
{
86 1
    return new Service\Toot($toot_string, $options);
87
}
88
89
/**
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
/**
99
 * @return Grant\Grant
100
 */
101
function grant(array $data)
102
{
103 5
    if (isset($data['username'], $data['password'])) {
104 5
        return new Grant\PasswordCredential($data['username'], $data['password']);
105
    }
106
}
107
108
/**
109
 * @return Service\Authorization
110
 */
111
function authorization(array $data)
112
{
113 1
    return Service\Authorization::fromObject((object)$data);
114
}
115
116
/**
117
 * @return \GuzzleHttp\ClientInterface
118
 */
119
function http(\GuzzleHttp\ClientInterface $client = null)
120
{
121
    /** @var \GuzzleHttp\ClientInterface */
122 1
    static $cached_client;
123
124 1
    if ($client !== null) {
125
        $cached_client = $client;
126 1
    } elseif ($cached_client === null) {
127 1
        $cached_client = new \GuzzleHttp\Client;
128
    }
129
130 1
    return $cached_client;
131
}
132