1 | <?php declare(strict_types=1); |
||
22 | * @author Matthieu Calie <[email protected]> |
||
23 | */ |
||
24 | class SyncTimings extends Command |
||
25 | { |
||
26 | /** |
||
27 | * @var SymfonyStyle |
||
28 | */ |
||
29 | private $io; |
||
30 | |||
31 | /** |
||
32 | * @var TogglClient |
||
33 | */ |
||
34 | private $togglClient; |
||
35 | |||
36 | /** |
||
37 | * @var TogglClients |
||
38 | */ |
||
39 | private $togglClients; |
||
40 | |||
41 | /** |
||
42 | * @var ReportsClient |
||
43 | */ |
||
44 | private $reportsClient; |
||
45 | |||
46 | /** |
||
47 | * @var InvoiceNinjaClient |
||
48 | */ |
||
49 | private $invoiceNinjaClient; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $clients; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $projects; |
||
60 | |||
61 | /** |
||
62 | * SyncTimings constructor. |
||
63 | * |
||
64 | * @param TogglClient $togglClient |
||
65 | * @param ReportsClient $reportsClient |
||
66 | * @param InvoiceNinjaClient $invoiceNinjaClient |
||
67 | * @param array $clients |
||
68 | * @param array $projects |
||
69 | */ |
||
70 | public function __construct( |
||
71 | TogglClient $togglClient, |
||
72 | ReportsClient $reportsClient, |
||
73 | InvoiceNinjaClient $invoiceNinjaClient, |
||
74 | $clients, |
||
75 | $projects, |
||
76 | String $storageDir, |
||
77 | String $storageFileName |
||
78 | ) { |
||
79 | $this->togglClient = $togglClient; |
||
80 | $this->reportsClient = $reportsClient; |
||
81 | $this->invoiceNinjaClient = $invoiceNinjaClient; |
||
82 | $this->clients = $clients; |
||
83 | $this->projects = $projects; |
||
84 | $this->storageDir = $storageDir; |
||
85 | $this->storageFileName = $storageFileName; |
||
86 | $this->retrieveSentTimeEntries(); |
||
87 | |||
88 | parent::__construct(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Configure the command |
||
93 | */ |
||
94 | protected function configure() |
||
95 | { |
||
96 | $this |
||
97 | ->setName('sync:timings') |
||
98 | ->setDescription('Syncs timings from toggl to invoiceninja') |
||
99 | ; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | protected function execute(InputInterface $input, OutputInterface $output) |
||
106 | { |
||
107 | $this->io = new SymfonyStyle($input, $output); |
||
108 | $workspaces = $this->togglClient->getWorkspaces(); |
||
109 | |||
110 | if (!is_array($workspaces) || count($workspaces) === 0) { |
||
111 | $this->io->error('No workspaces to sync.'); |
||
112 | |||
113 | return; |
||
114 | } |
||
115 | |||
116 | foreach ($workspaces as $workspace) { |
||
117 | $detailedReport = $this->reportsClient->getDetailedReport($workspace->getId()); |
||
118 | $this->clients = array_merge($this->clients, $this->retrieveClientsForWorkspace($workspace->getId())); |
||
119 | $this->projects = array_merge($this->projects, $this->retrieveProjectsForWorkspace($workspace->getId())); |
||
120 | |||
121 | foreach($detailedReport->getData() as $timeEntry) { |
||
122 | $timeEntrySent = false; |
||
123 | |||
124 | if (in_array($timeEntry->getId(), $this->sentTimeEntries)) |
||
125 | continue; |
||
126 | |||
127 | // Log the entry if the client key exists |
||
128 | if ($this->timeEntryCanBeLoggedByConfig($this->clients, $timeEntry->getClient(), $timeEntrySent)) { |
||
129 | $this->logTask($timeEntry, $this->clients, $timeEntry->getClient(), $this->projects, $timeEntry->getProject()); |
||
130 | |||
131 | $this->sentTimeEntries[] = $timeEntry->getId(); |
||
132 | $timeEntrySent = true; |
||
133 | } |
||
134 | |||
135 | // Log the entry if the project key exists |
||
136 | if ($this->timeEntryCanBeLoggedByConfig($this->projects, $timeEntry->getProject(), $timeEntrySent)) { |
||
137 | $this->logTask($timeEntry, $this->projects, $timeEntry->getProject()); |
||
138 | |||
139 | $this->sentTimeEntries[] = $timeEntry->getId(); |
||
140 | $timeEntrySent = true; |
||
141 | } |
||
142 | |||
143 | if ($timeEntrySent) { |
||
144 | $this->io->success('TimeEntry ('. $timeEntry->getDescription() . ') sent to InvoiceNinja'); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | $this->storeSentTimeEntries(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param array $config |
||
154 | * @param string $entryKey |
||
155 | * @param bool $hasAlreadyBeenSent |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | private function timeEntryCanBeLoggedByConfig(array $config, string $entryKey, bool $hasAlreadyBeenSent): bool |
||
160 | { |
||
161 | if ($hasAlreadyBeenSent) { |
||
162 | return false; |
||
163 | } |
||
164 | |||
165 | return (is_array($config) && array_key_exists($entryKey, $config)); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param TimeEntry $entry |
||
170 | * @param array $clients |
||
171 | * @param string $clientKey |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | private function logTask(TimeEntry $entry, array $clients, string $clientKey, array $projects = NULL, string $projectKey = NULL) |
||
176 | { |
||
177 | $task = new Task(); |
||
178 | |||
179 | $task->setDescription($this->buildTaskDescription($entry)); |
||
180 | $task->setTimeLog($this->buildTimeLog($entry)); |
||
181 | $task->setClientId($clients[$clientKey]); |
||
182 | |||
183 | if (isset($projects) && isset($projectKey)) |
||
184 | $task->setProjectId($projects[$projectKey]); |
||
185 | |||
186 | $this->invoiceNinjaClient->saveNewTask($task); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param TimeEntry $entry |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | private function buildTaskDescription(TimeEntry $entry): string |
||
195 | { |
||
196 | $description = ''; |
||
197 | |||
198 | if ($entry->getProject()) { |
||
322 |