1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Darkilliant\ProcessBundle\LoopStateMarker; |
4
|
|
|
|
5
|
|
|
use Darkilliant\ProcessBundle\State\ProcessState; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
|
8
|
|
|
class FileLoopStateMarker |
9
|
|
|
{ |
10
|
|
|
/** @var Filesystem */ |
11
|
|
|
private $fs; |
12
|
|
|
/** @var bool */ |
13
|
|
|
private $removeOnSuccess = false; |
14
|
|
|
|
15
|
7 |
|
public function __construct(Filesystem $fs) |
16
|
|
|
{ |
17
|
7 |
|
$this->fs = $fs; |
18
|
7 |
|
} |
19
|
|
|
|
20
|
3 |
|
public function onStartLoop(ProcessState $state) |
21
|
|
|
{ |
22
|
3 |
|
if (!$this->isEnabled($state)) { |
23
|
1 |
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
$this->removeOnSuccess = $state->getOptions()['track_loop_state_remove_on_success']; |
27
|
|
|
|
28
|
2 |
|
$fileinfo = pathinfo($state->getContext('file_finder_current')); |
29
|
2 |
|
$this->createStructureProcessing($fileinfo['dirname']); |
30
|
|
|
|
31
|
2 |
|
$targetDirectory = $fileinfo['dirname'].'/_processing/wait'; |
32
|
2 |
|
$targetPath = $targetDirectory.'/'.$fileinfo['basename']; |
33
|
|
|
|
34
|
2 |
|
$this->fs->rename( |
35
|
2 |
|
$state->getContext('file_finder_current'), |
36
|
2 |
|
$targetPath, |
37
|
2 |
|
true |
38
|
|
|
); |
39
|
|
|
|
40
|
2 |
|
$state->setData($targetPath); |
41
|
2 |
|
} |
42
|
|
|
|
43
|
3 |
|
public function onSuccessLoop(ProcessState $state) |
44
|
|
|
{ |
45
|
3 |
|
if (!$this->isEnabled($state)) { |
46
|
1 |
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$fileinfo = pathinfo($state->getContext('file_finder_current')); |
50
|
2 |
|
$this->createStructureProcessing($fileinfo['dirname']); |
51
|
|
|
|
52
|
2 |
|
$sourceDirectory = $fileinfo['dirname'].'/_processing/wait'; |
53
|
2 |
|
$targetDirectory = $fileinfo['dirname'].'/_processing/success'; |
54
|
|
|
|
55
|
2 |
|
if ($this->removeOnSuccess) { |
56
|
1 |
|
$this->fs->remove($sourceDirectory.'/'.$fileinfo['basename']); |
57
|
|
|
|
58
|
1 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$this->fs->rename( |
62
|
1 |
|
$sourceDirectory.'/'.$fileinfo['basename'], |
63
|
1 |
|
$targetDirectory.'/'.$fileinfo['basename'], |
64
|
1 |
|
true |
65
|
|
|
); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
2 |
|
public function onFailedLoop(ProcessState $state) |
69
|
|
|
{ |
70
|
2 |
|
if (!$this->isEnabled($state)) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$fileinfo = pathinfo($state->getContext('file_finder_current')); |
75
|
1 |
|
$this->createStructureProcessing($fileinfo['dirname']); |
76
|
|
|
|
77
|
1 |
|
$sourceDirectory = $fileinfo['dirname'].'/_processing/wait'; |
78
|
1 |
|
$targetDirectory = $fileinfo['dirname'].'/_processing/failed'; |
79
|
|
|
|
80
|
1 |
|
$this->fs->rename( |
81
|
1 |
|
$sourceDirectory.'/'.$fileinfo['basename'], |
82
|
1 |
|
$targetDirectory.'/'.$fileinfo['basename'], |
83
|
1 |
|
true |
84
|
|
|
); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
7 |
|
private function isEnabled(ProcessState $state): bool |
88
|
|
|
{ |
89
|
7 |
|
return $state->getOptions()['track_loop_state'] ?? false; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
private function createStructureProcessing($directory) |
93
|
|
|
{ |
94
|
|
|
// Create wait directory |
95
|
4 |
|
$targetDirectory = $directory.'/_processing/wait'; |
96
|
4 |
|
if (!$this->fs->exists($targetDirectory)) { |
97
|
4 |
|
$this->fs->mkdir($targetDirectory); |
98
|
|
|
} |
99
|
|
|
// Create sucesss directory |
100
|
4 |
|
$targetDirectory = $directory.'/_processing/success'; |
101
|
4 |
|
if (!$this->fs->exists($targetDirectory)) { |
102
|
4 |
|
$this->fs->mkdir($targetDirectory); |
103
|
|
|
} |
104
|
|
|
// Create fail directory |
105
|
4 |
|
$targetDirectory = $directory.'/_processing/failed'; |
106
|
4 |
|
if (!$this->fs->exists($targetDirectory)) { |
107
|
4 |
|
$this->fs->mkdir($targetDirectory); |
108
|
|
|
} |
109
|
4 |
|
} |
110
|
|
|
} |
111
|
|
|
|