Completed
Push — master ( bb3be0...c2b8bb )
by Joachim
08:36
created

ReportCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 9.6 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 55%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 17
c 3
b 1
f 0
lcom 2
cbo 11
dl 12
loc 125
ccs 33
cts 60
cp 0.55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 13 1
D execute() 12 81 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @return int|null|void
58
     * @throws ConsignmentNotEnabledException
59
     * @throws InvalidDateFormatException
60
     * @throws NonExistentConsignmentServiceException
61
     * @throws NonExistentManufacturerException
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65 3
        // fetch arguments and options
66
        $manufacturer = $input->getArgument('manufacturer');
67
        $start = $input->getOption('start');
68 3
        $end = $input->getOption('end');
69 3
        $doNotDeliver = boolval($input->getOption('do-not-deliver'));
70 3
        $doNotUpdateLastStockMovement = boolval($input->getOption('do-not-update-last-stock-movement'));
71 3
        $doNotUseLastStockMovement = boolval($input->getOption('do-not-use-last-stock-movement'));
72 3
73 3
        // validate dates
74 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...
75
            $start = \DateTime::createFromFormat('Y-m-d', $start);
76 3
            if (false === $start) {
77 1
                throw new InvalidDateFormatException('The format for start is invalid');
78 1
            }
79 1
        }
80
81 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...
82
            $end = \DateTime::createFromFormat('Y-m-d', $end);
83 2
            if (false === $end) {
84 1
                throw new InvalidDateFormatException('The format for end is invalid');
85 1
            }
86 1
        }
87
88
        // find manufacturer
89
        $manufacturer = $this->manufacturerRepository->findOneByExternalId($manufacturer);
90
91 1
        if (!$manufacturer) {
92
            throw new NonExistentManufacturerException('The manufacturer does not exist');
93 1
        }
94 1
95
        // check if the manufacturer is enabled for consignment
96
        if (!$manufacturer->isConsignment()) {
97
            throw new ConsignmentNotEnabledException('Consignment is not enabled for the manufacturer');
98
        }
99
100
        if ($input->isInteractive()) {
101
            // output config
102
            $table = new Table($output);
103
            $table
104
                ->setHeaders(['Option', 'Value'])
105
                ->setRows([
106
                    ['Manufacturer', $manufacturer->getName()],
107
                    ['Start date', $start ? $start->format('Y-m-d') : 'None'],
108
                    ['End date', $end ? $end->format('Y-m-d') : 'None'],
109
                    ['Deliver?', $doNotDeliver ? 'No' : 'Yes'],
110
                    ['Update last stock movement?', $doNotUpdateLastStockMovement ? 'No' : 'Yes'],
111
                    ['Use last stock movement?', $doNotUseLastStockMovement ? 'No' : 'Yes'],
112
                ]);
113
            $table->render();
114
115
            // confirm config
116
            $helper = $this->getHelper('question');
117
            $question = new ConfirmationQuestion('Continue with this config? ', false);
118
119
            if (!$helper->ask($input, $output, $question)) {
120
                return;
121
            }
122
        }
123
124
        // find the consignment service
125
        $consignmentService = $this->consignmentServiceCollection->findConsignmentService($manufacturer);
126
        $consignmentService->setLogger(new ConsoleLogger($output));
127
128
        // generate the report
129
        $report = $consignmentService->generateReport([
130
            'update_last_stock_movement' => !$doNotUpdateLastStockMovement,
131
            'use_last_stock_movement' => !$doNotUseLastStockMovement,
132
            'start_date' => $start,
133
            'end_date' => $end,
134
        ]);
135
136
        // generate report file because we want to generate the file no matter if the $deliver option is set
137
        $consignmentService->generateReportFile($report);
138
139
        // deliver report
140
        if (!$doNotDeliver) {
141
            $consignmentService->deliverReport($report);
142
        }
143
    }
144
}
145