Completed
Pull Request — master (#146)
by Hannes
02:12
created

ExportCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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-17 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Console;
24
25
use byrokrat\giroapp\DependencyInjection\DispatcherProperty;
26
use byrokrat\giroapp\DependencyInjection\DonorMapperProperty;
27
use byrokrat\giroapp\Events;
28
use byrokrat\giroapp\Event\DonorEvent;
29
use byrokrat\giroapp\Event\FileEvent;
30
use byrokrat\giroapp\State\StatePool;
31
use byrokrat\giroapp\Utils\File;
32
use byrokrat\autogiro\Writer\Writer;
33
34
/**
35
 * Command to create autogiro files
36
 */
37
class ExportCommand implements CommandInterface
38
{
39
    use DonorMapperProperty, DispatcherProperty;
40
41
    /**
42
     * @var Writer
43
     */
44
    private $autogiroWriter;
45
46
    /**
47
     * @var StatePool
48
     */
49
    private $statePool;
50
51
    public function __construct(Writer $autogiroWriter, StatePool $statePool)
52
    {
53
        $this->autogiroWriter = $autogiroWriter;
54
        $this->statePool = $statePool;
55
    }
56
57
    public static function configure(CommandWrapper $wrapper): void
58
    {
59
        $wrapper->setName('export');
60
        $wrapper->setDescription('Export a file to autogirot');
61
        $wrapper->setHelp('Create a file with new set of autogiro instructions');
62
    }
63
64
    public function execute(): void
65
    {
66
        $exported = false;
67
68
        foreach ($this->donorMapper->findAll() as $donor) {
69
            if ($donor->getState()->isExportable()) {
70
                $donor->exportToAutogiro($this->autogiroWriter);
71
                $donor->setState($this->statePool->getState($donor->getState()->getNextStateId()));
72
                $this->dispatcher->dispatch(
73
                    Events::DONOR_UPDATED,
74
                    new DonorEvent(
75
                        "Exported mandate <info>{$donor->getMandateKey()}</info>",
76
                        $donor
77
                    )
78
                );
79
                $exported = true;
80
            }
81
        }
82
83
        if ($exported) {
84
            $this->dispatcher->dispatch(
85
                Events::FILE_EXPORTED,
86
                new FileEvent(
87
                    'Generating file to export',
88
                    new File('export', $this->autogiroWriter->getContent())
89
                )
90
            );
91
        }
92
    }
93
}
94