Skip to content

Commit 94cd338

Browse files
committed
One way sync code
1 parent ce857d3 commit 94cd338

File tree

4 files changed

+150
-4
lines changed

4 files changed

+150
-4
lines changed

src/FileListing/FileListingFactory.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
namespace DivineOmega\FileSync\FileListing;
44

5+
use League\Flysystem\Filesystem;
6+
57
abstract class FileListingFactory
68
{
79
public static function createFromFilesystems(array $filesystems)
810
{
9-
return array_map(function($filesystem) {
10-
return new FileListing($filesystem);
11+
return array_map(function(FileSystem $filesystem) {
12+
return self::createFromFilesystem($filesystem);
1113
}, $filesystems);
1214
}
15+
16+
public static function createFromFilesystem(Filesystem $filesystem)
17+
{
18+
return new FileListing($filesystem);
19+
}
1320
}

src/FileSyncStrategies/MultiDirectional.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function begin(): void
3030
if ($this->showProgressBar) {
3131
$maxProgress = 1;
3232
$progressBar = new ProgressBar();
33-
$progressBar->setMaxProgress(1);
33+
$progressBar->setMaxProgress($maxProgress);
3434
$progressBar->setMessage('Getting file listings...');
3535
$progressBar->display();
3636
}

src/FileSyncStrategies/OneWay.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,82 @@
22

33
namespace DivineOmega\FileSync\FileSyncStrategies;
44

5+
use DivineOmega\CliProgressBar\ProgressBar;
6+
use DivineOmega\FileSync\FileListing\FileListingFactory;
7+
use DivineOmega\FileSync\FileListing\TransferActionFactory;
58
use DivineOmega\FileSync\Interfaces\FileSyncStrategyInterface;
9+
use League\Flysystem\Filesystem;
610

711
class OneWay implements FileSyncStrategyInterface
812
{
13+
private $fromFilesystem;
14+
private $toFilesystem;
15+
private $showProgressBar = false;
16+
17+
public function from(Filesystem $filesystem)
18+
{
19+
$this->fromFilesystem = $filesystem;
20+
return $this;
21+
}
22+
23+
public function to(Filesystem $filesystem)
24+
{
25+
$this->toFilesystem = $filesystem;
26+
return $this;
27+
}
28+
29+
public function withProgressBar(): self
30+
{
31+
$this->showProgressBar = true;
32+
return $this;
33+
}
34+
935
public function begin(): void
1036
{
11-
// TODO: Implement begin() method.
37+
if ($this->showProgressBar) {
38+
$maxProgress = 3;
39+
$progressBar = new ProgressBar();
40+
$progressBar->setMaxProgress($maxProgress);
41+
$progressBar->setMessage('Getting file listings...');
42+
$progressBar->display();
43+
}
44+
45+
$fromFileListing = FileListingFactory::createFromFilesystem($this->fromFilesystem);
46+
47+
if ($this->showProgressBar) {
48+
$progressBar->advance();
49+
$progressBar->display();
50+
}
51+
52+
$toFileListing = FileListingFactory::createFromFilesystem($this->toFilesystem);
53+
54+
if ($this->showProgressBar) {
55+
$progressBar->advance();
56+
$progressBar->setMessage('Determining differences...');
57+
$progressBar->display();
58+
}
59+
60+
$files = $fromFileListing->getFilesToTransferTo($toFileListing);
61+
$transferActions = TransferActionFactory::createFromFiles($files, $this->fromFilesystem, $this->toFilesystem);
62+
63+
if ($this->showProgressBar) {
64+
$maxProgress += count($transferActions);
65+
$progressBar->setMaxProgress($maxProgress);
66+
$progressBar->setMessage('Transferring files...');
67+
$progressBar->display();
68+
}
69+
70+
foreach($transferActions as $transferAction) {
71+
$transferAction->transfer();
72+
73+
if ($this->showProgressBar) {
74+
$progressBar->advance();
75+
$progressBar->display();
76+
}
77+
}
78+
79+
if ($this->showProgressBar) {
80+
$progressBar->complete();
81+
}
1282
}
1383
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use DivineOmega\FileSync\FileSync;
4+
use League\Flysystem\Adapter\Local;
5+
use League\Flysystem\Filesystem;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class OneWayFileSystemTest extends TestCase
9+
{
10+
public function setupDirectory($name, $fillWithFiles = true)
11+
{
12+
$path = __DIR__.'/Data/'.$name.'/';
13+
14+
if (is_dir($path)) {
15+
16+
$files = glob($path.'*.txt');
17+
18+
foreach($files as $file) {
19+
unlink($file);
20+
}
21+
} else {
22+
mkdir($path, 0777, true);
23+
}
24+
25+
if ($fillWithFiles)
26+
{
27+
$numFiles = rand(1, 100);
28+
29+
$faker = Faker\Factory::create();
30+
31+
$monthInSeconds = 2592000;
32+
33+
for ($i=0; $i < $numFiles; $i++) {
34+
$filename = $faker->word().'.txt';
35+
$content = $faker->text(rand(5, 1000));
36+
file_put_contents($path.$filename, $content);
37+
touch($path.$filename, time() - rand(0, $monthInSeconds));
38+
}
39+
}
40+
41+
$adapter = new Local($path);
42+
return new Filesystem($adapter);
43+
}
44+
45+
public function testMultiDirectionalFileSync()
46+
{
47+
$directoryA = $this->setupDirectory('oneway-a');
48+
$directoryB = $this->setupDirectory('oneway-b', false);
49+
50+
(new FileSync())
51+
->oneWay()
52+
->from($directoryA)
53+
->to($directoryB)
54+
->begin();
55+
56+
$filesA = glob(__DIR__.'/Data/oneway-a/*.txt');
57+
$filesB = glob(__DIR__.'/Data/oneway-b/*.txt');
58+
59+
$this->assertSameSize($filesA, $filesB);
60+
61+
foreach($filesA as $fileA) {
62+
$fileB = __DIR__.'/Data/oneway-b/'.basename($fileA);
63+
64+
$this->assertFileExists($fileB);
65+
66+
$this->assertFileEquals($fileA, $fileB);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)