JWESerializerManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A names() 0 4 1
A serialize() 0 8 2
A unserialize() 0 15 3
A add() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\Encryption\Serializer;
15
16
use InvalidArgumentException;
17
use Jose\Component\Encryption\JWE;
18
19
class JWESerializerManager
20
{
21
    /**
22
     * @var JWESerializer[]
23
     */
24
    private $serializers = [];
25
26
    /**
27
     * @param JWESerializer[] $serializers
28
     */
29
    public function __construct(array $serializers)
30
    {
31
        foreach ($serializers as $serializer) {
32
            $this->add($serializer);
33
        }
34
    }
35
36
    /**
37
     * Return the serializer names supported by the manager.
38
     *
39
     * @return string[]
40
     */
41
    public function names(): array
42
    {
43
        return array_keys($this->serializers);
44
    }
45
46
    /**
47
     * Converts a JWE into a string.
48
     * Throws an exception if none of the serializer was able to convert the input.
49
     *
50
     * @throws InvalidArgumentException if the serializer is not supported
51
     */
52
    public function serialize(string $name, JWE $jws, ?int $recipientIndex = null): string
53
    {
54
        if (!isset($this->serializers[$name])) {
55
            throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
56
        }
57
58
        return $this->serializers[$name]->serialize($jws, $recipientIndex);
59
    }
60
61
    /**
62
     * Loads data and return a JWE object.
63
     * Throws an exception if none of the serializer was able to convert the input.
64
     *
65
     * @param string      $input A string that represents a JWE
66
     * @param null|string $name  the name of the serializer if the input is unserialized
67
     *
68
     * @throws InvalidArgumentException if the input cannot be loaded
69
     */
70
    public function unserialize(string $input, ?string &$name = null): JWE
71
    {
72
        foreach ($this->serializers as $serializer) {
73
            try {
74
                $jws = $serializer->unserialize($input);
75
                $name = $serializer->name();
76
77
                return $jws;
78
            } catch (InvalidArgumentException $e) {
79
                continue;
80
            }
81
        }
82
83
        throw new InvalidArgumentException('Unsupported input.');
84
    }
85
86
    /**
87
     * Adds a serializer to the manager.
88
     */
89
    private function add(JWESerializer $serializer): void
90
    {
91
        $this->serializers[$serializer->name()] = $serializer;
92
    }
93
}
94