Failed Conditions
Push — v7 ( 722dd5...1e67af )
by Florent
03:17
created

X5UFactory::loadFromUrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
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\Converter\JsonConverterInterface;
19
use Jose\Component\Core\JWK;
20
use Jose\Component\Core\JWKSet;
21
use Jose\Component\KeyManagement\KeyConverter\KeyConverter;
22
23
final class X5UFactory extends UrlKeySetFactory
24
{
25
    private $jsonConverter;
26
27
    /**
28
     * X5UFactory constructor.
29
     *
30
     * @param JsonConverterInterface $jsonConverter
31
     * @param HttpClient $client
32
     * @param RequestFactory $requestFactory
33
     */
34
    public function __construct(JsonConverterInterface $jsonConverter, HttpClient $client, RequestFactory $requestFactory)
35
    {
36
        $this->jsonConverter = $jsonConverter;
37
        parent::__construct($client, $requestFactory);
38
    }
39
40
    /**
41
     * @param string $url
42
     * @param array  $headers
43
     *
44
     * @throws \HttpRuntimeException
45
     *
46
     * @return JWKSet
47
     */
48
    public function loadFromUrl(string $url, array $headers = []): JWKSet
49
    {
50
        $content = $this->getContent($url, $headers);
51
        $data = $this->jsonConverter->decode($content);
52
        if (!is_array($data)) {
53
            throw new \InvalidArgumentException('Invalid content.');
54
        }
55
56
        $keys = [];
57
        foreach ($data as $kid => $cert) {
58
            $jwk = KeyConverter::loadKeyFromCertificate($cert);
59
            if (is_string($kid)) {
60
                $jwk['kid'] = $kid;
61
                $keys[$kid] = JWK::create($jwk);
62
            } else {
63
                $keys[] = JWK::create($jwk);
64
            }
65
        }
66
67
        return JWKSet::createFromKeys($keys);
68
    }
69
}
70