JWTOptions::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 4
eloc 9
c 3
b 1
f 1
nc 5
nop 1
dl 0
loc 19
rs 9.9666
1
<?php
2
3
namespace UAPAY;
4
5
use UAPAY\Exception;
6
7
class JWTOptions
8
{
9
    /**
10
     *      @var array
11
     */
12
    protected $jwt = array(
13
        'using'         => false,
14
        'UAPAY_pubkey'  => '',
15
        'our_privkey'   => '',
16
        'key_type'      => '',
17
        'algorithm'     => '',
18
    );
19
20
    /**
21
     *      Constructor
22
     *
23
     *      @param array $options array of options
24
     *      @throws Exception\Data
25
     */
26
    public function __construct($options)
27
    {
28
        if (isset($options['jwt']))
29
        {
30
            $this->is_valid_using($options);
31
            $this->is_present_option($options, 'UAPAY_pubkey');
32
            $this->is_present_option($options, 'our_privkey');
33
34
            // todo replace with:
35
            //      $this->is_present_option($options, 'algorithm');
36
            //      in next major version
37
            if (empty($options['jwt']['algorithm'])) {
38
                $options['jwt']['algorithm'] = 'RS512';
39
            }
40
            if (empty($options['jwt']['key_type'])) {
41
                $options['jwt']['key_type'] = Key::KEYS_IN_FILES;
42
            }
43
44
            $this->jwt = $options['jwt'];
45
        }
46
    }
47
48
    /**
49
     *      Check is valid 'using' option
50
     *
51
     *      @param array $options
52
     *      @throws Exception\Data
53
     */
54
    protected function is_valid_using($options)
55
    {
56
        $this->is_present_option($options, 'using');
57
        if ( ! is_bool($options['jwt']['using']))
58
        {
59
            throw new Exception\Data('parameter jwt/using is incorrect');
60
        }
61
    }
62
63
    /**
64
     *      Check is present option
65
     *
66
     *      @param array $options
67
     *      @param string $name
68
     *      @throws Exception\Data
69
     */
70
    protected function is_present_option($options, $name)
71
    {
72
        if ( ! isset($options['jwt'][$name]))
73
        {
74
            throw new Exception\Data('parameter jwt/'.$name.' is not specified');
75
        }
76
    }
77
78
    /**
79
     *      Get jwt options
80
     *
81
     *      @return array
82
     */
83
    public function get()
84
    {
85
        return $this->jwt;
86
    }
87
}
88