CloudConvert::createResourceOwner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Osavchenko\OAuth2\Client\Provider;
6
7
use League\OAuth2\Client\Provider\AbstractProvider;
8
use League\OAuth2\Client\Token\AccessToken;
9
use Osavchenko\OAuth2\Client\Provider\Exception\CloudConvertIdentityProviderException;
10
use Psr\Http\Message\ResponseInterface;
11
12
class CloudConvert extends AbstractProvider
13
{
14
    protected const BASE_URL = 'https://cloudconvert.com';
15
    protected const API_URL = 'https://api.cloudconvert.com';
16
17
    protected const SANDBOX_BASE_URL = 'https://sandbox.cloudconvert.com';
18
    protected const SANDBOX_API_URL = 'https://api.sandbox.cloudconvert.com';
19
20
    public const SCOPE_USER_READ = 'user.read';
21
    public const SCOPE_USER_WRITE = 'user.write';
22
    public const SCOPE_TASK_READ = 'task.read';
23
    public const SCOPE_TASK_WRITE = 'task.write';
24
    public const SCOPE_WEBHOOK_READ = 'webhook.read';
25
    public const SCOPE_WEBHOOK_WRITE = 'webhook.write';
26
27
    protected bool $isSandbox;
28
29
    public function __construct(array $options = [], array $collaborators = [])
30
    {
31
        $this->isSandbox = $options['sandbox'] ?? false;
32
33
        parent::__construct($options, $collaborators);
34
    }
35
36
37
    public function getBaseAuthorizationUrl(): string
38
    {
39
        return sprintf('%s/%s', $this->isSandbox ? self::SANDBOX_BASE_URL : self::BASE_URL, 'oauth/authorize');
40
    }
41
42
    public function getBaseAccessTokenUrl(array $params): string
43
    {
44
        return sprintf('%s/%s', $this->isSandbox ? self::SANDBOX_BASE_URL : self::BASE_URL, 'oauth/token');
45
    }
46
47
    public function getResourceOwnerDetailsUrl(AccessToken $token): string
48
    {
49
        return sprintf('%s/%s', $this->isSandbox ? self::SANDBOX_API_URL : self::API_URL, 'v2/users/me');
50
    }
51
52
    protected function getDefaultScopes(): array
53
    {
54
        return [self::SCOPE_USER_READ];
55
    }
56
57
    protected function checkResponse(ResponseInterface $response, $data): void
58
    {
59
        if ($response->getStatusCode() >= 400) {
60
            throw new CloudConvertIdentityProviderException(
61
                $data['message'] ?? $response->getReasonPhrase(),
62
                $response->getStatusCode(),
63
                (string) $response->getBody()
64
            );
65
        }
66
    }
67
68
    protected function createResourceOwner(array $response, AccessToken $token): CloudConvertResourceOwner
69
    {
70
        return new CloudConvertResourceOwner($response);
71
    }
72
73
    protected function getAuthorizationHeaders($token = null): array
74
    {
75
        if ($token === null) {
76
            return [];
77
        }
78
79
        return [
80
            'Authorization' => sprintf('Bearer %s', (string) $token),
81
        ];
82
    }
83
}
84