Completed
Pull Request — master (#30)
by Bernhard
03:56
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 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 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 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
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
62
        // add all the read options from the abstract super class
63
        parent::configure();
64
    }
65
66
    /**
67
     * Execute the command
68
     *
69
     * @param \Symfony\Component\Console\Input\InputInterface   $input  The command input
70
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output
71
     *
72
     * @return void
73
     *
74
     * {@inheritDoc}
75
     * @throws JiraException
76
     * @throws \JsonMapper_Exception
77
     * @see \Symfony\Component\Console\Command\Command::execute()
78
     */
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        // get the ticket
82
        $ticket = $input->getArgument('ticket');
83
84
        // we might need framing dates
85
        $toDate = null;
86
        $fromDate = null;
87
88
        // there might be a limit
89
        $limit = null;
90
91
        // prepare all the input options
92
        $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 82 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...
93
94
        // filter by ticket if given
95
        /** @var \Wicked\Timely\Storage\StorageInterface $storage */
96
        $storage = StorageFactory::getStorage();
97
        $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit);
98
99
        // return if we did not find any bookings
100
        if (empty($bookings)) {
101
            $output->write('No bookings found, nothing to push ...', true);
102
            return;
103
        }
104
105
        // get tasks from bookings
106
        $tasks = TaskFactory::getTasksFromBookings($bookings);
107
108
        unset($password);
109
        $bookingsPushed = array();
110
111
        // get our issue service and push the tasks
112
        $pushService = PushServiceFactory::getService($output);
113
        foreach ($tasks as $task) {
114
            // Already pushed to jira? take next one
115
            if ($task->isPushed()) {
116
                continue;
117
            }
118
119
            try {
120
                $result = $pushService->push($task);
121
                if ($result) {
122
                    $bookingsPushed[] = $task->getStartBooking();
123
                    $output->writeln(
124
                        sprintf(
125
                            'PUSHED %s : %s > %s',
126
                            $task->getTicketId(),
127
                            $task->getDuration(),
128
                            $task->getComment()
129
                        )
130
                    );
131
                }
132
            } catch (\Exception $e) {
133
                $output->write(
134
                    sprintf(
135
                        'Error while pushing. Status %s, with message: "%s"',
136
                        $e->getCode(),
137
                        $e->getMessage()
138
                    )
139
                );
140
            }
141
        }
142
143
        foreach ($bookingsPushed as $booking) {
144
            $storage->storePush($booking);
145
            $formatter = FormatterFactory::getFormatter();
146
            $bookString = $formatter->toString($booking);
147
            $output->write($bookString, true);
148
        }
149
150
        // write output
151
        $output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true);
152
    }
153
}
154