Completed
Push — master ( 16bc05...356550 )
by sebastian
03:03
created

src/jwe/impl/JWEJOSEHeaderFactory.php (2 issues)

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
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
15
namespace jwe\impl;
16
17
use jwe\IJWEJOSEHeader;
18
use jwe\RegisteredJWEJOSEHeaderNames;
19
20
/**
21
 * Class JWEJOSEHeaderFactory
22
 * @package jwe\impl
23
 */
24 View Code Duplication
final class JWEJOSEHeaderFactory {
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...
25
26
    static protected function getProductClass(){
0 ignored issues
show
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
27
        return  '\jwe\impl\JWEJOSEHeader';
28
    }
29
30
    /**
31
     * @param array $raw_headers
32
     * @return IJWEJOSEHeader
33
     */
34
    public static function build(array $raw_headers){
35
36
        $args = array();
37
38
        foreach(RegisteredJWEJOSEHeaderNames::$registered_basic_headers_set as $header_name){
39
            $value = isset($raw_headers[$header_name]) ? $raw_headers[$header_name] : null;
40
            $type  = @RegisteredJWEJOSEHeaderNames::$registered_basic_headers_set_types[$header_name];
41
            if(!is_null($value))
42
            {
43
                if(is_null($type)) continue;
44
                $class    = new \ReflectionClass($type);
45
                $value    = $class->newInstanceArgs(array($value));
46
            }
47
            array_push($args, $value);
48
            unset($raw_headers[$header_name]);
49
        }
50
51
52
        $class        = new \ReflectionClass(self::getProductClass());
53
        $basic_header = $class->newInstanceArgs($args);
54
55
        // unregistered headers
56
57
        foreach($raw_headers as $k => $v){
58
            $basic_header->addHeader(new JOSEHeaderParam($k, new JsonValue($v)));
59
        }
60
61
        return $basic_header;
62
    }
63
}