Completed
Push — master ( 7d5f81...069e15 )
by Jan
02:05
created

SignatureDataFormatter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 58
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A formatDataForSignature() 0 4 1
C generateMessage() 0 28 8
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Crypto;
4
5
class SignatureDataFormatter
6
{
7
8
	/** @var mixed[] */
9
	private $keysPriority;
10
11
	/**
12
	 * @param mixed[] $keysPriority
13
	 */
14 43
	public function __construct(array $keysPriority)
15
	{
16 43
		$this->keysPriority = $keysPriority;
17 43
	}
18
19
	/**
20
	 * @param mixed[] $data
21
	 * @return string
22
	 */
23 12
	public function formatDataForSignature(array $data): string
24
	{
25 12
		return implode('|', $this->generateMessage($data, $this->keysPriority));
26
	}
27
28
	/**
29
	 * @param mixed[] $data
30
	 * @param mixed[] $keys
31
	 * @return mixed[]
32
	 */
33 12
	private function generateMessage(array $data, array $keys): array
34
	{
35 12
		$message = [];
36
37 12
		foreach ($keys as $key => $values) {
38 8
			if (is_int($key)) {
39 3
				foreach ($data as $items) {
40 3
					$message = array_merge($message, $this->generateMessage($items, $values));
41
				}
42 3
				continue;
43
			}
44 8
			if (!array_key_exists($key, $data)) {
45 4
				continue;
46
			}
47
48 8
			if (is_array($values)) {
49 4
				$message = array_merge($message, $this->generateMessage($data[$key], $values));
50
			} else {
51 8
				if (is_bool($data[$key])) {
52
					$message[] = $data[$key] ? 'true' : 'false';
53
				} else {
54 8
					$message[] = $data[$key];
55
				}
56
			}
57
		}
58
59 12
		return $message;
60
	}
61
62
}
63