Failed Conditions
Push — master ( e962b4...04ef90 )
by Florent
06:44
created

checkSectorIdentifierUri()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 12
nc 4
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\AuthorizationEndpoint\Rule;
15
16
use Http\Client\HttpClient;
17
use Http\Message\RequestFactory;
18
use function League\Uri\parse;
19
use OAuth2Framework\Component\ClientRule\Rule;
20
use OAuth2Framework\Component\Core\Client\ClientId;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
23
final class SectorIdentifierUriRule implements Rule
24
{
25
    /**
26
     * @var HttpClient
27
     */
28
    private $client;
29
30
    /**
31
     * @var RequestFactory
32
     */
33
    private $requestFactory;
34
35
    /**
36
     * SectorIdentifierUriRule constructor.
37
     *
38
     * @param RequestFactory $requestFactory
39
     * @param HttpClient     $client
40
     */
41
    public function __construct(RequestFactory $requestFactory, HttpClient $client)
42
    {
43
        $this->requestFactory = $requestFactory;
44
        $this->client = $client;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, callable $next): DataBag
51
    {
52
        if ($commandParameters->has('sector_identifier_uri')) {
53
            $this->checkSectorIdentifierUri($commandParameters->get('sector_identifier_uri'));
54
            $validatedParameters = $validatedParameters->with('sector_identifier_uri', $commandParameters->get('sector_identifier_uri'));
55
        }
56
57
        return $next($clientId, $commandParameters, $validatedParameters);
58
    }
59
60
    private function checkSectorIdentifierUri(string $url)
61
    {
62
        $data = parse($url);
63
64
        if ('https' !== $data['scheme'] || null === $data['host']) {
65
            throw new \InvalidArgumentException(sprintf('The sector identifier URI "%s" is not valid.', $url));
66
        }
67
68
        $request = $this->requestFactory->createRequest('GET', $url);
69
        $response = $this->client->sendRequest($request);
70
        if (200 !== $response->getStatusCode()) {
71
            throw new \InvalidArgumentException(sprintf('Unable to get Uris from the Sector Identifier Uri "%s".', $url));
72
        }
73
74
        $body = $response->getBody()->getContents();
75
        $data = json_decode($body, true);
76
        if (!is_array($data) || empty($data)) {
77
            throw new \InvalidArgumentException('The provided sector identifier URI is not valid: it must contain at least one URI.');
78
        }
79
    }
80
}
81