Completed
Push — master ( dbfa6d...65a5eb )
by Bernhard
16s queued 12s
created

AbstractReadCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 49
cp 0
rs 8.8945
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\AbstractReadCommand
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    wick-ed
15
 * @copyright 2020 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    wick-ed
34
 * @copyright 2020 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
abstract class AbstractReadCommand 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
     * Constant for the "specific" option
78
     *
79
     * @var string OPTION_SPECIFIC
80
     */
81
    const OPTION_SPECIFIC = 'specific';
82
83
    /**
84
     * Configures the "show" command
85
     *
86
     * @return void
87
     *
88
     * {@inheritDoc}
89
     * @see \Symfony\Component\Console\Command\Command::configure()
90
     */
91
    protected function configure()
92
    {
93
        $this
94
        ->addArgument(
95
            'ticket',
96
            InputArgument::OPTIONAL,
97
            'Filter tracked times for a certain (ticket) identifier.'
98
        )
99
            ->addOption(
100
                self::OPTION_FROM,
101
                substr(strtolower(self::OPTION_FROM), 0, 1),
102
                InputOption::VALUE_REQUIRED,
103
                'filter from a certain date on. Accepts PHP date and time formats. See help section for further information.'
104
            )
105
            ->addOption(
106
                self::OPTION_SPECIFIC,
107
                substr(strtolower(self::OPTION_SPECIFIC), 0, 1),
108
                InputOption::VALUE_REQUIRED,
109
                'Show only for a specific date'
110
            )
111
            ->addOption(
112
                self::OPTION_TO,
113
                substr(strtolower(self::OPTION_TO), 0, 1),
114
                InputOption::VALUE_REQUIRED,
115
                'Show up to a certain date'
116
            )->setHelp($this->getHelp() . PHP_EOL . PHP_EOL . <<<'EOF'
117
The tracked times in question can also be filter by supplying the optional (ticket) identifier:
118
119
  <info>timely %command.name% FOO-42</info>
120
121
You can further filter by narrowing the time interval.
122
This can be done by using supplied filter keywords <info>current</info>, <info>today</info> or <info>yesterday</info>:
123
124
  <info>timely %command.name% current</info>
125
  or
126
  <info>timely %command.name% yesterday</info>
127
128
This filters for the tracking currently active (as in "what you are currently doing") or all tracked times of yesterday.
129
130
Filtering the processed time trackings is also possible through the <info>from</info>, <info>to</info> and <info>specific</info> options.
131
These options support the PHp date and time format as described here: https://www.php.net/manual/en/datetime.formats.php
132
This allows for refined filtering as shown in the examples below.
133
134
Filter for a certain specific date:
135
<info>timely %command.name% -s 2019-11-28</info>
136
137
Filter for a given time range:
138
<info>timely %command.name% -f 2019-11-28 -t 2019-11-30</info>
139
140
Filter for the last week:
141
<info>timely %command.name% -f"-1 week"</info>
142
143
Filter for everything within the last October:
144
<info>timely %command.name% -f"first day of october" -t"last day of october"</info>
145
146
EOF
147
            )
148
        ;
149
    }
150
151
    /**
152
     * Will provide given input params in a usable format
153
     *
154
     * @param InputInterface  $input    The Symfony console input
155
     * @param string          $ticket   Ticket ID handle, if given
156
     * @param null|string|int $fromDate The date from which an action should be done
157
     * @param null|string|int $toDate   The date up to which an action should be done
158
     * @param int             $limit    Limit for the action, e.g. retrieving tasks
159
     *
160
     * @return void
161
     */
162
    protected function prepareInputParams(InputInterface $input, &$ticket, &$fromDate, &$toDate, &$limit)
163
    {
164
        // check if we got a keyword
165
        if ($ticket === self::FILTER_KEYWORD_TODAY) {
166
            // set the fromDate to today, and clear the ticket
167
            $fromDate = strtotime(date('Y-m-d', time()));
168
            $ticket = null;
169
        } elseif ($ticket === self::FILTER_KEYWORD_YESTERDAY) {
170
            // set the fromDate to yesterday, the toDate to today and clear the ticket
171
            $fromDate = strtotime(date('Y-m-d', time() - 24 * 60 * 60));
172
            $toDate = strtotime(date('Y-m-d', time()));
173
            $ticket = null;
174
        } else {
175
            // check for options first
176
            if ($input->getOption(self::OPTION_SPECIFIC)) {
177
                // test for valid format
178
                $tmpDate = strtotime($input->getOption(self::OPTION_SPECIFIC));
179
                if ($tmpDate !== false) {
180
                    $fromDate = $tmpDate;
181
                    $toDate = $fromDate + 24 * 60 * 60;
182
                }
183
            } else {
184 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...
185
                    // test for valid format
186
                    $tmpDate = strtotime($input->getOption(self::OPTION_TO));
187
                    if ($tmpDate !== false) {
188
                        $toDate = $tmpDate;
189
                    }
190
                }
191 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...
192
                    // test for valid format
193
                    $tmpDate = strtotime($input->getOption(self::OPTION_FROM));
194
                    if ($tmpDate !== false) {
195
                        $fromDate = $tmpDate;
196
                    }
197
                }
198
            }
199
        }
200
201
        // check for an amount limiting keyword
202
        if ($ticket === self::FILTER_KEYWORD_CURRENT) {
203
            // set the limit to 1, and clear the ticket
204
            $limit = 1;
205
            $ticket = null;
206
        }
207
    }
208
}
209