|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Velikonja\LabbyBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
7
|
|
|
use Velikonja\LabbyBundle\Database\SyncerDb; |
|
8
|
|
|
use SyncFS\Syncer as SyncerFs; |
|
9
|
|
|
use Velikonja\LabbyBundle\Event\SyncEvent; |
|
10
|
|
|
use Velikonja\LabbyBundle\Events; |
|
11
|
|
|
|
|
12
|
|
|
class Syncer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var SyncerFs |
|
16
|
|
|
*/ |
|
17
|
|
|
private $syncerFs; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var SyncerDb | null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $syncerDb; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EventDispatcherInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $eventDispatcher; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
31
|
|
|
* @param SyncerFs $syncerFs |
|
32
|
|
|
* @param null|SyncerDb $syncerDb |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public function __construct( |
|
35
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
36
|
|
|
SyncerFs $syncerFs, |
|
37
|
|
|
SyncerDb $syncerDb = null |
|
38
|
|
|
) { |
|
39
|
4 |
|
$this->syncerFs = $syncerFs; |
|
40
|
4 |
|
$this->syncerDb = $syncerDb; |
|
41
|
4 |
|
$this->eventDispatcher = $eventDispatcher; |
|
42
|
4 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param OutputInterface $output |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function sync(OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->eventDispatcher->dispatch(Events::PRE_SYNC, new SyncEvent($output)); |
|
50
|
1 |
|
$this->syncDb($output); |
|
51
|
1 |
|
$this->syncFs($output); |
|
52
|
1 |
|
$this->eventDispatcher->dispatch(Events::POST_SYNC, new SyncEvent($output)); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param OutputInterface $output |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function syncFs(OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$this->eventDispatcher->dispatch(Events::PRE_SYNC_FS, new SyncEvent($output)); |
|
61
|
2 |
|
$this->syncerFs->sync($output); |
|
62
|
2 |
|
$this->eventDispatcher->dispatch(Events::POST_SYNC_FS, new SyncEvent($output)); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param OutputInterface $output |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function syncDb(OutputInterface $output) |
|
71
|
|
|
{ |
|
72
|
3 |
|
if (! $this->syncerDb) { |
|
73
|
|
|
throw new \RuntimeException('Syncer DB is not defined.'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
$this->eventDispatcher->dispatch(Events::PRE_SYNC_DB, new SyncEvent($output)); |
|
77
|
3 |
|
$this->syncerDb->sync($output); |
|
78
|
3 |
|
$this->eventDispatcher->dispatch(Events::POST_SYNC_DB, new SyncEvent($output)); |
|
79
|
3 |
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|