1 <?php
2 3 4
5 namespace Team3\PayU\SignatureCalculator;
6
7 use Team3\PayU\Configuration\Credentials\CredentialsInterface;
8 use Team3\PayU\SignatureCalculator\Encoder\Algorithms\AlgorithmInterface;
9 use Team3\PayU\SignatureCalculator\Encoder\EncoderException;
10 use Team3\PayU\SignatureCalculator\Encoder\EncoderInterface;
11
12 13 14 15 16 17
18 class SignatureCalculator implements SignatureCalculatorInterface
19 {
20 21 22
23 private $encoder;
24
25 26 27
28 public function __construct(EncoderInterface $encoder)
29 {
30 $this->encoder = $encoder;
31 }
32
33 34 35 36 37 38 39 40 41 42
43 public function calculate(
44 array $data,
45 CredentialsInterface $credentials,
46 AlgorithmInterface $algorithm
47 ) {
48 $concatenated = '';
49 array_walk_recursive($data, function ($value) use (&$concatenated) {
50 $concatenated .= $value;
51 });
52 $concatenated .= $credentials->getPrivateKey();
53
54 return $this->encode($concatenated, $algorithm);
55 }
56
57 58 59 60 61 62 63 64 65
66 private function encode($data, AlgorithmInterface $algorithm)
67 {
68 try {
69 $encodedData = $this->encoder->encode($data, $algorithm);
70 } catch (EncoderException $exception) {
71 throw new SignatureCalculatorException(
72 $exception->getMessage(),
73 $exception->getCode(),
74 $exception
75 );
76 }
77
78 return $encodedData;
79 }
80 }
81