Completed
Push — master ( 6c40ed...665ea8 )
by sebastian
01:26
created

ContentEncryptionAlgorithms_Registry.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace jwa\cryptographic_algorithms;
2
/**
3
 * Copyright 2015 OpenStack Foundation
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 * Unless required by applicable law or agreed to in writing, software
9
 * distributed under the License is distributed on an "AS IS" BASIS,
10
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
 * See the License for the specific language governing permissions and
12
 * limitations under the License.
13
 **/
14
use jwa\JSONWebSignatureAndEncryptionAlgorithms;
15
use jwa\cryptographic_algorithms\content_encryption\AES_CBC_HS\sha2\A128CBCHS256_Algorithm;
16
use jwa\cryptographic_algorithms\content_encryption\AES_CBC_HS\sha2\A192CBCHS384_Algorithm;
17
use jwa\cryptographic_algorithms\content_encryption\AES_CBC_HS\sha2\A256CBCHS512_Algorithm;
18
use jwa\cryptographic_algorithms\content_encryption\ContentEncryptionAlgorithm;
19
/**
20
 * Class ContentEncryptionAlgorithms_Registry
21
 * @package jwa\cryptographic_algorithms
22
 */
23 View Code Duplication
final class ContentEncryptionAlgorithms_Registry
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
26
    /**
27
     * @var ContentEncryptionAlgorithms_Registry
28
     */
29
    private static $instance;
30
31
    private $algorithms = array();
32
33
    private function __construct()
34
    {
35
36
        $this->algorithms[JSONWebSignatureAndEncryptionAlgorithms::A128CBC_HS256] = new A128CBCHS256_Algorithm;
37
        $this->algorithms[JSONWebSignatureAndEncryptionAlgorithms::A192CBC_HS384] = new A192CBCHS384_Algorithm;
38
        $this->algorithms[JSONWebSignatureAndEncryptionAlgorithms::A256CBC_HS512] = new A256CBCHS512_Algorithm;
39
    }
40
41
    private function __clone() {}
42
43
    /**
44
     * @return ContentEncryptionAlgorithms_Registry
45
     */
46
    public static function getInstance() {
47
        if (!is_object(self::$instance)) {
48
            self::$instance = new ContentEncryptionAlgorithms_Registry();
49
        }
50
        return self::$instance;
51
    }
52
53
    /**
54
     * @param string $alg
55
     * @return bool
56
     */
57
    public function isSupported($alg) {
58
        return array_key_exists($alg, $this->algorithms);
59
    }
60
61
    /**
62
     * @param $alg
63
     * @return null|ContentEncryptionAlgorithm
64
     */
65
    public function get($alg) {
66
        if (!$this->isSupported($alg)) return null;
67
        return $this->algorithms[$alg];
68
    }
69
}