GenerateFileCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\DandomainConsignmentBundle\Command;
6
7
use Loevgaard\DandomainConsignment\Entity\Generated\ReportInterface;
8
use Loevgaard\DandomainConsignment\Repository\ReportRepository;
9
use Loevgaard\DandomainConsignmentBundle\ConsignmentService\ConsignmentServiceCollection;
10
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentConsignmentServiceException;
11
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentReportException;
12
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Logger\ConsoleLogger;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class GenerateFileCommand extends ContainerAwareCommand
19
{
20
    /**
21
     * @var ReportRepository
22
     */
23
    private $reportRepository;
24
25
    /**
26
     * @var ConsignmentServiceCollection
27
     */
28
    private $consignmentServiceCollection;
29
30
    public function __construct(ReportRepository $reportRepository, ConsignmentServiceCollection $consignmentServiceCollection)
31
    {
32
        parent::__construct();
33
34
        $this->reportRepository = $reportRepository;
35
        $this->consignmentServiceCollection = $consignmentServiceCollection;
36
    }
37
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('loevgaard:dandomain-consignment:generate-file')
42
            ->setDescription('Generates a report file from an existing report')
43
            ->addArgument('report', InputArgument::REQUIRED, 'The report to generate')
44
        ;
45
    }
46
47
    /**
48
     * @param InputInterface  $input
49
     * @param OutputInterface $output
50
     *
51
     * @return int|null|void
52
     *
53
     * @throws NonExistentReportException
54
     * @throws NonExistentConsignmentServiceException
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $reportId = (int) $input->getArgument('report');
59
60
        /** @var ReportInterface $report */
61
        $report = $this->reportRepository->find($reportId);
62
63
        if (!$report) {
64
            throw new NonExistentReportException('The report with id '.$reportId.' does not exist');
65
        }
66
67
        // find the consignment service
68
        $consignmentService = $this->consignmentServiceCollection->findConsignmentService($report->getManufacturer());
69
        $consignmentService->setLogger(new ConsoleLogger($output));
70
71
        // generate the report
72
        $reportFile = $consignmentService->generateReportFile($report);
73
74
        $output->writeln('Report saved to '.$reportFile->getPathname());
75
    }
76
}
77