Failed Conditions
Push — ng ( 977556...d85ce7 )
by Florent
03:52
created

SectorIdentifierUriRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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