HashFactory::initializeCalculator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace SimpleHash;
2
3
use SimpleHash\Container\HashContainer;
4
use SimpleHash\Exception\SimpleHashException;
5
6
/**
7
 * Hash factory
8
 *
9
 * @package    SimpleHash
10
 * @author     Kai Hempel <[email protected]>
11
 * @copyright  2016 Kai Hempel <[email protected]>
12
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
13
 * @link       https://www.kuweh.de/
14
 * @since      Class available since Release 1.0.0
15
 */
16
class HashFactory
17
{
18
    /**
19
     * Calculator parameter
20
     *
21
     * @var array
22
     */
23
    protected $calculatorParams = array();
24
25
    /**
26
     * Constructor
27
     *
28
     * @param   array       $calculatorParams
29
     */
30 12
    public function __construct(array $calculatorParams = [])
31
    {
32 12
        $this->calculatorParams = $calculatorParams;
33 12
    }
34
35
    /**
36
     * Magic factory method
37
     *
38
     * @param   string      $name
39
     * @param   array       $arguments
40
     * @throws  SimpleHashException
41
     */
42 9
    public function __call($name, $arguments)
43
    {
44 9
        if ($this->isCalledGetter($name) === false) {
45 1
            throw SimpleHashException::make('Unsupported method "' . $name . '" called!');
46
        }
47
48
        // Initialize variables
49
50 8
        $plainText = $this->getPlainStringFromArguments($arguments);
51
52
        // Initialize calculator
53
54 8
        $calculatorClass = $this->getCalculatorName($name);
55 8
        if (! $this->calculatorExists($calculatorClass)) {
56 1
            throw SimpleHashException::make('No calclator "' . $calculatorClass . '" exists!');
57
        }
58
59 7
        $calculator = $this->initializeCalculator($calculatorClass);
60
61
        // Initialize hash container with hash string
62
63 7
        $container = new HashContainer();
64 7
        $container->setHashString($calculator->getHash($plainText));
65
66
        // Return hash container
67
68 7
        return $container;
69
    }
70
71
    /**
72
     * Checks if a getter method was called
73
     *
74
     * @param   string      $name
75
     * @return  string
76
     */
77 9
    private function isCalledGetter($name)
78
    {
79 9
        return substr($name, 0, 3) == 'get';
80
    }
81
82
    /**
83
     * Builds the calculator class name string
84
     *
85
     * @param   string      $name
86
     * @return  string
87
     */
88 10
    private function getCalculatorName($name)
89
    {
90 10
        $algorithmName = str_replace(['get', 'Hash'], ['', ''], $name);
91 10
        return '\SimpleHash\Calculator\\' . ucfirst(strtolower($algorithmName)) . 'Calculator';
92
    }
93
94
    /**
95
     * Checks if the given class name exists
96
     *
97
     * @param   string      $calculatorClass
98
     * @return  boolean
99
     */
100 10
    private function calculatorExists($calculatorClass)
101
    {
102 10
        return class_exists($calculatorClass, true);
103
    }
104
105
    /**
106
     * Extracts the string for hashing from the given argument array
107
     *
108
     * @param   array       $arguments
109
     * @return  string
110
     */
111 8
    private function getPlainStringFromArguments($arguments)
112
    {
113 8
        if (empty($arguments) || empty($arguments[0])) {
114 1
            return '';
115
        }
116
117 7
        return (string)$arguments[0];
118
    }
119
120
    /**
121
     * Initialize calculator object
122
     *
123
     * @param   string      $calculatorClass
124
     * @return  \SimpleHash\Calculator\HashCalculatorInterface
125
     */
126 7
    private function initializeCalculator($calculatorClass)
127
    {
128 7
        return new $calculatorClass($this->calculatorParams);
129
    }
130
131
    /**
132
     * CHecks if the calculator class exists
133
     *
134
     * @param   string      $name
135
     * @return  boolean
136
     */
137 7
    public function hasCalculator($name)
138
    {
139 7
        return $this->calculatorExists($this->getCalculatorName($name));
140
    }
141
}
142