Test Failed
Push — master ( c83fb3...05e696 )
by Luka
02:24
created

AuthFactory::setGrant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Baguette\Mastodon\Service;
4
5
use Baguette\Mastodon;
6
use Baguette\Mastodon\Client;
7
use Baguette\Mastodon\Grant\Grant;
8
9
/**
10
 * Mastodon Anthorization object factory
11
 *
12
 * @author    USAMI Kenta <[email protected]>
13
 * @copyright 2017 Baguette HQ
14
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
15
 * @property-read Client     $client
16
 * @property-read string     $client_id
17
 * @property-read string     $client_secret
18
 * @property-read Grant      $grant
19
 * @property-read Scope      $scope
20
 */
21
class AuthFactory
22
{
23
    use \Teto\Object\PrivateGetter;
24
25
    /** @var Client */
26
    private $client;
27
    /** @var string */
28
    private $client_id;
29
    /** @var string */
30
    private $client_secret;
31
    /** @var Grant */
32
    private $grant;
33
34
    /**
35
     * @param Client $client
36
     * @param string $client_id
37
     * @param string $client_secret
38 5
     */
39
    public function __construct(Client $client, $client_id, $client_secret)
40 5
    {
41 5
        $this->client = $client;
42 5
        $this->client_id = $client_id;
43 5
        $this->client_secret = $client_secret;
44
    }
45
46
    /**
47
     * @param Grant $grant
48 4
     */
49
    public function setGrant(Grant $grant)
50 4
    {
51 4
        $this->grant = $grant;
52
    }
53
54
    /**
55
     * @return Authorization
56
     */
57
    public function authorize(Scope $scope)
58
    {
59
        $res = $this->grant->auth(Mastodon\http(), $this, $scope);
60
61
        if ($res->getStatusCode() !== 200) {
62
            throw new AuthorizationException;
63
        }
64
65
        return Authorization::fromObject(\GuzzleHttp\json_decode($res->getBody()));
66
    }
67
}
68