Completed
Push — master ( 668fd8...cdc0a6 )
by Florent
12:11
created

JWAManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 66
wmc 11
lcom 1
cbo 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isAlgorithmSupported() 0 4 1
A getAlgorithms() 0 4 1
A listAlgorithms() 0 4 1
A getAlgorithm() 0 4 2
A addAlgorithm() 0 6 2
A removeAlgorithm() 0 13 4
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 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\Algorithm;
13
14
/**
15
 * Class JWAManager.
16
 */
17
final class JWAManager implements JWAManagerInterface
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $algorithms = [];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function isAlgorithmSupported($algorithm)
28
    {
29
        return null !== $this->getAlgorithm($algorithm);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getAlgorithms()
36
    {
37
        return $this->algorithms;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function listAlgorithms()
44
    {
45
        return array_keys($this->getAlgorithms());
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getAlgorithm($algorithm)
52
    {
53
        return array_key_exists($algorithm, $this->algorithms) ? $this->algorithms[$algorithm] : null;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function addAlgorithm(JWAInterface $algorithm)
60
    {
61
        if (!$this->isAlgorithmSupported($algorithm->getAlgorithmName())) {
62
            $this->algorithms[$algorithm->getAlgorithmName()] = $algorithm;
63
        }
64
    }
65
66
    /**
67
     * [@inheritdoc}.
68
     */
69
    public function removeAlgorithm($algorithm)
70
    {
71
        if ($algorithm instanceof JWAInterface) {
72
            $name = $algorithm->getAlgorithmName();
73
        } elseif (is_string($algorithm)) {
74
            $name = $algorithm;
75
        } else {
76
            throw new \InvalidArgumentException('Argument must be a string or a JWAInterface object.');
77
        }
78
        if (array_key_exists($name, $this->algorithms)) {
79
            unset($this->algorithms[$name]);
80
        }
81
    }
82
}
83