Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Storage interface and replace by StorageImpl #366

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ services:
Buddy\Repman\Service\Proxy\ProxyRegister:
public: true

repman.organization.dist_storage: '@Buddy\Repman\Service\Dist\Storage\StorageImpl'
repman.organization.dist_storage: '@Buddy\Repman\Service\Dist\Storage'

Buddy\Repman\Service\Mailer\SymfonyMailer:
arguments:
Expand Down
121 changes: 113 additions & 8 deletions src/Service/Dist/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,135 @@
namespace Buddy\Repman\Service\Dist;

use Buddy\Repman\Service\Dist;
use Buddy\Repman\Service\Downloader;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use Munus\Control\Option;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

interface Storage
class Storage
{
public function has(Dist $dist): bool;
private Downloader $downloader;
private FilesystemInterface $repoFilesystem;

public function __construct(Downloader $downloader, FilesystemInterface $repoFilesystem)
{
$this->downloader = $downloader;
$this->repoFilesystem = $repoFilesystem;
}

public function has(Dist $dist): bool
{
return $this->repoFilesystem->has($this->filename($dist));
}

/**
* @param string[] $headers
*/
public function download(string $url, Dist $dist, array $headers = []): void;
public function download(string $url, Dist $dist, array $headers = []): void
{
if ($this->has($dist)) {
return;
}

$filename = $this->filename($dist);

public function remove(Dist $dist): void;
$this->repoFilesystem->writeStream(
$filename,
$this->downloader->getContents(
$url,
$headers,
function () use ($url): void {
throw new NotFoundHttpException(\sprintf('File not found at %s', $url));
}
)->getOrElseThrow(
new \RuntimeException(\sprintf('Failed to download %s from %s', $dist->package(), $url))
)
);
}

public function filename(Dist $dist): string;
public function remove(Dist $dist): void
{
$filename = $this->filename($dist);
if ($this->repoFilesystem->has($filename)) {
$this->repoFilesystem->delete($filename);
}
}

public function size(Dist $dist): int;
public function filename(Dist $dist): string
{
return \sprintf(
'%s/dist/%s/%s_%s.%s',
$dist->repo(),
$dist->package(),
$dist->version(),
$dist->ref(),
$dist->format()
);
}

public function size(Dist $dist): int
{
$filename = $this->filename($dist);
if ($this->repoFilesystem->has($filename)) {
/* @phpstan-ignore-next-line - will always return int because file exists */
return $this->repoFilesystem->getSize($filename);
}

return 0;
}

/**
* @return Option<string>
*/
public function getLocalFileForDistUrl(string $distFilename): Option;
public function getLocalFileForDist(Dist $dist): Option
{
return $this->getLocalFileForDistUrl($this->filename($dist));
}

/**
* @return Option<string>
*/
public function getLocalFileForDist(Dist $dist): Option;
public function getLocalFileForDistUrl(string $distFilename): Option
{
$tmpLocalFilename = $this->getTempFileName();
$tmpLocalFileHandle = \fopen(
$tmpLocalFilename,
'wb'
);
if (false === $tmpLocalFileHandle) {
throw new \RuntimeException('Could not open temporary file for writing zip file for dist.');
}

$distReadStream = $this->readStream($distFilename)->getOrNull();
if (null === $distReadStream) {
return Option::none();
}
\stream_copy_to_stream($distReadStream, $tmpLocalFileHandle);
\fclose($tmpLocalFileHandle);

return Option::of($tmpLocalFilename);
}

private function getTempFileName(): string
{
return \sys_get_temp_dir().\DIRECTORY_SEPARATOR.\uniqid('repman-dist-', true);
}

/**
* @return Option<resource>
*/
private function readStream(string $path): Option
{
try {
$resource = $this->repoFilesystem->readStream($path);
if (false === $resource) {
return Option::none();
}
} catch (FileNotFoundException $e) {
return Option::none();
}

return Option::of($resource);
}
}
140 changes: 0 additions & 140 deletions src/Service/Dist/Storage/StorageImpl.php

This file was deleted.

34 changes: 17 additions & 17 deletions tests/Unit/Service/Organization/PackageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ final class PackageManagerTest extends TestCase

protected function setUp(): void
{
$basePath = dirname(__DIR__, 3);
$basePath = \dirname(__DIR__, 3);
$this->filesystem = new Filesystem(new Local($basePath.'/Resources/fixtures/'));
$this->manager = new PackageManager(
new Storage\StorageImpl(
new Storage(
new FakeDownloader(), new Filesystem(new MemoryAdapter())
),
$this->filesystem
Expand Down Expand Up @@ -85,15 +85,15 @@ public function testRemoveProvider(): void
$manager->saveProvider([], $org, $package1);
$manager->saveProvider([], $org, $package2);

self::assertTrue(file_exists($this->baseDir.'/buddy/p/'.$package1.'.json'));
self::assertTrue(file_exists($this->baseDir.'/buddy/p/'.$package2.'.json'));
self::assertFileExists($this->baseDir.'/buddy/p/'.$package1.'.json');
self::assertFileExists($this->baseDir.'/buddy/p/'.$package2.'.json');

$manager->removeProvider($org, $package1);

self::assertTrue(is_dir($this->baseDir.'/buddy'));
self::assertTrue(is_dir(dirname($this->baseDir.'/buddy/p/'.$package1)));
self::assertFalse(file_exists($this->baseDir.'/buddy/p/'.$package1.'.json'));
self::assertTrue(file_exists($this->baseDir.'/buddy/p/'.$package2.'.json'));
self::assertDirectoryExists($this->baseDir.'/buddy');
self::assertDirectoryExists(\dirname($this->baseDir.'/buddy/p/'.$package1));
self::assertFileDoesNotExist($this->baseDir.'/buddy/p/'.$package1.'.json');
self::assertFileExists($this->baseDir.'/buddy/p/'.$package2.'.json');
}

public function testRemoveDist(): void
Expand All @@ -104,15 +104,15 @@ public function testRemoveDist(): void
$package1 = 'vendor/package1';
$package2 = 'vendor/package2';

@mkdir($this->baseDir.'/buddy/dist/'.$package1, 0777, true);
@mkdir($this->baseDir.'/buddy/dist/'.$package2, 0777, true);
@\mkdir($this->baseDir.'/buddy/dist/'.$package1, 0777, true);
@\mkdir($this->baseDir.'/buddy/dist/'.$package2, 0777, true);

$manager->removeDist($org, $package1);

self::assertTrue(is_dir($this->baseDir.'/buddy'));
self::assertTrue(is_dir($this->baseDir.'/buddy/dist/vendor'));
self::assertFalse(is_dir($this->baseDir.'/buddy/dist/'.$package1));
self::assertTrue(is_dir($this->baseDir.'/buddy/dist/'.$package2));
self::assertDirectoryExists($this->baseDir.'/buddy');
self::assertDirectoryExists($this->baseDir.'/buddy/dist/vendor');
self::assertDirectoryDoesNotExist($this->baseDir.'/buddy/dist/'.$package1);
self::assertDirectoryExists($this->baseDir.'/buddy/dist/'.$package2);
}

public function testRemoveOrganizationDir(): void
Expand All @@ -124,20 +124,20 @@ public function testRemoveOrganizationDir(): void

$manager->saveProvider([], $org, $package);

self::assertTrue(is_dir($this->baseDir.'/buddy/p/hello'));
self::assertDirectoryExists($this->baseDir.'/buddy/p/hello');

$manager->removeProvider($org, $package)
->removeOrganizationDir($org);

self::assertFalse(is_dir($this->baseDir.'/buddy'));
self::assertDirectoryDoesNotExist($this->baseDir.'/buddy');
}

private function getManagerWithLocalStorage(): PackageManager
{
$repoFilesystem = new Filesystem(new Local($this->baseDir));

return new PackageManager(
new Storage\StorageImpl(new FakeDownloader(), $repoFilesystem),
new Storage(new FakeDownloader(), $repoFilesystem),
$repoFilesystem
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Buddy\Repman\Entity\Organization\Package\Version;
use Buddy\Repman\Repository\PackageRepository;
use Buddy\Repman\Service\Dist\Storage\StorageImpl;
use Buddy\Repman\Service\Dist\Storage;
use Buddy\Repman\Service\Organization\PackageManager;
use Buddy\Repman\Service\PackageNormalizer;
use Buddy\Repman\Service\PackageSynchronizer\ComposerPackageSynchronizer;
Expand All @@ -31,7 +31,7 @@ protected function setUp(): void
$this->baseDir = sys_get_temp_dir().'/repman';
$repoFilesystem = new Filesystem(new Local($this->baseDir));
$this->downloader = new FakeDownloader();
$distStorage = new StorageImpl($this->downloader, $repoFilesystem);
$distStorage = new Storage($this->downloader, $repoFilesystem);
$this->synchronizer = new ComposerPackageSynchronizer(
new PackageManager($distStorage, $repoFilesystem),
new PackageNormalizer(),
Expand Down
Loading