Passed
Push — master ( 88fe71...f59922 )
by Hannes
02:55 queued 11s
created

NewDonorProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
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-19 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Model;
24
25
use byrokrat\giroapp\MandateSources;
26
use byrokrat\giroapp\States;
27
use byrokrat\giroapp\State\StateInterface;
28
use byrokrat\giroapp\State\StateCollection;
29
use byrokrat\giroapp\Utils\SystemClock;
30
use byrokrat\banking\AccountNumber;
31
use byrokrat\id\IdInterface;
32
use Hashids\Hashids;
33
34
class NewDonorProcessor
35
{
36
    private const MANDATE_KEY_LENGTH = 16;
37
38
    /** @var Hashids */
39
    private $hashEngine;
40
41
    /** @var StateCollection */
42
    private $stateCollection;
43
44
    /** @var SystemClock */
45
    private $systemClock;
46
47
    public function __construct(StateCollection $stateCollection, SystemClock $systemClock)
48
    {
49
        $this->stateCollection = $stateCollection;
50
        $this->systemClock = $systemClock;
51
        $this->hashEngine = new Hashids(
52
            '',
53
            self::MANDATE_KEY_LENGTH,
54
            'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
55
        );
56
    }
57
58
    public function processNewDonor(NewDonor $newDonor): Donor
59
    {
60
        $state = $this->createState($newDonor->getMandateSource());
61
62
        return new Donor(
63
            $this->createMandateKey($newDonor->getDonorId(), $newDonor->getAccount()),
64
            $state,
65
            $state->getDescription(),
66
            $newDonor->getMandateSource(),
67
            $newDonor->getPayerNumber(),
68
            $newDonor->getAccount(),
69
            $newDonor->getDonorId(),
70
            $newDonor->getName(),
71
            $newDonor->getPostalAddress(),
72
            $newDonor->getEmail(),
73
            $newDonor->getPhone(),
74
            $newDonor->getDonationAmount(),
75
            $newDonor->getComment(),
76
            $this->systemClock->getNow(),
77
            $this->systemClock->getNow(),
78
            $newDonor->getAttributes()
79
        );
80
    }
81
82
    private function createMandateKey(IdInterface $id, AccountNumber $account): string
83
    {
84
        $key = $this->hashEngine->encode($id->format('Ss') . substr($account->get16(), 0, -1));
85
86
        if (strlen($key) != self::MANDATE_KEY_LENGTH) {
87
            throw new \LogicException('Mandate key of wrong key size.');
88
        }
89
90
        return $key;
91
    }
92
93
    private function createState(string $mandateSource): StateInterface
94
    {
95
        switch ($mandateSource) {
96
            case MandateSources::MANDATE_SOURCE_PAPER:
97
            case MandateSources::MANDATE_SOURCE_ONLINE_FORM:
98
                return $this->stateCollection->getState(States::NEW_MANDATE);
99
            case MandateSources::MANDATE_SOURCE_DIGITAL:
100
                return $this->stateCollection->getState(States::NEW_DIGITAL_MANDATE);
101
        }
102
103
        throw new \LogicException("Invalid mandate source: $mandateSource");
104
    }
105
}
106