Completed
Push — master ( 2ce899...dff293 )
by Bernhard
01:51
created

Show::execute()   C

Complexity

Conditions 9
Paths 44

Size

Total Lines 64
Code Lines 31

Duplication

Lines 14
Ratio 21.88 %

Importance

Changes 0
Metric Value
dl 14
loc 64
rs 6.5449
c 0
b 0
f 0
cc 9
eloc 31
nc 44
nop 2

How to fix   Long Method   

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
/**
4
 * \Wicked\Timely\Command\Show
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2016 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\Command;
21
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Wicked\Timely\Storage\StorageFactory;
28
use Wicked\Timely\Formatter\FormatterFactory;
29
30
/**
31
 * Class for the "show" command. Command used to print all tracked times
32
 *
33
 * @author    Bernhard Wick <[email protected]>
34
 * @copyright 2016 Bernhard Wick
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/wick-ed/timely
37
 */
38
class Show extends Command
39
{
40
41
    /**
42
     * Constant for the "today" keyword
43
     *
44
     * @var string FILTER_KEYWORD_TODAY
45
     */
46
    const FILTER_KEYWORD_TODAY = 'today';
47
48
    /**
49
     * Constant for the "yesterday" keyword
50
     *
51
     * @var string FILTER_KEYWORD_YESTERDAY
52
     */
53
    const FILTER_KEYWORD_YESTERDAY = 'yesterday';
54
55
    /**
56
     * Constant for the "current" keyword
57
     *
58
     * @var string FILTER_KEYWORD_CURRENT
59
     */
60
    const FILTER_KEYWORD_CURRENT = 'current';
61
62
    /**
63
     * Constant for the "to" option
64
     *
65
     * @var string OPTION_TO
66
     */
67
    const OPTION_TO = 'to';
68
69
    /**
70
     * Constant for the "from" option
71
     *
72
     * @var string OPTION_FROM
73
     */
74
    const OPTION_FROM = 'from';
75
76
    /**
77
     * Configures the "show" command
78
     *
79
     * @return void
80
     *
81
     * {@inheritDoc}
82
     * @see \Symfony\Component\Console\Command\Command::configure()
83
     */
84
    protected function configure()
85
    {
86
        $this
87
        ->setName('show')
88
        ->setDescription('Show tracked times')
89
        ->addArgument(
90
            'ticket',
91
            InputArgument::OPTIONAL,
92
            'Show tracked times for a certain ticket'
93
        )
94
            ->addOption(
95
                self::OPTION_FROM,
96
                null,
97
                InputOption::VALUE_REQUIRED,
98
                'Show from a certain date on'
99
            )
100
            ->addOption(
101
                self::OPTION_TO,
102
                null,
103
                InputOption::VALUE_REQUIRED,
104
                'Show up to a certain date'
105
            );
106
    }
107
108
    /**
109
     * Execute the command
110
     *
111
     * @param \Symfony\Component\Console\Input\InputInterface   $input  The command input
112
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output
113
     *
114
     * @return void
115
     *
116
     * {@inheritDoc}
117
     * @see \Symfony\Component\Console\Command\Command::execute()
118
     */
119
    protected function execute(InputInterface $input, OutputInterface $output)
120
    {
121
        // get the ticket
122
        $ticket = $input->getArgument('ticket');
123
124
        // we might need framing dates
125
        $toDate = null;
126
        $fromDate = null;
127
128
        // check if we got a keyword
129
        if ($ticket === self::FILTER_KEYWORD_TODAY) {
130
            // set the fromDate to today, and clear the ticket
131
            $fromDate = strtotime(date('Y-m-d', time()));
132
            $ticket = null;
133
134
        } elseif ($ticket === self::FILTER_KEYWORD_YESTERDAY) {
135
            // set the fromDate to yesterday, the toDate to today and clear the ticket
136
            $fromDate = strtotime(date('Y-m-d', time() - 24 * 60 * 60));
137
            $toDate = strtotime(date('Y-m-d', time()));
138
            $ticket = null;
139
140
        } else {
141
            // check for options first
142 View Code Duplication
            if ($input->getOption(self::OPTION_TO)) {
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...
143
                // test for valid format
144
                $tmpDate = strtotime($input->getOption(self::OPTION_TO));
145
                if ($tmpDate !== false) {
146
                    $toDate = $tmpDate;
147
                }
148
            }
149 View Code Duplication
            if ($input->getOption(self::OPTION_FROM)) {
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...
150
                // test for valid format
151
                $tmpDate = strtotime($input->getOption(self::OPTION_FROM));
152
                if ($tmpDate !== false) {
153
                    $fromDate = $tmpDate;
154
                }
155
            }
156
        }
157
158
        // check for an amount limiting keyword
159
        $limit = null;
160
        if ($ticket === self::FILTER_KEYWORD_CURRENT) {
161
            // set the limit to 1, and clear the ticket
162
            $limit = 1;
163
            $ticket = null;
164
        }
165
166
        // filter by ticket if given
167
        /** @var \Wicked\Timely\Storage\StorageFactory $storage */
168
        $storage = StorageFactory::getStorage();
169
        $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit);
0 ignored issues
show
Bug introduced by
The method retrieve() does not seem to exist on object<Wicked\Timely\Storage\StorageFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
170
171
        // prepare with empty message
172
        $text = 'Sorry, could not find any matching bookings';
173
174
        // format for output (if we got bookings)
175
        if (!empty($bookings)) {
176
            $formatter = FormatterFactory::getFormatter(FormatterFactory::OUTPUT_CHANNEL);
177
            $text = $formatter->toString($bookings);
178
        }
179
180
        // write output
181
        $output->write($text, true);
182
    }
183
}
184