Failed Conditions
Push — v7 ( 838dcb...318c5f )
by Florent
04:36
created

DownloadedJWKSet::downloadContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 13
nc 2
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Object;
13
14
use Assert\Assertion;
15
use Http\Client\HttpClient;
16
use Http\Message\RequestFactory;
17
18
/**
19
 * Class DownloadedJWKSet.
20
 */
21
abstract class DownloadedJWKSet implements JWKSetInterface
22
{
23
    use BaseJWKSet;
24
    use JWKSetPEM;
25
26
    /**
27
     * @var string
28
     */
29
    private $url;
30
31
    /**
32
     * @var HttpClient
33
     */
34
    private $client;
35
36
    /**
37
     * @var RequestFactory
38
     */
39
    private $requestFactory;
40
41
    /**
42
     * DownloadedJWKSet constructor.
43
     *
44
     * @param RequestFactory $requestFactory
45
     * @param HttpClient     $client
46
     * @param string         $url
47
     * @param bool           $allow_http_connection
48
     */
49
    public function __construct(RequestFactory $requestFactory, HttpClient $client, string $url, bool $allow_http_connection = false)
50
    {
51
        Assertion::false(false === filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED), 'Invalid URL.');
52
        $allowed_protocols = ['https'];
53
        if (true === $allow_http_connection) {
54
            $allowed_protocols[] = 'http';
55
        }
56
        Assertion::inArray(mb_substr($url, 0, mb_strpos($url, '://', 0, '8bit'), '8bit'), $allowed_protocols, sprintf('The provided sector identifier URI is not valid: scheme must be one of the following: %s.', json_encode($allowed_protocols)));
57
58
        $this->url = $url;
59
        $this->client = $client;
60
        $this->requestFactory = $requestFactory;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function addKey(JWKInterface $key)
67
    {
68
        //Not available
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function removeKey($index)
75
    {
76
        //Not available
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function getContent()
83
    {
84
        $request = $this->requestFactory->createRequest('GET', $this->url);
85
        $response = $this->client->sendRequest($request);
86
        Assertion::true($response->getStatusCode() >= 200 && $response->getStatusCode() < 300, 'Unable to get the content.');
87
        return (string) $response->getBody()->getContents();
88
    }
89
}
90