Failed Conditions
Push — v7 ( e264c8...089db6 )
by Florent
06:30
created

X5UFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 60
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B loadFromUrl() 0 27 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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 Jose\Component\KeyManagement;
15
16
use Http\Client\HttpClient;
17
use Http\Message\RequestFactory;
18
use Jose\Component\Core\JWK;
19
use Jose\Component\Core\JWKSet;
20
use Jose\Component\KeyManagement\KeyConverter\KeyConverter;
21
22
final class X5UFactory
23
{
24
    /**
25
     * @var HttpClient
26
     */
27
    private $client;
28
29
    /**
30
     * @var RequestFactory
31
     */
32
    private $requestFactory;
33
34
    /**
35
     * JKUManager constructor.
36
     *
37
     * @param HttpClient     $client
38
     * @param RequestFactory $requestFactory
39
     */
40
    public function __construct(HttpClient $client, RequestFactory $requestFactory)
41
    {
42
        $this->client = $client;
43
        $this->requestFactory = $requestFactory;
44
    }
45
46
    /**
47
     * @param string $url
48
     * @param array  $headers
49
     *
50
     * @throws \HttpRuntimeException
51
     *
52
     * @return JWKSet
53
     */
54
    public function loadFromUrl(string $url, array $headers = []): JWKSet
55
    {
56
        $request = $this->requestFactory->createRequest('GET', $url, $headers);
57
        $response = $this->client->sendRequest($request);
58
59
        if (200 !== $response->getStatusCode()) {
60
            throw new \HttpRuntimeException('Unable to get the key set.', $response->getStatusCode());
61
        }
62
63
        $data = json_decode($response->getBody()->getContents(), true);
64
        if (!is_array($data)) {
65
            throw new \InvalidArgumentException('Invalid content.');
66
        }
67
68
        $keys = [];
69
        foreach ($data as $kid => $cert) {
70
            $jwk = KeyConverter::loadKeyFromCertificate($cert);
71
            if (is_string($kid)) {
72
                $jwk['kid'] = $kid;
73
                $keys[$kid] = JWK::create($jwk);
74
            } else {
75
                $keys[] = JWK::create($jwk);
76
            }
77
        }
78
79
        return JWKSet::createFromKeys($keys);
80
    }
81
}
82