Failed Conditions
Push — ng ( 3a2d0f...7d4708 )
by Florent
04:04
created

SectorIdentifierUriRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 10 2
A checkSectorIdentifierUri() 0 17 2
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\Server\ClientRegistrationEndpoint\Rule;
15
16
use Http\Client\HttpClient;
17
use Http\Message\ResponseFactory;
18
use OAuth2Framework\Component\Server\Core\Client\ClientId;
19
use OAuth2Framework\Component\Server\Core\DataBag\DataBag;
20
21
final class SectorIdentifierUriRule implements Rule
22
{
23
    /**
24
     * @var HttpClient
25
     */
26
    private $client;
27
28
    /**
29
     * @var ResponseFactory
30
     */
31
    private $responseFactory;
32
33
    /**
34
     * SectorIdentifierUriRule constructor.
35
     *
36
     * @param ResponseFactory $responseFactory
37
     * @param HttpClient      $client
38
     */
39
    public function __construct(ResponseFactory $responseFactory, HttpClient $client)
40
    {
41
        $this->responseFactory = $responseFactory;
42
        $this->client = $client;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, callable $next): DataBag
49
    {
50
        if ($commandParameters->has('sector_identifier_uri')) {
51
            Assertion::url($commandParameters->get('sector_identifier_uri'), sprintf('The sector identifier URI "%s" is not valid.', $commandParameters->get('sector_identifier_uri')));
52
            $this->checkSectorIdentifierUri($commandParameters->get('sector_identifier_uri'));
53
            $validatedParameters = $validatedParameters->with('sector_identifier_uri', $commandParameters->get('sector_identifier_uri'));
54
        }
55
56
        return $next($clientId, $commandParameters, $validatedParameters);
57
    }
58
59
    /**
60
     * @param string $url
61
     *
62
     * @throws \InvalidArgumentException
63
     */
64
    private function checkSectorIdentifierUri(string $url)
65
    {
66
        $allowedProtocols = ['https'];
67
        Assertion::inArray(mb_substr($url, 0, mb_strpos($url, '://', 0, '8bit'), '8bit'), $allowedProtocols, sprintf('The provided sector identifier URI is not valid: scheme must be one of the following: %s.', implode(', ', $allowedProtocols)));
68
        $request = $this->responseFactory->createRequest('GET', $url);
69
        $response = $this->client->sendRequest($request);
70
        Assertion::eq(200, $response->getStatusCode(), sprintf('Unable to get Uris from the Sector Identifier Uri "%s".', $url));
71
72
        $body = $response->getBody()->getContents();
73
        $data = json_decode($body, true);
74
        Assertion::isArray($data, 'The provided sector identifier URI is not valid: bad response.');
75
        Assertion::notEmpty($data, 'The provided sector identifier URI is not valid: it must contain at least one URI.');
76
        foreach ($data as $sector_url) {
77
            Assertion::url($sector_url, 'The provided sector identifier URI is not valid: it must contain only URIs.');
78
            Assertion::inArray(mb_substr($sector_url, 0, mb_strpos($sector_url, '://', 0, '8bit'), '8bit'), $allowedProtocols, sprintf('An URL provided in the sector identifier URI is not valid: scheme must be one of the following: %s.', implode(', ', $allowedProtocols)));
79
        }
80
    }
81
}
82