ReportCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\DandomainConsignmentBundle\Command;
6
7
use Loevgaard\DandomainConsignmentBundle\ConsignmentService\ConsignmentServiceCollection;
8
use Loevgaard\DandomainConsignmentBundle\Exception\ConsignmentNotEnabledException;
9
use Loevgaard\DandomainConsignmentBundle\Exception\InvalidDateFormatException;
10
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentConsignmentServiceException;
11
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentManufacturerException;
12
use Loevgaard\DandomainFoundation\Repository\ManufacturerRepository;
13
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Logger\ConsoleLogger;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Question\ConfirmationQuestion;
21
22
class ReportCommand extends ContainerAwareCommand
23
{
24
    /**
25
     * @var ManufacturerRepository
26
     */
27
    protected $manufacturerRepository;
28
29
    /**
30
     * @var ConsignmentServiceCollection
31
     */
32
    protected $consignmentServiceCollection;
33
34 3
    public function __construct(ManufacturerRepository $manufacturerRepository, ConsignmentServiceCollection $consignmentServiceCollection)
35
    {
36 3
        $this->manufacturerRepository = $manufacturerRepository;
37 3
        $this->consignmentServiceCollection = $consignmentServiceCollection;
38
39 3
        parent::__construct();
40 3
    }
41
42 3
    protected function configure()
43
    {
44
        $this
45 3
            ->setName('loevgaard:dandomain-consignment:report')
46 3
            ->setDescription('Generates a report and optionally delivers it to the given manufacturer')
47 3
            ->addArgument('manufacturer', InputArgument::REQUIRED, 'The manufacturer to generate a report for. Use the id from Dandomain')
48 3
            ->addOption('start', null, InputOption::VALUE_REQUIRED, 'The start date in the format `YYYY-MM-DD`')
49 3
            ->addOption('end', null, InputOption::VALUE_REQUIRED, 'The end date in the format `YYYY-MM-DD`')
50 3
            ->addOption('do-not-deliver', null, InputOption::VALUE_NONE, 'If set the command will NOT deliver the report')
51 3
            ->addOption('do-not-update-last-stock-movement', null, InputOption::VALUE_NONE, 'If set, the command will NOT update the last stock movement property for the manufacturer')
52 3
            ->addOption('do-not-use-last-stock-movement', null, InputOption::VALUE_NONE, 'If set, the command will NOT use the last stock movement as the starting point when generating the report')
53
        ;
54 3
    }
55
56
    /**
57
     * @param InputInterface  $input
58
     * @param OutputInterface $output
59
     *
60
     * @return int|null|void
61
     *
62
     * @throws ConsignmentNotEnabledException
63
     * @throws InvalidDateFormatException
64
     * @throws NonExistentConsignmentServiceException
65
     * @throws NonExistentManufacturerException
66
     */
67 3
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69
        // fetch arguments and options
70 3
        $manufacturer = $input->getArgument('manufacturer');
71 3
        $start = $input->getOption('start');
72 3
        $end = $input->getOption('end');
73 3
        $doNotDeliver = boolval($input->getOption('do-not-deliver'));
74 3
        $doNotUpdateLastStockMovement = boolval($input->getOption('do-not-update-last-stock-movement'));
75 3
        $doNotUseLastStockMovement = boolval($input->getOption('do-not-use-last-stock-movement'));
76
77
        // validate dates
78 3 View Code Duplication
        if ($start) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79 1
            $start = \DateTime::createFromFormat('Y-m-d', $start);
80 1
            if (false === $start) {
81 1
                throw new InvalidDateFormatException('The format for start is invalid');
82
            }
83
        }
84
85 2 View Code Duplication
        if ($end) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86 1
            $end = \DateTime::createFromFormat('Y-m-d', $end);
87 1
            if (false === $end) {
88 1
                throw new InvalidDateFormatException('The format for end is invalid');
89
            }
90
        }
91
92
        // find manufacturer
93 1
        $manufacturer = $this->manufacturerRepository->findOneByExternalId($manufacturer);
94
95 1
        if (!$manufacturer) {
96 1
            throw new NonExistentManufacturerException('The manufacturer does not exist');
97
        }
98
99
        // check if the manufacturer is enabled for consignment
100
        if (!$manufacturer->isConsignment()) {
101
            throw new ConsignmentNotEnabledException('Consignment is not enabled for the manufacturer');
102
        }
103
104
        if ($input->isInteractive()) {
105
            // output config
106
            $table = new Table($output);
107
            $table
108
                ->setHeaders(['Option', 'Value'])
109
                ->setRows([
110
                    ['Manufacturer', $manufacturer->getName()],
111
                    ['Start date', $start ? $start->format('Y-m-d') : 'None'],
112
                    ['End date', $end ? $end->format('Y-m-d') : 'None'],
113
                    ['Deliver?', $doNotDeliver ? 'No' : 'Yes'],
114
                    ['Update last stock movement?', $doNotUpdateLastStockMovement ? 'No' : 'Yes'],
115
                    ['Use last stock movement?', $doNotUseLastStockMovement ? 'No' : 'Yes'],
116
                ]);
117
            $table->render();
118
119
            // confirm config
120
            $helper = $this->getHelper('question');
121
            $question = new ConfirmationQuestion('Continue with this config? ', false);
122
123
            if (!$helper->ask($input, $output, $question)) {
124
                return;
125
            }
126
        }
127
128
        // find the consignment service
129
        $consignmentService = $this->consignmentServiceCollection->findConsignmentService($manufacturer);
130
        $consignmentService->setLogger(new ConsoleLogger($output));
131
132
        $options = [
133
            'update_last_stock_movement' => !$doNotUpdateLastStockMovement,
134
            'use_last_stock_movement' => !$doNotUseLastStockMovement,
135
        ];
136
137
        if ($start) {
138
            $options['start_date'] = $start;
139
        }
140
141
        if ($end) {
142
            $options['end_date'] = $end;
143
        }
144
145
        // generate the report
146
        $report = $consignmentService->generateReport($options);
147
148
        // generate report file because we want to generate the file no matter if the $deliver option is set
149
        $consignmentService->generateReportFile($report);
150
151
        // deliver report
152
        if (!$doNotDeliver) {
153
            $consignmentService->deliverReport($report);
154
        }
155
    }
156
}
157