Failed Conditions
Push — v7 ( 137ba9...eb2dfc )
by Florent
03:53
created

ClaimCheckerManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A check() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Checker;
15
16
use Jose\Component\Core\JWTInterface;
17
18
/**
19
 * Class ClaimCheckerManager.
20
 */
21
final class ClaimCheckerManager
22
{
23
    /**
24
     * @var ClaimCheckerInterface[]
25
     */
26
    private $checkers = [];
27
28
    /**
29
     * @param ClaimCheckerInterface $checker
30
     */
31
    public function add(ClaimCheckerInterface $checker)
32
    {
33
        $this->checkers[$checker->supportedClaim()] = $checker;
34
    }
35
36
    /**
37
     * @param JWTInterface $jwt
38
     */
39
    public function check(JWTInterface $jwt)
40
    {
41
        $claims = json_decode($jwt->getPayload(), true);
42
        if (!is_array($claims)) {
43
            throw new \InvalidArgumentException('The payload is does not contain claims.');
44
        }
45
46
        foreach ($this->checkers as $claim => $checker) {
47
            if (array_key_exists($claim, $claims)) {
48
                $checker->checkClaim($claims[$claim]);
49
            }
50
        }
51
    }
52
}
53