Passed
Push — feature/eco-2295/eco-2344-crea... ( 12129e...3ef933 )
by Aleksey
01:12
created

MacHashCalculator::removeDelimiters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Service\CrefoPayApi\HashCalculator;
9
10
use SprykerEco\Service\CrefoPayApi\CrefoPayApiConfig;
11
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
78
    {
79
        return $this->config->getPrivateKey();
80
    }
81
}
82