Completed
Push — master ( 4c5cd4...ea9d41 )
by Sergii
10:25
created

Sync::run()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 57
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.9463

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 57
ccs 30
cts 41
cp 0.7317
rs 7.6759
cc 7
eloc 40
nc 17
nop 0
crap 7.9463

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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