SHA2SignatureCalculator::calculate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Skrill\Signature;
6
7
use Money\Money;
8
use Money\MoneyFormatter;
9
use Skrill\ValueObject\Signature;
10
use Skrill\ValueObject\SecretWord;
11
use Skrill\ValueObject\MerchantID;
12
use Skrill\ValueObject\TransactionID;
13
14
/**
15
 * Class SHA2SignatureCalculator.
16
 */
17
final class SHA2SignatureCalculator implements SignatureCalculator
18
{
19
    /**
20
     * @var SecretWord
21
     */
22
    private $secretWord;
23
24
    /**
25
     * @var
26
     */
27
    private $merchantId;
28
    /**
29
     * @var MoneyFormatter
30
     */
31
    private $moneyFormatter;
32
33
    /**
34
     * @param SecretWord     $secretWord
35
     * @param MerchantID     $merchantId
36
     * @param MoneyFormatter $moneyFormatter
37
     */
38
    public function __construct(SecretWord $secretWord, MerchantID $merchantId, MoneyFormatter $moneyFormatter)
39
    {
40
        $this->secretWord = $secretWord;
41
        $this->merchantId = $merchantId;
42
        $this->moneyFormatter = $moneyFormatter;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function calculate(TransactionID $transactionId, Money $amount, int $status): Signature
49
    {
50
        return new Signature(strtoupper(hash('sha256', implode([
51
            $this->merchantId->getValue(),
52
            $transactionId,
53
            strtoupper(hash('sha256', strval($this->secretWord))),
54
            $this->moneyFormatter->format($amount),
55
            $amount->getCurrency(),
56
            $status,
57
        ]))));
58
    }
59
}
60