Completed
Push — master ( 47e08a...15a70c )
by Luka
02:38
created

functions.php ➔ request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 5
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
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
133
/**
134
 * Manually API Request
135
 *
136
 * @param  Mastodon        $service A instance object of Mastodon class
137
 * @param  string          $method  HTTP Method (GET, POST, PUT, DELETE, ...)
138
 * @param  string          $path    API Path (URL)
139
 * @param  array           $options Options for GuzzleHttp
140
 * @param  string|string[] $class   A class name of return value
141
 * @return \Psr\Http\Message\ResponseInterface|Entity\Entity|Entity\Entity[]|mixed Returns ResponseInterface if $class is NULL.
142
 */
143
function request(Mastodon $service, $method, $path, $options, $class = null)
144
{
145
    $response = $service->client->requestAPI($method, $path, $options, $service->session);
0 ignored issues
show
Documentation introduced by
The property $client is declared private in Baguette\Mastodon\Mastodon. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property $session is declared private in Baguette\Mastodon\Mastodon. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
147
    if ($class === null) {
148
        return $response;
149
    }
150
151
    return Entity\map($class, \GuzzleHttp\json_decode($response->getBody(), true));
152
}
153