Completed
Push — master ( 24821b...f3ba78 )
by Kai
02:15
created

HashFactory::hasCalculator()   A

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 1
Bugs 0 Features 0
Metric Value
c 1
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 6
    public function __construct(array $calculatorParams = [])
31
    {
32 6
        $this->calculatorParams = $calculatorParams;
33 6
    }
34
35
    /**
36
     * Magic factory method
37
     *
38
     * @param   string      $name
39
     * @param   array       $arguments
40
     * @throws  SimpleHashException
41
     */
42 4
    public function __call($name, $arguments)
43
    {
44 4
        if ($this->isCalledGetter($name) === false) {
45 1
            throw SimpleHashException::make('Unsupported method "' . $name . '" called!');
46
        }
47
48
        // Intialize variables
49
50 3
        $plainText          = $this->getPlainStringFromArguments($arguments);
51
52
        // Initialize calculator
53
54 3
        $calculatorClass = $this->getCalculatorName($name);
55 3
        if (! $this->calculatorExists($calculatorClass)) {
56 1
            throw SimpleHashException::make('No calclator "' . $calculatorClass . '" exists!');
57
        }
58
59 2
        $calculator = $this->initializeCalculator($calculatorClass);
60
61
        // Initialize hash container with hash string
62
63 2
        $container = new HashContainer();
64 2
        $container->setHashString($calculator->getHash($plainText));
65
66
        // Return hash container
67
68 2
        return $container;
69
    }
70
71
    /**
72
     * Checks if a getter method was called
73
     *
74
     * @param   string      $name
75
     * @return  string
76
     */
77 4
    private function isCalledGetter($name)
78
    {
79 4
        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 4
    private function getCalculatorName($name)
89
    {
90 4
        $algorithmName = str_replace(['get', 'Hash'], ['', ''], $name);
91 4
        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 4
    private function calculatorExists($calculatorClass)
101
    {
102 4
        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 3
    private function getPlainStringFromArguments($arguments)
112
    {
113 3
        if (empty($arguments) || empty($arguments[0])) {
114 1
            return '';
115
        }
116
117 2
        return (string)$arguments[0];
118
    }
119
120
    /**
121
     * Initialize calculator object
122
     *
123
     * @param   string      $calculatorClass
124
     * @param   array       $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
125
     * @return  \SimpleHash\Calculator\HashCalculatorInterface
126
     */
127 2
    private function initializeCalculator($calculatorClass)
128
    {
129 2
        return new $calculatorClass($this->calculatorParams);
130
    }
131
132
    /**
133
     * CHecks if the calculator class exists
134
     *
135
     * @param   string      $name
136
     * @return  boolean
137
     */
138 1
    public function hasCalculator($name)
139
    {
140 1
        return $this->calculatorExists($this->getCalculatorName($name));
141
    }
142
}
143