Syncer::sync()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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