Completed
Push — master ( f3ba78...aeb154 )
by Kai
03:26 queued 01:17
created

Hash::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php namespace SimpleHash;
2
3
use SimpleHash\Exception\SimpleHashException;
4
5
/**
6
 * Hash facade
7
 *
8
 * @package    SimpleHash
9
 * @author     Kai Hempel <[email protected]>
10
 * @copyright  2016 Kai Hempel <[email protected]>
11
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
12
 * @link       https://www.kuweh.de/
13
 * @since      Class available since Release 1.0.0
14
 */
15
class Hash
16
{
17
    /**
18
     * Facade interface method
19
     *
20
     * @param   string      $name
21
     * @param   array       $arguments
22
     * @return  \SimpleHash\Container\HashContainer
23
     * @throws  \SimpleHash\Exception\SimpleHashException
24
     */
25 2
    public static function __callStatic($name, $arguments)
26
    {
27 2
        $factory = new HashFactory(self::getCalclatorParams($arguments));
28
29
        // Check if the requested algorithm is implemented
30
31 2
        if (! $factory->hasCalculator($name)) {
32 1
            throw SimpleHashException::make('Algorithm "' . $name . '" is not implemented yet!');
33
        }
34
35
        // Build hash container
36
37 1
        $getterMethod = self::getFactoryMethodName($name);
38 1
        return $factory->$getterMethod(self::getPlainTextString($arguments));
39
    }
40
41
    /**
42
     * Returns the plain text string from the arguments
43
     *
44
     * @param   array       $arguments
45
     * @return  string
46
     */
47 1
    private static function getPlainTextString(array $arguments)
48
    {
49 1
        return (empty($arguments[0])) ? '' : (string)$arguments[0];
50
    }
51
52
    /**
53
     * Returns the optional calculator parameter from the arguments
54
     *
55
     * @param   array       $arguments
56
     * @return  array
57
     */
58 2
    private static function getCalclatorParams(array $arguments)
59
    {
60 2
        return (! empty($arguments[1]) && is_array($arguments[1])) ? $arguments[1] : [];
61
    }
62
63
    /**
64
     * Returns the factory hash getter name
65
     *
66
     * @param   string      $name
67
     * @return  string
68
     */
69 1
    private static function getFactoryMethodName($name)
70
    {
71 1
        return 'get' . ucfirst(strtolower($name)) . 'Hash';
72
    }
73
}