Completed
Push — master ( 89bf6a...4ff99e )
by Joachim
07:05
created

ReportCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 124
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 55.74%

Importance

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