Completed
Push — master ( 0f6cd0...ce5be5 )
by Florent
41:39 queued 33s
created

ClaimCheckerManagerFactory::getSupportedClaims()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 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\Factory;
13
14
use Jose\ClaimChecker\ClaimCheckerInterface;
15
use Jose\ClaimChecker\ClaimCheckerManager;
16
17
final class ClaimCheckerManagerFactory
18
{
19
    /**
20
     * @param string[] $claims
21
     *
22
     * @return \Jose\ClaimChecker\ClaimCheckerManagerInterface
23
     */
24
    public static function createClaimCheckerManager(array $claims)
25
    {
26
        $claim_checker_manager = new ClaimCheckerManager();
27
28
        foreach ($claims as $key=>$value) {
29
            if ($value instanceof ClaimCheckerInterface) {
0 ignored issues
show
Bug introduced by
The class Jose\ClaimChecker\ClaimCheckerInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
30
                $claim_checker_manager->addClaimChecker($value);
31
            } else {
32
                if (is_string($key)) {
33
                    $class = self::getClaimClass($key);
34
                } else {
35
                    $class = self::getClaimClass($value);
36
                }
37
                $claim_checker_manager->addClaimChecker(new $class($value));
38
            }
39
        }
40
41
        return $claim_checker_manager;
42
    }
43
44
    /**
45
     * @param string $claim
46
     *
47
     * @return bool
48
     */
49
    private static function isClaimSupported($claim)
50
    {
51
        return array_key_exists($claim, self::getSupportedClaims());
52
    }
53
54
    /**
55
     * @param string $claim
56
     *
57
     * @throws \InvalidArgumentException
58
     *
59
     * @return string
60
     */
61
    private static function getClaimClass($claim)
62
    {
63
        if (true === self::isClaimSupported($claim)) {
64
            return self::getSupportedClaims()[$claim];
65
        }
66
        throw new \InvalidArgumentException(sprintf('Claim "%s" is not supported.', $claim));
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    private static function getSupportedClaims()
73
    {
74
        return [
75
            'aud' => '\Jose\ClaimChecker\AudienceChecker',
76
            'exp' => '\Jose\ClaimChecker\ExpirationTimeChecker',
77
            'iat' => '\Jose\ClaimChecker\IssuedAtChecker',
78
            'nbf' => '\Jose\ClaimChecker\NotBeforeChecker',
79
        ];
80
    }
81
}
82