Test Setup Failed
Push — master ( 69f0ae...fa3399 )
by Matthieu
03:05
created

SyncTimings::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 5
1
<?php
2
3
namespace Syncer\Command;
4
5
use Syncer\Dto\InvoiceNinja\Task;
6
use Syncer\Dto\Toggl\TimeEntry;
7
use Syncer\InvoiceNinja\Client as InvoiceNinjaClient;
8
use Syncer\Toggl\ReportsClient;
9
use Syncer\Toggl\TogglClient;
10
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
/**
17
 * Class SyncTimings
18
 * @package Syncer\Command
19
 *
20
 * @author Matthieu Calie <[email protected]>
21
 */
22
class SyncTimings extends Command
23
{
24
    /**
25
     * @var SymfonyStyle
26
     */
27
    private $io;
28
29
    /**
30
     * @var TogglClient
31
     */
32
    private $togglClient;
33
34
    /**
35
     * @var ReportsClient
36
     */
37
    private $reportsClient;
38
39
    /**
40
     * @var InvoiceNinjaClient
41
     */
42
    private $invoiceNinjaClient;
43
44
    /**
45
     * @var array
46
     */
47
    private $clients;
48
49
    /**
50
     * @var array
51
     */
52
    private $projects;
53
54
    /**
55
     * SyncTimings constructor.
56
     *
57
     * @param TogglClient $togglClient
58
     * @param ReportsClient $reportsClient
59
     * @param InvoiceNinjaClient $invoiceNinjaClient
60
     * @param array $clients
61
     * @param array $projects
62
     */
63
    public function __construct(
64
        TogglClient $togglClient,
65
        ReportsClient $reportsClient,
66
        InvoiceNinjaClient $invoiceNinjaClient,
67
        $clients,
68
        $projects
69
    ) {
70
        $this->togglClient = $togglClient;
71
        $this->reportsClient = $reportsClient;
72
        $this->invoiceNinjaClient = $invoiceNinjaClient;
73
        $this->clients = $clients;
74
        $this->projects = $projects;
75
76
        parent::__construct();
77
    }
78
79
    /**
80
     * Configure the command
81
     */
82
    protected function configure()
83
    {
84
        $this
85
            ->setName('sync:timings')
86
            ->setDescription('Syncs timings from toggl to invoiceninja')
87
        ;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        $this->io = new SymfonyStyle($input, $output);
96
        $workspaces = $this->togglClient->getWorkspaces();
97
98
        foreach ($workspaces as $workspace) {
0 ignored issues
show
Bug introduced by
The expression $workspaces of type object|array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
99
            $detailedReport = $this->reportsClient->getDetailedReport($workspace->getId());
100
101
            foreach($detailedReport->getData() as $timeEntry) {
102
                $timeEntrySent = false;
103
104
                // Log the entry if the client key exists
105 View Code Duplication
                if (is_array($this->clients)
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...
106
                    && array_key_exists($timeEntry->getClient(), $this->clients)
107
                ) {
108
                    $this->logTask($timeEntry, $this->clients, $timeEntry->getClient());
109
110
                    $timeEntrySent = true;
111
                }
112
113
                // Log the entry if the project key exists
114 View Code Duplication
                if (is_array($this->projects)
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...
115
                    && array_key_exists($timeEntry->getProject(), $this->projects)
116
                    && !$timeEntrySent
117
                ) {
118
                    $this->logTask($timeEntry, $this->projects, $timeEntry->getProject());
119
120
                    $timeEntrySent = true;
121
                }
122
123
                if ($timeEntrySent) {
124
                    $this->io->success('TimeEntry ('. $timeEntry->getDescription() . ') sent to toggl');
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * @param TimeEntry $entry
132
     * @param array $config
133
     * @param $key
134
     */
135
    private function logTask(TimeEntry $entry, array $config, $key)
136
    {
137
        $task = new Task();
138
139
        $task->setDescription($this->buildTaskDescription($entry));
140
        $task->setTimeLog($this->buildTimeLog($entry));
141
        $task->setClientId($config[$key]);
142
143
        $this->invoiceNinjaClient->saveNewTask($task);
144
    }
145
146
    /**
147
     * @param TimeEntry $entry
148
     *
149
     * @return string
150
     */
151
    private function buildTaskDescription(TimeEntry $entry)
152
    {
153
        $description = '';
154
155
        if ($entry->getProject()) {
156
            $description .= $entry->getProject() . ': ';
157
        }
158
159
        $description .= $entry->getDescription();
160
161
        return $description;
162
    }
163
164
    /**
165
     * @param TimeEntry $entry
166
     *
167
     * @return string
168
     */
169
    private function buildTimeLog(TimeEntry $entry)
170
    {
171
        $timeLog = [
172
            $entry->getStart()->getTimestamp(),
173
            $entry->getEnd()->getTimestamp(),
174
        ];
175
176
        return \GuzzleHttp\json_encode($timeLog);
177
    }
178
}
179