Failed Conditions
Push — master ( b8d841...bc596e )
by Florent
28:20
created

AccountResolver::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\WebFingerEndpoint\IdentifierResolver;
15
16
use function League\Uri\parse;
17
18
final class AccountResolver implements IdentifierResolver
19
{
20
    public function supports(string $resource): bool
21
    {
22
        $uri = parse($resource);
23
24
        return 'acct' === $uri['scheme'];
25
    }
26
27
    public function resolve(string $resource): Identifier
28
    {
29
        $uri = parse($resource);
30
        if (!\is_string($uri['path'])) {
31
            throw new \InvalidArgumentException('Invalid resource.');
32
        }
33
        $parts = \explode('@', $uri['path']);
34
        if (2 !== \count($parts)) {
35
            throw new \InvalidArgumentException('Invalid resource.');
36
        }
37
        $parts[0] = \str_replace('%40', '@', $parts[0]);
38
        $pos = \mb_strpos($parts[1], ':');
39
        if (false === $pos) {
40
            $port = null;
41
        } else {
42
            $port = \intval(\mb_substr($parts[1], $pos + 1));
43
            $parts[1] = \mb_substr($parts[1], 0, $pos);
44
        }
45
46
        return new Identifier($parts[0], $parts[1], $port);
47
    }
48
}
49