Completed
Push — master ( 81d10e...3eebf6 )
by Bernhard
01:55
created

Push::execute()   C

Complexity

Conditions 10
Paths 23

Size

Total Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 6.1333
c 0
b 0
f 0
cc 10
nc 23
nop 2

How to fix   Long Method    Complexity   

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    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 Wicked\Timely\DotEnvConfiguration;
23
use JiraRestApi\Issue\IssueService;
24
use JiraRestApi\Issue\Worklog;
25
use JiraRestApi\JiraException;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Wicked\Timely\Entities\TaskFactory;
29
use Wicked\Timely\Formatter\FormatterFactory;
30
use Wicked\Timely\Helper\Date;
31
use Wicked\Timely\Storage\StorageFactory;
32
33
/**
34
 * Class for the "track" command. Command is used to track time bookings
35
 *
36
 * @author    Bernhard Wick <[email protected]>
37
 * @copyright 2016 Bernhard Wick
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/wick-ed/timely
40
 */
41
class Push extends AbstractReadCommand
42
{
43
44
    const COMMAND_NAME = 'push';
45
46
    /**
47
     * Configures the "track" command
48
     *
49
     * @return void
50
     *
51
     * {@inheritDoc}
52
     * @see \Symfony\Component\Console\Command\Command::configure()
53
     */
54
    protected function configure()
55
    {
56
        $this
57
        ->setName(static::COMMAND_NAME)
58
        ->setDescription('Pushes booked times against the configured remote');
59
60
        // add all the read options from the abstract super class
61
        parent::configure();
62
    }
63
64
    /**
65
     * Execute the command
66
     *
67
     * @param \Symfony\Component\Console\Input\InputInterface   $input  The command input
68
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output
69
     *
70
     * @return void
71
     *
72
     * {@inheritDoc}
73
     * @see \Symfony\Component\Console\Command\Command::execute()
74
     */
75
    protected function execute(InputInterface $input, OutputInterface $output)
76
    {
77
        // get the ticket
78
        $ticket = $input->getArgument('ticket');
79
80
        // we might need framing dates
81
        $toDate = null;
82
        $fromDate = null;
83
84
        // there might be a limit
85
        $limit = null;
86
87
        // prepare all the input options
88
        $this->prepareInputParams($input, $ticket, $fromDate, $toDate, $limit);
89
90
        // filter by ticket if given
91
        /** @var \Wicked\Timely\Storage\StorageInterface $storage */
92
        $storage = StorageFactory::getStorage();
93
        $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit);
94
95
        // return if we did not find any bookings
96
        if (empty($bookings)) {
97
            $output->write('No bookings found, nothing to push ...', true);
98
            return;
99
        }
100
101
        // get tasks from bookings
102
        $tasks = TaskFactory::getTasksFromBookings($bookings);
103
104
        // retrieve configuration
105
        $configuration = new DotEnvConfiguration();
106
107
        $bookingsPushed = array();
108
        // get our issue service and push the tasks
109
        $issueService = new IssueService($configuration);
110
        foreach ($tasks as $task) {
111
            // Allready pushed to jira? take next one
112
            if ($task->isPushed()) {
113
                continue;
114
            }
115
            // create a worklog from the task
116
            $workLog = new Worklog();
117
            $workLog->setComment($task->getComment())
118
                ->setStarted($task->getStartTime())
119
                ->setTimeSpent(Date::secondsToUnits(Date::roundByInterval($task->getDuration(), 900)));
120
121
            // check the sanity of our worklog and discard it if there is something missing
122
            if (!$task->getTicketId() || empty($workLog->timeSpent) || empty($workLog->comment))
123
            {
124
                $output->writeln('Not pushing one worklog as it misses vital information');
125
                continue;
126
            }
127
128
            // log the worklog about to being pushed if output is verbose
129
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
130
                $output->writeln(
131
                    sprintf(
132
                        '%s : %s > %s',
133
                        $task->getTicketId(),
134
                        $workLog->timeSpent,
135
                        $workLog->comment
136
                    )
137
                );
138
            }
139
140
            try {
141
                // push to remote
142
                $issueService->addWorklog($task->getTicketId(), $workLog);
143
144
                $output->writeln(
145
                    sprintf(
146
                        'PUSHED %s : %s > %s',
147
                        $task->getTicketId(),
148
                        $workLog->timeSpent,
149
                        $workLog->comment
150
                    )
151
                );
152
153
                $bookingsPushed[] = $task->getStartBooking();
154
155
            } catch (JiraException $e) {
156
                $output->write(
157
                    sprintf(
158
                        'Error while pushing. Status %s, with message: "%s"',
159
                        $e->getCode(),
160
                        $e->getMessage()
161
                    )
162
                );
163
            }
164
        }
165
166
        foreach ($bookingsPushed as $booking) {
167
            $storage->storePush($booking);
168
            $formatter = FormatterFactory::getFormatter();
169
            $bookString = $formatter->toString($booking);
170
            $output->write($bookString, true);
171
        }
172
173
        // write output
174
        $output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true);
175
176
    }
177
}
178