Completed
Pull Request — master (#470)
by Richard
10:42
created

KeyFactory::build()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
ccs 5
cts 6
cp 0.8333
cc 3
eloc 6
nc 2
nop 1
crap 3.0416
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf\Jwt;
13
14
use Firebase\JWT\JWT;
15
use Xmf\Key\Basic;
16
use Xmf\Key\FileStorage;
17
18
/**
19
 * Build a key to be used for JSON Web Token processing
20
 *
21
 * @category  Xmf\Jwt\KeyFactory
22
 * @package   Xmf
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2016 XOOPS Project (http://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @version   Release: 1.0
27
 * @link      http://xoops.org
28
 */
29
class KeyFactory
30
{
31
    /**
32
     * Create a Key object for JWT use based on default choices. If the key has not been
33
     * established, create it.
34
     *
35
     * @param string $keyName name of the key
36
     *
37
     * @return Basic
38
     */
39 1
    public static function build($keyName)
40
    {
41 1
        if (empty($keyName) || !is_string($keyName)) {
42
            throw new \InvalidArgumentException('keyName must be a non-empty string');
43
        }
44 1
        $key = new Basic(new FileStorage(), $keyName);
45 1
        $key->create(); // will automatically skip if key has already been generated
46 1
        return $key;
47
    }
48
}
49