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

Push::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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