Completed
Push — master ( bcc17f...b261f8 )
by Joachim
11:32
created

ReportCommand   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 122
Duplicated Lines 9.84 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 2
cbo 8
dl 12
loc 122
ccs 0
cts 77
cp 0
rs 10

3 Methods

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

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