Skip to content

Commit 4cb662c

Browse files
committed
refactor: cover azure filesystem bridge with mago
1 parent 68cc594 commit 4cb662c

11 files changed

Lines changed: 72 additions & 55 deletions

File tree

Justfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ analyze-mago *args:
7676
src/bridge/psr3/telemetry \
7777
src/bridge/symfony/http-foundation-telemetry \
7878
src/bridge/telemetry/otlp \
79-
src/bridge/filesystem/async-aws
79+
src/bridge/filesystem/async-aws \
80+
src/bridge/filesystem/azure
8081

8182
# Auto-fix code style (Mago format + lint --fix) and GitHub Actions findings (zizmor --fix).
8283
fix:

src/bridge/filesystem/azure/src/Flow/Filesystem/Bridge/Azure/AzureBlobDestinationStream.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public static function openAppend(
4444
BlockFactory $blockFactory = new NativeLocalFileBlocksFactory(),
4545
int $blockSize = 1024 * 1024 * 4,
4646
): self {
47-
if ($blockSize < 1) {
48-
throw new InvalidArgumentException('Block size must be greater than 0');
49-
}
50-
5147
$blocks = new Blocks(
5248
$blockSize,
5349
$blockFactory,
@@ -74,10 +70,6 @@ public static function openBlank(
7470
BlockFactory $blockFactory = new NativeLocalFileBlocksFactory(),
7571
int $blockSize = 1024 * 1024 * 4,
7672
): self {
77-
if ($blockSize < 1) {
78-
throw new InvalidArgumentException('Block size must be greater than 0');
79-
}
80-
8173
return new self(
8274
$blobService,
8375
$path,
@@ -118,9 +110,7 @@ public function close(): void
118110

119111
$this->blobService->putBlockBlob($this->path->path(), $handle, $this->blocks->block()->size());
120112

121-
if (is_resource($handle)) {
122-
fclose($handle);
123-
}
113+
fclose($handle);
124114

125115
unlink($this->blocks->block()->path()->path());
126116
}

src/bridge/filesystem/azure/src/Flow/Filesystem/Bridge/Azure/AzureBlobDestinationStream/AzureBlobBlockLifecycle.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use function count;
1717
use function fclose;
1818
use function fopen;
19-
use function is_resource;
2019
use function unlink;
2120

2221
final class AzureBlobBlockLifecycle implements BlockLifecycle
@@ -48,9 +47,7 @@ public function filled(Block $block): void
4847

4948
$this->blobService->putBlockBlobBlock($this->path->path(), $block->id(), $handle, $block->size());
5049

51-
if (is_resource($handle)) {
52-
fclose($handle);
53-
}
50+
fclose($handle);
5451

5552
unlink($block->path()->path());
5653

src/bridge/filesystem/azure/src/Flow/Filesystem/Bridge/Azure/AzureBlobFilesystem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function getSystemTmpDir(): Path
5252
return $this->options->tmpDir();
5353
}
5454

55+
/**
56+
* @return \Generator<FileStatus>
57+
*/
5558
public function list(Path $path, Filter $pathFilter = new KeepAll()): Generator
5659
{
5760
$this->mount->supports($path) || throw new InvalidSchemeException($path->protocol(), $this->mount->protocol);

src/bridge/filesystem/azure/src/Flow/Filesystem/Bridge/Azure/AzureBlobSourceStream.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public function isOpen(): bool
4747
public function iterate(int $length = 1): Generator
4848
{
4949
$offset = 0;
50+
$size = $this->size() ?? 0;
5051

51-
while ($offset < $this->size()) {
52+
while ($offset < $size) {
5253
yield $this->read($length, $offset);
5354
$offset += $length;
5455
}
@@ -61,7 +62,7 @@ public function path(): Path
6162

6263
public function read(int $length, int $offset): string
6364
{
64-
$offset = $offset < 0 ? $this->size() + $offset : $offset;
65+
$offset = $offset < 0 ? ($this->size() ?? 0) + $offset : $offset;
6566

6667
return $this->blobService
6768
->getBlob($this->path->path(), (new GetBlobOptions())->withRange(new Range($offset, $offset + $length - 1)))
@@ -72,8 +73,9 @@ public function readLines(string $separator = "\n", ?int $length = null): Genera
7273
{
7374
$offset = 0;
7475
$content = '';
76+
$size = $this->size() ?? 0;
7577

76-
while ($offset < $this->size()) {
78+
while ($offset < $size) {
7779
// Read a chunk of the file
7880
$chunk = $this->read($length ?? (1024 * 1024 * 9), $offset);
7981
$offset += strlen($chunk);
@@ -96,12 +98,12 @@ public function readLines(string $separator = "\n", ?int $length = null): Genera
9698

9799
$content = $lines[$lastIndex];
98100
} elseif (substr_count($content, $separator) === 1) {
99-
// Split the content by the separator
100-
/**
101-
* @phpstan-ignore-next-line
102-
*/
103-
yield substr($content, 0, strpos($content, $separator));
104-
$content = substr($content, strpos($content, $separator) + 1);
101+
$pos = strpos($content, $separator);
102+
103+
if ($pos !== false) {
104+
yield substr($content, 0, $pos);
105+
$content = substr($content, $pos + 1);
106+
}
105107
}
106108
}
107109

src/bridge/filesystem/azure/tests/Flow/Filesystem/Bridge/Azure/Tests/Integration/AzureBlobDestinationStreamTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public function test_writing_content_bigger_than_block_size_to_azure(): void
3131
$stream->append($content = str_repeat('a', 200));
3232
$stream->close();
3333

34-
static::assertTrue($fs->status(path('azure-blob://file.txt'))?->isFile());
35-
static::assertFalse($fs->status(path('azure-blob://file.txt'))->isDirectory());
34+
$status = $fs->status(path('azure-blob://file.txt'));
35+
static::assertNotNull($status);
36+
static::assertTrue($status->isFile());
37+
static::assertFalse($status->isDirectory());
3638
static::assertSame($content, $fs->readFrom(path('azure-blob://file.txt'))->content());
3739

3840
$fs->rm(path('azure-blob://file.txt'));
@@ -48,8 +50,10 @@ public function test_writing_content_from_resource(): void
4850
$stream->fromResource($resource);
4951
$stream->close();
5052

51-
static::assertTrue($fs->status(path('azure-blob://orders.csv'))?->isFile());
52-
static::assertFalse($fs->status(path('azure-blob://orders.csv'))->isDirectory());
53+
$status = $fs->status(path('azure-blob://orders.csv'));
54+
static::assertNotNull($status);
55+
static::assertTrue($status->isFile());
56+
static::assertFalse($status->isDirectory());
5357
static::assertSame(
5458
file_get_contents(__DIR__ . '/Fixtures/orders.csv'),
5559
$fs->readFrom(path('azure-blob://orders.csv'))->content(),
@@ -66,8 +70,10 @@ public function test_writing_content_smaller_than_block_size_to_azure(): void
6670
$stream->append('Hello, World!');
6771
$stream->close();
6872

69-
static::assertTrue($fs->status(path('azure-blob://file.txt'))?->isFile());
70-
static::assertFalse($fs->status(path('azure-blob://file.txt'))->isDirectory());
73+
$status = $fs->status(path('azure-blob://file.txt'));
74+
static::assertNotNull($status);
75+
static::assertTrue($status->isFile());
76+
static::assertFalse($status->isDirectory());
7177
static::assertSame('Hello, World!', $fs->readFrom(path('azure-blob://file.txt'))->content());
7278

7379
$fs->rm(path('azure-blob://file.txt'));

src/bridge/filesystem/azure/tests/Flow/Filesystem/Bridge/Azure/Tests/Integration/AzureBlobFilesystemTest.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public function test_appending_to_existing_blob(): void
2727
$stream->append("This is second line\n");
2828
$stream->close();
2929

30-
static::assertTrue($fs->status(path('azure-blob://file.txt'))?->isFile());
31-
static::assertFalse($fs->status(path('azure-blob://file.txt'))->isDirectory());
30+
$status = $fs->status(path('azure-blob://file.txt'));
31+
static::assertNotNull($status);
32+
static::assertTrue($status->isFile());
33+
static::assertFalse($status->isDirectory());
3234
static::assertSame(<<<'TXT'
3335
This is first line
3436
This is second line
@@ -59,8 +61,10 @@ public function test_appending_to_existing_block_blob_new_blocks(): void
5961
}
6062
$stream->close();
6163

62-
static::assertTrue($fs->status(path('azure-blob://file.txt'))?->isFile());
63-
static::assertFalse($fs->status(path('azure-blob://file.txt'))->isDirectory());
64+
$status = $fs->status(path('azure-blob://file.txt'));
65+
static::assertNotNull($status);
66+
static::assertTrue($status->isFile());
67+
static::assertFalse($status->isDirectory());
6468
static::assertSame($output, $fs->readFrom(path('azure-blob://file.txt'))->content());
6569

6670
$fs->rm(path('azure-blob://file.txt'));
@@ -83,8 +87,10 @@ public function test_appending_to_existing_non_block_blob_new_blocks(): void
8387
}
8488
$stream->close();
8589

86-
static::assertTrue($fs->status(path('azure-blob://file.txt'))?->isFile());
87-
static::assertFalse($fs->status(path('azure-blob://file.txt'))->isDirectory());
90+
$status = $fs->status(path('azure-blob://file.txt'));
91+
static::assertNotNull($status);
92+
static::assertTrue($status->isFile());
93+
static::assertFalse($status->isDirectory());
8894
static::assertSame($output, $fs->readFrom(path('azure-blob://file.txt'))->content());
8995

9096
$fs->rm(path('azure-blob://file.txt'));
@@ -153,11 +159,10 @@ public function test_file_status_on_pattern(): void
153159
static::assertIsResource($resource);
154160
$fs->writeTo(path('azure-blob://some_path_to/file.txt'))->fromResource($resource)->close();
155161

156-
static::assertTrue($fs->status(path('azure-blob://some_path_to/*.txt'))?->isFile());
157-
static::assertSame(
158-
'azure-blob://some_path_to/file.txt',
159-
$fs->status(path('azure-blob://some_path_to/*.txt'))->path->uri(),
160-
);
162+
$status = $fs->status(path('azure-blob://some_path_to/*.txt'));
163+
static::assertNotNull($status);
164+
static::assertTrue($status->isFile());
165+
static::assertSame('azure-blob://some_path_to/file.txt', $status->path->uri());
161166
}
162167

163168
public function test_file_status_on_root_folder(): void
@@ -316,8 +321,10 @@ public function test_writing_to_azure_blob_storage(): void
316321
$stream->append('Hello, World!');
317322
$stream->close();
318323

319-
static::assertTrue($fs->status(path('azure-blob://file.txt'))?->isFile());
320-
static::assertFalse($fs->status(path('azure-blob://file.txt'))->isDirectory());
324+
$status = $fs->status(path('azure-blob://file.txt'));
325+
static::assertNotNull($status);
326+
static::assertTrue($status->isFile());
327+
static::assertFalse($status->isDirectory());
321328
static::assertSame('Hello, World!', $fs->readFrom(path('azure-blob://file.txt'))->content());
322329

323330
$fs->rm(path('azure-blob://file.txt'));
@@ -333,8 +340,10 @@ public function test_writing_to_to_azure_from_resources(): void
333340
$stream->fromResource($resource);
334341
$stream->close();
335342

336-
static::assertTrue($fs->status(path('azure-blob://orders.csv'))?->isFile());
337-
static::assertFalse($fs->status(path('azure-blob://orders.csv'))->isDirectory());
343+
$status = $fs->status(path('azure-blob://orders.csv'));
344+
static::assertNotNull($status);
345+
static::assertTrue($status->isFile());
346+
static::assertFalse($status->isDirectory());
338347
static::assertSame(
339348
file_get_contents(__DIR__ . '/Fixtures/orders.csv'),
340349
$fs->readFrom(path('azure-blob://orders.csv'))->content(),
@@ -355,8 +364,10 @@ public function test_writing_to_to_azure_using_blocks(): void
355364

356365
$stream->close();
357366

358-
static::assertTrue($fs->status(path('azure-blob://block_blob.csv'))?->isFile());
359-
static::assertFalse($fs->status(path('azure-blob://block_blob.csv'))->isDirectory());
367+
$status = $fs->status(path('azure-blob://block_blob.csv'));
368+
static::assertNotNull($status);
369+
static::assertTrue($status->isFile());
370+
static::assertFalse($status->isDirectory());
360371

361372
$fs->rm(path('azure-blob://block_blob.csv'));
362373
}

src/bridge/filesystem/azure/tests/Flow/Filesystem/Bridge/Azure/Tests/Unit/AzureBlobDestinationStreamTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function test_using_put_blob_with_content_when_data_is_larger_than_block_
5353
->method('putBlockBlobBlock')
5454
->with(
5555
'/file.txt',
56-
static::isType('string'),
57-
static::isType('resource'),
58-
static::isType('int'),
56+
static::isString(),
57+
static::isResource(),
58+
static::isInt(),
5959
static::isInstanceOf(PutBlockBlobBlockOptions::class),
6060
);
6161

@@ -98,7 +98,7 @@ public function test_using_put_blob_with_content_when_data_is_smaller_than_block
9898
->method('putBlockBlob')
9999
->with(
100100
'/file.txt',
101-
static::isType('resource'),
101+
static::isResource(),
102102
strlen('Hello, World!'),
103103
static::isInstanceOf(PutBlockBlobOptions::class),
104104
);

src/lib/azure-sdk/src/Flow/Azure/SDK/BlobService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function getContainerProperties(GetContainerPropertiesOptions $options =
367367
/**
368368
* @throws AzureException
369369
*
370-
* @return \Generator<Blob>
370+
* @return \Generator<int, Blob>
371371
*/
372372
public function listBlobs(ListBlobOptions $options = new ListBlobOptions()): Generator
373373
{

src/lib/azure-sdk/src/Flow/Azure/SDK/BlobServiceInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getBlockBlobBlockList(
4747
public function getContainerProperties(GetContainerPropertiesOptions $options = new GetContainerPropertiesOptions()): ?ContainerProperties;
4848

4949
/**
50-
* @return \Generator<Blob>
50+
* @return \Generator<int, Blob>
5151
*/
5252
public function listBlobs(ListBlobOptions $options = new ListBlobOptions()): Generator;
5353

0 commit comments

Comments
 (0)