1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TogglJira\Command; |
5
|
|
|
|
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
7
|
|
|
use Psr\Log\LoggerAwareTrait; |
8
|
|
|
use TogglJira\Options\SyncOptions; |
9
|
|
|
use TogglJira\Service\SyncService; |
10
|
|
|
use Zend\Config\Writer\Json; |
11
|
|
|
use Zend\Console\Adapter\AdapterInterface; |
12
|
|
|
use Zend\Console\Request; |
13
|
|
|
|
14
|
|
|
class SyncCommand implements CommandInterface, LoggerAwareInterface |
15
|
|
|
{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var SyncService |
20
|
|
|
*/ |
21
|
|
|
private $service; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SyncOptions |
25
|
|
|
*/ |
26
|
|
|
private $syncOptions; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Json |
30
|
|
|
*/ |
31
|
|
|
private $writer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param SyncService $service |
35
|
|
|
* @param SyncOptions $syncOptions |
36
|
|
|
* @param Json $writer |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct(SyncService $service, SyncOptions $syncOptions, Json $writer) |
39
|
|
|
{ |
40
|
2 |
|
$this->service = $service; |
41
|
2 |
|
$this->syncOptions = $syncOptions; |
42
|
2 |
|
$this->writer = $writer; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param AdapterInterface $console |
48
|
|
|
* @return int |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
1 |
|
public function execute(Request $request, AdapterInterface $console): int |
52
|
|
|
{ |
53
|
1 |
|
$reqStartDate = $request->getParam('startDate', null); |
54
|
1 |
|
$reqEndDate = $request->getParam('endDate', null); |
55
|
1 |
|
$startDate = $reqStartDate ? new \DateTime($reqStartDate) : $this->syncOptions->getLastSync(); |
56
|
1 |
|
$endDate = $reqEndDate ? new \DateTime($reqEndDate) : new \DateTime('now'); |
57
|
1 |
|
$overwrite = (bool) $request->getParam('overwrite', false); |
58
|
|
|
|
59
|
1 |
|
$this->logger->info( |
60
|
1 |
|
'Syncing time entries', |
61
|
1 |
|
['lastSync' => $startDate->format(DATE_ATOM)] |
62
|
|
|
); |
63
|
|
|
|
64
|
1 |
|
$this->service->sync($startDate, $endDate, $overwrite); |
65
|
1 |
|
$this->syncOptions->setLastSync($endDate); |
66
|
|
|
|
67
|
1 |
|
$this->writer->toFile('config.json', $this->syncOptions->toArray()); |
68
|
|
|
|
69
|
1 |
|
$this->logger->info('Updated last sync time'); |
70
|
|
|
|
71
|
1 |
|
return 0; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|