AuthFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 61.53%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
ccs 8
cts 13
cp 0.6153
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setGrant() 0 4 1
A authorize() 0 10 2
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
     */
39 6
    public function __construct(Client $client, $client_id, $client_secret)
40
    {
41 6
        $this->client = $client;
42 6
        $this->client_id = $client_id;
43 6
        $this->client_secret = $client_secret;
44 6
    }
45
46
    /**
47
     * @param Grant $grant
48
     */
49 4
    public function setGrant(Grant $grant)
50
    {
51 4
        $this->grant = $grant;
52 4
    }
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