Failed Conditions
Push — ng ( 17df75...6face8 )
by Florent
04:09
created

AccountResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 6 1
A resolve() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\IssuerDiscoveryEndpoint\IdentifierResolver;
15
16
use function League\Uri\parse;
17
18
class AccountResolver implements IdentifierResolver
19
{
20
    /**
21
     * @param string $resource_name
22
     *
23
     * @return bool
24
     */
25
    public function supports(string $resource_name): bool
26
    {
27
        $uri = parse($resource_name);
28
29
        return 'acct' === $uri['scheme'];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function resolve(string $resource_name): Identifier
36
    {
37
        $uri = parse($resource_name);
38
        if (!is_string($uri['path'])) {
39
            throw new \InvalidArgumentException('Invalid resource.');
40
        }
41
        $parts = explode('@', $uri['path']);
42
        if (2 !== count($parts)) {
43
            throw new \InvalidArgumentException('Invalid resource.');
44
        }
45
        $parts[0] = str_replace('%40', '@', $parts[0]);
46
        $pos = strpos($parts[1], ':');
47
        if ($pos === false) {
48
            $port = null;
49
        } else {
50
            $port = intval(substr($parts[1], $pos+1));
51
            $parts[1] = substr($parts[1], 0, $pos);
52
        }
53
54
        return new Identifier($parts[0], $parts[1], $port);
55
    }
56
}
57