Total Complexity | 6 |
Total Lines | 68 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
12 | class MacHashCalculator implements MacHashCalculatorInterface |
||
13 | { |
||
14 | protected const HASHING_ALGORITHM = 'sha1'; |
||
15 | protected const CHARS_TO_REPLACE = [" ","\t","\s","\r","\n"]; |
||
16 | |||
17 | /** |
||
18 | * @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiConfig |
||
19 | */ |
||
20 | protected $config; |
||
21 | |||
22 | /** |
||
23 | * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiConfig $config |
||
24 | */ |
||
25 | public function __construct(CrefoPayApiConfig $config) |
||
26 | { |
||
27 | $this->config = $config; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param array $data |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function calculateMac(array $data): string |
||
36 | { |
||
37 | $macString = $this->createMacString($data); |
||
38 | |||
39 | return $this->hashValue($macString); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param array $data |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | protected function createMacString(array $data): string |
||
48 | { |
||
49 | ksort($data); |
||
50 | |||
51 | return $this->removeDelimiters(implode('', $data)); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param $value |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | protected function hashValue($value): string |
||
60 | { |
||
61 | return hash_hmac(static::HASHING_ALGORITHM, $value, $this->getPrivateKey()); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $macString |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | protected function removeDelimiters(string $macString): string |
||
70 | { |
||
71 | return str_replace(static::CHARS_TO_REPLACE, '', $macString); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | protected function getPrivateKey(): string |
||
80 | } |
||
81 | } |
||
82 |