Completed
Push — master ( 5cc331...5ba948 )
by Hannes
04:16
created

NewDonorProcessor::createState()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of byrokrat\giroapp.
4
 *
5
 * byrokrat\giroapp is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\giroapp is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-20 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Domain;
24
25
use byrokrat\giroapp\Domain\State\MandateCreated;
26
use byrokrat\giroapp\Domain\State\StateCollection;
27
use byrokrat\giroapp\Utils\SystemClock;
28
use byrokrat\banking\AccountNumber;
29
use byrokrat\id\IdInterface;
30
use Hashids\Hashids;
31
32
class NewDonorProcessor
33
{
34
    private const MANDATE_KEY_LENGTH = 16;
35
36
    /** @var Hashids */
37
    private $hashEngine;
38
39
    /** @var StateCollection */
40
    private $stateCollection;
41
42
    /** @var SystemClock */
43
    private $systemClock;
44
45
    public function __construct(StateCollection $stateCollection, SystemClock $systemClock)
46
    {
47
        $this->stateCollection = $stateCollection;
48
        $this->systemClock = $systemClock;
49
        $this->hashEngine = new Hashids(
50
            '',
51
            self::MANDATE_KEY_LENGTH,
52
            'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
53
        );
54
    }
55
56
    public function processNewDonor(NewDonor $newDonor): Donor
57
    {
58
        return new Donor(
59
            $this->createMandateKey($newDonor->getDonorId(), $newDonor->getAccount()),
60
            $this->stateCollection->getState(MandateCreated::getStateId()),
61
            $newDonor->getMandateSource(),
62
            $newDonor->getPayerNumber(),
63
            $newDonor->getAccount(),
64
            $newDonor->getDonorId(),
65
            '',
66
            new PostalAddress,
67
            '',
68
            '',
69
            $newDonor->getDonationAmount(),
70
            '',
71
            $this->systemClock->getNow(),
72
            $this->systemClock->getNow(),
73
            []
74
        );
75
    }
76
77
    private function createMandateKey(IdInterface $id, AccountNumber $account): string
78
    {
79
        $key = $this->hashEngine->encode($id->format('Ss') . substr($account->get16(), 0, -1));
80
81
        if (strlen($key) != self::MANDATE_KEY_LENGTH) {
82
            throw new \LogicException('Mandate key of wrong key size.');
83
        }
84
85
        return $key;
86
    }
87
}
88