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

Push::execute()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 0
cts 52
cp 0
rs 7.6339
c 0
b 0
f 0
cc 7
nc 15
nop 2
crap 56

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\Push
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 JiraRestApi\JiraException;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Wicked\Timely\Entities\TaskFactory;
26
use Wicked\Timely\Formatter\FormatterFactory;
27
use Wicked\Timely\PushServices\PushServiceFactory;
28
use Wicked\Timely\Storage\StorageFactory;
29
30
/**
31
 * Class for the "track" command. Command is used to track time bookings
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
class Push extends AbstractReadCommand
39
{
40
41
    /**
42
     * Constants used within this command
43
     *
44
     * @var string
45
     */
46
    const COMMAND_NAME = 'push';
47
48
    /**
49
     * Configures the "track" command
50
     *
51
     * @return void
52
     *
53
     * {@inheritDoc}
54
     * @see \Symfony\Component\Console\Command\Command::configure()
55
     */
56
    protected function configure()
57
    {
58
        $this
59
        ->setName(static::COMMAND_NAME)
60
        ->setDescription('Pushes booked times against the configured remote')
61
        ->setHelp(<<<'EOF'
62
The <info>%command.name%</info> command is used to push tracked times to an external time keeping service.
63
Jira being an example of a supported service.
64
65
The command has the same syntax and usability as the <info>show</info> command.
66
On execution the command will use the service's internal format to process all tracked times that a similar <info>show</info> command would have displayed.
67
68
The following command would create e.g. Jira worklogs for yesterday's tasks:
69
70
<info>timely %command.name% yesterday</info>
71
72
The <info>%command.name%</info> command keeps track of already pushed time trackings so nothing gets pushed twice.
73
EOF
74
        )
75
        ;
76
        // add all the read options from the abstract super class
77
        parent::configure();
78
    }
79
80
    /**
81
     * Execute the command
82
     *
83
     * @param \Symfony\Component\Console\Input\InputInterface   $input  The command input
84
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output
85
     *
86
     * @return void
87
     *
88
     * {@inheritDoc}
89
     * @throws JiraException
90
     * @throws \JsonMapper_Exception
91
     * @see \Symfony\Component\Console\Command\Command::execute()
92
     */
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        // get the ticket
96
        $ticket = $input->getArgument('ticket');
97
98
        // we might need framing dates
99
        $toDate = null;
100
        $fromDate = null;
101
102
        // there might be a limit
103
        $limit = null;
104
105
        // prepare all the input options
106
        $this->prepareInputParams($input, $ticket, $fromDate, $toDate, $limit);
0 ignored issues
show
Bug introduced by
It seems like $ticket defined by $input->getArgument('ticket') on line 96 can also be of type array<integer,string> or null; however, Wicked\Timely\Command\Ab...d::prepareInputParams() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
108
        // filter by ticket if given
109
        /** @var \Wicked\Timely\Storage\StorageInterface $storage */
110
        $storage = StorageFactory::getStorage();
111
        $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit);
112
113
        // return if we did not find any bookings
114
        if (empty($bookings)) {
115
            $output->write('No bookings found, nothing to push ...', true);
116
            return;
117
        }
118
119
        // get tasks from bookings
120
        $tasks = TaskFactory::getTasksFromBookings($bookings);
121
122
        unset($password);
123
        $bookingsPushed = array();
124
125
        // get our issue service and push the tasks
126
        $pushService = PushServiceFactory::getService($output);
127
        foreach ($tasks as $task) {
128
            // Already pushed to jira? take next one
129
            if ($task->isPushed()) {
130
                continue;
131
            }
132
133
            try {
134
                $result = $pushService->push($task);
135
                if ($result) {
136
                    $bookingsPushed[] = $task->getStartBooking();
137
                    $output->writeln(
138
                        sprintf(
139
                            'PUSHED %s : %s > %s',
140
                            $task->getTicketId(),
141
                            $task->getDuration(),
142
                            $task->getComment()
143
                        )
144
                    );
145
                }
146
            } catch (\Exception $e) {
147
                $output->write(
148
                    sprintf(
149
                        '<erro>Error while pushing. Status %s, with message: "%s"</erro>',
150
                        $e->getCode(),
151
                        $e->getMessage()
152
                    )
153
                );
154
            }
155
        }
156
157
        foreach ($bookingsPushed as $booking) {
158
            $storage->storePush($booking);
159
            $formatter = FormatterFactory::getFormatter();
160
            $bookString = $formatter->toString($booking);
161
            $output->write($bookString, true);
162
        }
163
164
        // write output
165
        $output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true);
166
    }
167
}
168