HashGenerator::generateHash()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 33
rs 9.3888
cc 5
nc 5
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\Client\FirstData\Generator;
9
10
use Generated\Shared\Transfer\FirstDataHashRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...DataHashRequestTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SprykerEco\Client\FirstData\Exception\InvalidArgumentsNumberProvided;
12
use SprykerEco\Client\FirstData\FirstDataConfig;
13
14
class HashGenerator implements HashGeneratorInterface
15
{
16
    protected const SEPARATOR = '|';
17
18
    /**
19
     * @var \SprykerEco\Client\FirstData\FirstDataConfig
20
     */
21
    protected $firstDataConfig;
22
23
    /**
24
     * @param \SprykerEco\Client\FirstData\FirstDataConfig $firstDataConfig
25
     */
26
    public function __construct(FirstDataConfig $firstDataConfig)
27
    {
28
        $this->firstDataConfig = $firstDataConfig;
29
    }
30
31
    /**
32
     * @param \Generated\Shared\Transfer\FirstDataHashRequestTransfer $firstDataHashRequestTransfer
33
     *
34
     * @throws \SprykerEco\Client\FirstData\Exception\InvalidArgumentsNumberProvided
35
     *
36
     * @return string
37
     */
38
    public function generateHash(FirstDataHashRequestTransfer $firstDataHashRequestTransfer): string
39
    {
40
        $firstDataHashRequestParams = $firstDataHashRequestTransfer->toArray();
41
42
        ksort($firstDataHashRequestParams);
43
44
        $firstDataHashRequestParams = array_filter($firstDataHashRequestParams, function ($arrayValue) {
45
            return $arrayValue !== null;
46
        });
47
48
        if (empty($firstDataHashRequestParams)) {
49
            throw new InvalidArgumentsNumberProvided('At least 1 param must be provided.');
50
        }
51
52
        $params = [];
53
54
        foreach ($firstDataHashRequestParams as $param) {
55
            if (is_bool($param)) {
56
                $param = $param ? 'true' : 'false';
57
            }
58
59
            $params[] = $param;
60
        }
61
62
        $paramString = implode(static::SEPARATOR, $params);
63
64
        $hashExtended = hash_hmac(
65
            $this->firstDataConfig->getHashAlgo(),
66
            $paramString,
67
            $this->firstDataConfig->getSharedSecret()
68
        );
69
70
        return base64_encode($hashExtended);
71
    }
72
}
73