Completed
Push — master ( a304c6...47d58a )
by Joachim
13:15
created

ReportCommand::execute()   D

Complexity

Conditions 15
Paths 31

Size

Total Lines 81
Code Lines 46

Duplication

Lines 12
Ratio 14.81 %

Code Coverage

Tests 18
CRAP Score 65.7449

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 81
ccs 18
cts 46
cp 0.3913
rs 4.9974
cc 15
eloc 46
nc 31
nop 2
crap 65.7449

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Loevgaard\DandomainConsignmentBundle\Command;
4
5
use Loevgaard\DandomainConsignmentBundle\ConsignmentService\ConsignmentServiceCollection;
6
use Loevgaard\DandomainConsignmentBundle\Exception\ConsignmentNotEnabledException;
7
use Loevgaard\DandomainConsignmentBundle\Exception\InvalidDateFormatException;
8
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentConsignmentServiceException;
9
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentManufacturerException;
10
use Loevgaard\DandomainFoundation\Repository\ManufacturerRepository;
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Helper\Table;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Logger\ConsoleLogger;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
20
class ReportCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * @var ManufacturerRepository
24
     */
25
    protected $manufacturerRepository;
26
27
    /**
28
     * @var ConsignmentServiceCollection
29
     */
30
    protected $consignmentServiceCollection;
31
32 3
    public function __construct(ManufacturerRepository $manufacturerRepository, ConsignmentServiceCollection $consignmentServiceCollection)
33
    {
34 3
        $this->manufacturerRepository = $manufacturerRepository;
35 3
        $this->consignmentServiceCollection = $consignmentServiceCollection;
36
37 3
        parent::__construct();
38 3
    }
39
40 3
    protected function configure()
41
    {
42
        $this
43 3
            ->setName('loevgaard:dandomain-consignment:report')
44 3
            ->setDescription('Generates a report and optionally delivers it to the given manufacturer')
45 3
            ->addArgument('manufacturer', InputArgument::REQUIRED, 'The manufacturer to generate a report for. Use the id from Dandomain')
46 3
            ->addOption('start', null, InputOption::VALUE_REQUIRED, 'The start date in the format `YYYY-MM-DD`')
47 3
            ->addOption('end', null, InputOption::VALUE_REQUIRED, 'The end date in the format `YYYY-MM-DD`')
48 3
            ->addOption('do-not-deliver', null, InputOption::VALUE_NONE, 'If set the command will NOT deliver the report')
49 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')
50 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')
51
        ;
52 3
    }
53
54
    /**
55
     * @param InputInterface  $input
56
     * @param OutputInterface $output
57
     *
58
     * @return int|null|void
59
     *
60
     * @throws ConsignmentNotEnabledException
61
     * @throws InvalidDateFormatException
62
     * @throws NonExistentConsignmentServiceException
63
     * @throws NonExistentManufacturerException
64
     */
65 3
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        // fetch arguments and options
68 3
        $manufacturer = $input->getArgument('manufacturer');
69 3
        $start = $input->getOption('start');
70 3
        $end = $input->getOption('end');
71 3
        $doNotDeliver = boolval($input->getOption('do-not-deliver'));
72 3
        $doNotUpdateLastStockMovement = boolval($input->getOption('do-not-update-last-stock-movement'));
73 3
        $doNotUseLastStockMovement = boolval($input->getOption('do-not-use-last-stock-movement'));
74
75
        // validate dates
76 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...
77 1
            $start = \DateTime::createFromFormat('Y-m-d', $start);
78 1
            if (false === $start) {
79 1
                throw new InvalidDateFormatException('The format for start is invalid');
80
            }
81
        }
82
83 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...
84 1
            $end = \DateTime::createFromFormat('Y-m-d', $end);
85 1
            if (false === $end) {
86 1
                throw new InvalidDateFormatException('The format for end is invalid');
87
            }
88
        }
89
90
        // find manufacturer
91 1
        $manufacturer = $this->manufacturerRepository->findOneByExternalId($manufacturer);
92
93 1
        if (!$manufacturer) {
94 1
            throw new NonExistentManufacturerException('The manufacturer does not exist');
95
        }
96
97
        // check if the manufacturer is enabled for consignment
98
        if (!$manufacturer->isConsignment()) {
99
            throw new ConsignmentNotEnabledException('Consignment is not enabled for the manufacturer');
100
        }
101
102
        if ($input->isInteractive()) {
103
            // output config
104
            $table = new Table($output);
105
            $table
106
                ->setHeaders(['Option', 'Value'])
107
                ->setRows([
108
                    ['Manufacturer', $manufacturer->getName()],
109
                    ['Start date', $start ? $start->format('Y-m-d') : 'None'],
110
                    ['End date', $end ? $end->format('Y-m-d') : 'None'],
111
                    ['Deliver?', $doNotDeliver ? 'No' : 'Yes'],
112
                    ['Update last stock movement?', $doNotUpdateLastStockMovement ? 'No' : 'Yes'],
113
                    ['Use last stock movement?', $doNotUseLastStockMovement ? 'No' : 'Yes'],
114
                ]);
115
            $table->render();
116
117
            // confirm config
118
            $helper = $this->getHelper('question');
119
            $question = new ConfirmationQuestion('Continue with this config? ', false);
120
121
            if (!$helper->ask($input, $output, $question)) {
122
                return;
123
            }
124
        }
125
126
        // find the consignment service
127
        $consignmentService = $this->consignmentServiceCollection->findConsignmentService($manufacturer);
128
        $consignmentService->setLogger(new ConsoleLogger($output));
129
130
        // generate the report
131
        $report = $consignmentService->generateReport([
132
            'update_last_stock_movement' => !$doNotUpdateLastStockMovement,
133
            'use_last_stock_movement' => !$doNotUseLastStockMovement,
134
            'start_date' => $start,
135
            'end_date' => $end,
136
        ]);
137
138
        // generate report file because we want to generate the file no matter if the $deliver option is set
139
        $consignmentService->generateReportFile($report);
140
141
        // deliver report
142
        if (!$doNotDeliver) {
143
            $consignmentService->deliverReport($report);
144
        }
145
    }
146
}
147