-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteLocalBatch.php
More file actions
129 lines (102 loc) · 2.58 KB
/
ExecuteLocalBatch.php
File metadata and controls
129 lines (102 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace SyncEngine\Service;
use Symfony\Component\Filesystem\Filesystem;
use SyncEngine\Model\TraceModel;
class ExecuteLocalBatch
{
private array $batches = [];
protected function __construct(
private readonly TraceModel $trace,
?ExecuteData $data = null,
?int $length = null,
bool $store = true
) {
if ( $data ) {
$this->batches = $data->chunk( $length, true );
if ( $store ) {
foreach ( $this->batches as $index => $batch ) {
$this->storeBatch( $batch, ++$index );
}
}
}
}
/**
* @param TraceModel $trace
* @param ExecuteData $data The full data.
* @param int $length The batch chunk length.
* @param bool $store Whether to store the batches in the filesystem.
*
* @return ExecuteLocalBatch
*/
public static function create(
TraceModel $trace,
ExecuteData $data,
int $length,
bool $store = true
): ExecuteLocalBatch
{
return new static( $trace, $data, $length, $store );
}
public static function load( TraceModel $trace ): ExecuteLocalBatch
{
return new static( $trace );
}
public function getBatch( int $iteration = 1 ): ?ExecuteData
{
$iteration = $iteration ?: 1;
$index = $iteration - 1;
if ( ! isset( $this->batches[ $index ] ) ) {
$this->batches[ $index ] = $this->fetchBatch( $iteration );
}
$batch = $this->batches[ $index ];
if ( null === $batch ) {
return null;
}
return ExecuteData::create( $batch );
}
public function fetchBatch( int $iteration ): ?array
{
$id = $this->trace->getId();
if ( ! $id ) {
return null;
}
$dir = $this->getBatchDir();
$name = $this->getBatchFilename( $iteration );
$file = $dir . $name;
if ( ! $name || ! file_exists( $file ) ) {
return null;
}
return json_decode( file_get_contents( $file ), true );
}
public function storeBatch( ExecuteData $data, int $iteration ): void
{
$id = $this->trace->getId();
if ( ! $id ) {
return;
}
$dir = $this->getBatchDir();
$name = $this->getBatchFilename( $iteration );
if ( ! $name ) {
return;
}
$file = $dir . $name;
( new Filesystem() )->dumpFile( $file, json_encode( $data->get() ) );
}
public function getBatchFilename( $iteration ): string
{
return $this->getBatchIdentifier( $iteration ) . '.json';
}
public function getBatchIdentifier( $iteration ): string
{
return $this->trace->getTraceIdentifier( $iteration ) . '_batch';
}
public function getBatchDir(): string
{
$fs = new Filesystem();
$dir = $this->trace->getTraceDir() . 'batches';
if ( ! $fs->exists( $dir ) ) {
$fs->mkdir( $dir );
}
return $dir . '/';
}
}