Completed
Push — master ( a8371b...db50d3 )
by Sergii
07:40
created

Sync   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 10
dl 0
loc 273
ccs 69
cts 87
cp 0.7931
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
C run() 0 63 8
A getFiles() 0 7 1
A getLogger() 0 8 2
A setLogger() 0 4 1
A getLocker() 0 4 1
A setLocker() 0 4 1
A getMasterFilters() 0 4 1
A setMasterFilters() 0 4 1
A getMasterPath() 0 4 1
A setMasterPath() 0 4 1
A getMasterStorage() 0 4 1
A setMasterStorage() 0 4 1
A getSlaveFilters() 0 4 1
A setSlaveFilters() 0 4 1
A getSlavePath() 0 4 1
A setSlavePath() 0 4 1
A getSlaveStorage() 0 4 1
A setSlaveStorage() 0 4 1
A getSlavePathTpl() 0 4 1
A setSlavePathTpl() 0 4 1
1
<?php
2
namespace AppBundle\Sync;
3
4
use AppBundle\Exception\FilterException;
5
use AppBundle\Exception\StorageException;
6
use AppBundle\Exception\TaskException;
7
use AppBundle\Sync\Storage\StorageInterface as Storage;
8
use Hope\Locker\LockerInterface;
9
use Psr\Log\NullLogger;
10
use Psr\Log\LoggerInterface;
11
use RuntimeException;
12
13
/**
14
 * Sync app
15
 *
16
 * @author Sergey Sadovoi <[email protected]>
17
 */
18
class Sync
19
{
20
    /**
21
     * @var Storage  Main Storage object
22
     */
23
    protected $masterStorage;
24
    /**
25
     * @var string  Root path for sync
26
     */
27
    protected $masterPath;
28
    /**
29
     * @var array  Filters for file list
30
     */
31
    protected $masterFilters = [];
32
33
    /**
34
     * @var Storage  Slave Storage object
35
     */
36
    protected $slaveStorage;
37
    /**
38
     * @var string  Root path for sync
39
     */
40
    protected $slavePath;
41
    /**
42
     * @var array  Filters for file list
43
     */
44
    protected $slaveFilters = [];
45
    /**
46
     * @var string  Template for new files destination
47
     */
48
    protected $slavePathTpl;
49
50
    /**
51
     * @var LoggerInterface
52
     */
53
    protected $logger;
54
    /**
55
     * @var LockerInterface
56
     */
57 1
    protected $locker;
58
59 1
    /**
60
     * Runs the sync tasks
61
     */
62 1
    public function run()
63
    {
64
        $logger = $this->getLogger();
65 1
        $locker = $this->getLocker();
66 1
67 1
        if ($locker->isLocked()) {
68 1
            $logger->warning('Another process is running');
69 1
            return;
70 1
        }
71
72 1
        $locker->lock();
73 1
        $logger->info('START Synchronization');
74 1
75 1
        try {
76 1
            $masterFiles = $this->getFiles(
77 1
                $this->getMasterStorage(),
78 1
                $this->getMasterPath(),
79
                $this->getMasterFilters()
80
            );
81
            $logger->info(sprintf('Master files count: %d', count($masterFiles)));
82
83
            $slaveFiles = $this->getFiles(
84
                $this->getSlaveStorage(),
85 1
                $this->getSlavePath(),
86 1
                $this->getSlaveFilters()
87 1
            );
88
            $logger->info(sprintf('Slave files count: %d', count($slaveFiles)));
89 1
        } catch (FilterException $e) {
90 1
            $logger->error($e->getMessage());
91 1
            $logger->error('ABORT Synchronization');
92
            return;
93
        }
94
95
        try {
96
            $generator = new TaskGenerator();
97 1
            $generator->setSlavePathTpl($this->getSlavePathTpl());
98 1
            $generator->setLogger($logger);
99
100 1
            $tasks = $generator->handle($masterFiles, $slaveFiles);
101 1
            $logger->info(sprintf('Generated %d tasks', count($tasks)));
102 1
        } catch (TaskException $e) {
103
            $logger->error($e->getMessage());
104
            $logger->error('ABORT Synchronization');
105
            return;
106
        }
107
108
        $processor = new Processor($this->getSlaveStorage());
109 1
        foreach ($tasks as $task) {
110
            try {
111
                $result = $processor->execute($task);
112 1
                $logger->info($result);
113 1
            } catch (TaskException $e) {
114
                $logger->error($e->getMessage());
115
            } catch (StorageException $e) {
116
                $logger->error($e->getMessage());
117
            } catch (RuntimeException $e) {
118
                $logger->error($e->getMessage());
119
            }
120
        }
121
122
        $logger->info('END Synchronization');
123
        $locker->release();
124 1
    }
125
126 1
    /**
127 1
     * Gets filtered file list for selected storage
128
     *
129 1
     * @param Storage $storage
130
     * @param string  $path
131
     * @param array   $filters
132
     *
133
     * @return Entity\FileCollection
134
     */
135 1
    protected function getFiles(Storage $storage, $path, array $filters)
136
    {
137 1
        $fc = $storage->listContents($path);
138 1
        $fc->filter($filters);
139
140
        return $fc;
141
    }
142
143
    /**
144
     * @return LoggerInterface
145
     */
146
    protected function getLogger()
147
    {
148
        if (is_null($this->logger)) {
149
            return new NullLogger();
150
        }
151
152
        return $this->logger;
153
    }
154
155 1
    /**
156
     * @param LoggerInterface $logger
157 1
     */
158
    public function setLogger(LoggerInterface $logger)
159
    {
160
        $this->logger = $logger;
161
    }
162
163 1
    /**
164
     * @return LockerInterface
165 1
     */
166 1
    public function getLocker()
167
    {
168
        return $this->locker;
169
    }
170
171 1
    /**
172
     * @param LockerInterface $locker
173 1
     */
174
    public function setLocker(LockerInterface $locker)
175
    {
176
        $this->locker = $locker;
177
    }
178
179 1
    /**
180
     * @return array
181 1
     */
182 1
    protected function getMasterFilters()
183
    {
184
        return $this->masterFilters;
185
    }
186
187 1
    /**
188
     * @param array $masterFilters
189 1
     */
190
    public function setMasterFilters(array $masterFilters)
191
    {
192
        $this->masterFilters = $masterFilters;
193
    }
194
195 1
    /**
196
     * @return string
197 1
     */
198 1
    protected function getMasterPath()
199
    {
200
        return $this->masterPath;
201
    }
202
203 1
    /**
204
     * @param string $masterPath
205 1
     */
206
    public function setMasterPath($masterPath)
207
    {
208
        $this->masterPath = $masterPath;
209
    }
210
211
    /**
212
     * @return Storage
213
     */
214
    protected function getMasterStorage()
215
    {
216
        return $this->masterStorage;
217
    }
218
219 1
    /**
220
     * @param Storage $masterStorage
221 1
     */
222
    public function setMasterStorage(Storage $masterStorage)
223
    {
224
        $this->masterStorage = $masterStorage;
225
    }
226
227 1
    /**
228
     * @return array
229 1
     */
230 1
    protected function getSlaveFilters()
231
    {
232
        return $this->slaveFilters;
233
    }
234
235 1
    /**
236
     * @param array $slaveFilters
237 1
     */
238
    public function setSlaveFilters(array $slaveFilters)
239
    {
240
        $this->slaveFilters = $slaveFilters;
241
    }
242
243 1
    /**
244
     * @return string
245 1
     */
246 1
    protected function getSlavePath()
247
    {
248
        return $this->slavePath;
249
    }
250
251 1
    /**
252
     * @param string $slavePath
253 1
     */
254
    public function setSlavePath($slavePath)
255
    {
256
        $this->slavePath = $slavePath;
257
    }
258
259 1
    /**
260
     * @return Storage
261 1
     */
262 1
    protected function getSlaveStorage()
263
    {
264
        return $this->slaveStorage;
265
    }
266
267
    /**
268
     * @param Storage $slaveStorage
269
     */
270
    public function setSlaveStorage(Storage $slaveStorage)
271
    {
272
        $this->slaveStorage = $slaveStorage;
273
    }
274
275
    /**
276
     * @return string
277
     */
278
    protected function getSlavePathTpl()
279
    {
280
        return $this->slavePathTpl;
281
    }
282
283
    /**
284
     * @param string $slavePathTpl
285
     */
286
    public function setSlavePathTpl($slavePathTpl)
287
    {
288
        $this->slavePathTpl = $slavePathTpl;
289
    }
290
}
291