Completed
Push — master ( dff293...9deb3b )
by Bernhard
10:04
created

AbstractReadCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
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    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
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
     * 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
        ->addArgument(
88
            'ticket',
89
            InputArgument::OPTIONAL,
90
            'Show tracked times for a certain ticket'
91
        )
92
            ->addOption(
93
                self::OPTION_FROM,
94
                null,
95
                InputOption::VALUE_REQUIRED,
96
                'Show from a certain date on'
97
            )
98
            ->addOption(
99
                self::OPTION_TO,
100
                null,
101
                InputOption::VALUE_REQUIRED,
102
                'Show up to a certain date'
103
            );
104
    }
105
106
    /**
107
     * @param InputInterface $input
108
     * @param $ticket
109
     * @param $toDate
110
     * @param $fromDate
111
     * @param $limit
112
     */
113
    protected function prepareInputParams(InputInterface $input, & $ticket, & $fromDate, & $toDate, & $limit)
114
    {
115
        // check if we got a keyword
116
        if ($ticket === self::FILTER_KEYWORD_TODAY) {
117
            // set the fromDate to today, and clear the ticket
118
            $fromDate = strtotime(date('Y-m-d', time()));
119
            $ticket = null;
120
121
        } elseif ($ticket === self::FILTER_KEYWORD_YESTERDAY) {
122
            // set the fromDate to yesterday, the toDate to today and clear the ticket
123
            $fromDate = strtotime(date('Y-m-d', time() - 24 * 60 * 60));
124
            $toDate = strtotime(date('Y-m-d', time()));
125
            $ticket = null;
126
127
        } else {
128
            // check for options first
129 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...
130
                // test for valid format
131
                $tmpDate = strtotime($input->getOption(self::OPTION_TO));
132
                if ($tmpDate !== false) {
133
                    $toDate = $tmpDate;
134
                }
135
            }
136 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...
137
                // test for valid format
138
                $tmpDate = strtotime($input->getOption(self::OPTION_FROM));
139
                if ($tmpDate !== false) {
140
                    $fromDate = $tmpDate;
141
                }
142
            }
143
        }
144
145
        // check for an amount limiting keyword
146
        if ($ticket === self::FILTER_KEYWORD_CURRENT) {
147
            // set the limit to 1, and clear the ticket
148
            $limit = 1;
149
            $ticket = null;
150
        }
151
    }
152
}
153