Skip to content

Commit

Permalink
Move all proxy logic to Proxy class (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jul 17, 2020
1 parent b79780a commit ea14b9a
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/Command/ProxySyncReleasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function syncPackages(\SimpleXMLElement $feed): void
list($name, $version) = explode(' ', (string) $item->guid);
if (isset($syncedPackages[$name])) {
$this->lock->refresh();
$proxy->downloadByVersion($name, $version);
$proxy->download($name, $version);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Admin/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function stats(Request $request): Response
*/
public function remove(string $proxy, string $packageName): Response
{
$this->dispatchMessage(new RemoveDist($packageName));
$this->dispatchMessage(new RemoveDist($proxy, $packageName));

$this->addFlash('success', sprintf('Dist files for package %s will be removed.', $packageName));

Expand Down
9 changes: 8 additions & 1 deletion src/Message/Proxy/RemoveDist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

final class RemoveDist
{
private string $proxy;
private string $packageName;

public function __construct(string $packageName)
public function __construct(string $proxy, string $packageName)
{
$this->proxy = $proxy;
$this->packageName = $packageName;
}

public function proxy(): string
{
return $this->proxy;
}

public function packageName(): string
{
return $this->packageName;
Expand Down
10 changes: 5 additions & 5 deletions src/MessageHandler/Proxy/RemoveDistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
namespace Buddy\Repman\MessageHandler\Proxy;

use Buddy\Repman\Message\Proxy\RemoveDist;
use Buddy\Repman\Service\Dist\Storage;
use Buddy\Repman\Service\Proxy\ProxyRegister;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class RemoveDistHandler implements MessageHandlerInterface
{
private Storage $distStorage;
private ProxyRegister $register;

public function __construct(Storage $distStorage)
public function __construct(ProxyRegister $register)
{
$this->distStorage = $distStorage;
$this->register = $register;
}

public function __invoke(RemoveDist $message): void
{
$this->distStorage->remove($message->packageName());
$this->register->getByHost($message->proxy())->removeDist($message->packageName());
}
}
8 changes: 0 additions & 8 deletions src/Service/Dist/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Buddy\Repman\Service\Dist;

use Buddy\Repman\Service\Dist;
use Munus\Collection\GenericList;

interface Storage
{
Expand All @@ -19,11 +18,4 @@ public function download(string $url, Dist $dist, array $headers = []): void;
public function filename(Dist $dist): string;

public function size(Dist $dist): int;

/**
* @return GenericList<string>
*/
public function packages(string $repo): GenericList;

public function remove(string $packageName): void;
}
39 changes: 0 additions & 39 deletions src/Service/Dist/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Buddy\Repman\Service\Dist;
use Buddy\Repman\Service\Dist\Storage;
use Buddy\Repman\Service\Downloader;
use Munus\Collection\GenericList;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class FileStorage implements Storage
Expand Down Expand Up @@ -71,42 +68,6 @@ public function size(Dist $dist): int
return $size === false ? 0 : $size;
}

/**
* @return GenericList<string>
*/
public function packages(string $repo): GenericList
{
$dir = $this->distsDir.'/'.$repo.'/dist';
if (!is_dir($dir)) {
return GenericList::empty();
}

$files = Finder::create()->directories()->sortByName()->depth(1)->ignoreVCS(true)->in($dir);

return GenericList::ofAll(array_map(
fn (SplFileInfo $fileInfo) => $fileInfo->getRelativePathname(),
iterator_to_array($files->getIterator()
)));
}

public function remove(string $packageName): void
{
$dirs = [];
foreach (Finder::create()->directories()->path($packageName)->ignoreVCS(true)->in($this->distsDir) as $dir) {
/* @var SplFileInfo $dir */
$dirs[] = $dir->getPathname();
foreach (Finder::create()->files()->in($dir->getPathname()) as $file) {
/* @var SplFileInfo $file */
@unlink($file->getPathname());
}
}

// can't remove dir in Finder loop, RecursiveDirectoryIterator throws error
foreach ($dirs as $dir) {
@rmdir($dir);
}
}

private function ensureDirExist(string $filename): void
{
$dirname = dirname($filename);
Expand Down
11 changes: 0 additions & 11 deletions src/Service/Dist/Storage/InMemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Buddy\Repman\Service\Dist;
use Buddy\Repman\Service\Dist\Storage;
use Munus\Collection\GenericList;

/**
* @codeCoverageIgnore
Expand Down Expand Up @@ -47,14 +46,4 @@ public function size(Dist $dist): int
{
return 0;
}

public function packages(string $repo): GenericList
{
return GenericList::ofAll($this->dists);
}

public function remove(string $packageName): void
{
// TODO: Implement remove() method.
}
}
11 changes: 10 additions & 1 deletion src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function syncedPackages(): GenericList
return $packages;
}

public function downloadByVersion(string $package, string $version): void
public function download(string $package, string $version): void
{
$lastDist = null;

Expand All @@ -101,6 +101,15 @@ public function downloadByVersion(string $package, string $version): void
}
}

public function removeDist(string $package): void
{
if (mb_strlen($package) === 0) {
throw new \InvalidArgumentException('Empty package name');
}

$this->filesystem->deleteDir(sprintf('%s/dist/%s', $this->name, $package));
}

/**
* @return mixed[]
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Service/Proxy/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function __construct(int $timestamp, $stream)
$this->stream = $stream;
}

public static function fromString(string $string): self
public static function fromString(string $string, string $streamName = 'php://memory'): self
{
$stream = fopen('php://memory', 'r+');
$stream = @fopen($streamName, 'r+');
if ($stream === false) {
throw new \RuntimeException('Failed to open in-memory stream');
throw new \RuntimeException(sprintf('Failed to open %s stream', $streamName));
}
fwrite($stream, $string);
rewind($stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Buddy\Repman\Tests\Integration\MessageHandler\Proxy;

use Buddy\Repman\Message\Proxy\RemoveDist;
use Buddy\Repman\Service\Dist\Storage;
use Buddy\Repman\Service\Proxy\ProxyRegister;
use Buddy\Repman\Tests\Integration\IntegrationTestCase;
use Munus\Collection\GenericList;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -15,11 +15,11 @@ final class RemoveDistHandlerTest extends IntegrationTestCase
public function testRemoveDistByPackageName(): void
{
$this->container()->get(MessageBusInterface::class)->dispatch(
new RemoveDist('some-vendor/some-name')
new RemoveDist('packagist.org', 'some-vendor/some-name')
);

self::assertTrue(GenericList::ofAll(['buddy-works/repman'])->equals(
$this->container()->get(Storage::class)->packages('packagist.org')
$this->container()->get(ProxyRegister::class)->getByHost('packagist.org')->syncedPackages()
));
}
}
31 changes: 19 additions & 12 deletions tests/Unit/Service/Dist/Storage/FileStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Buddy\Repman\Service\Dist\Storage\FileStorage;
use Buddy\Repman\Service\Downloader;
use Buddy\Repman\Tests\Doubles\FakeDownloader;
use Munus\Collection\GenericList;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class FileStorageTest extends TestCase
{
Expand Down Expand Up @@ -86,23 +86,30 @@ public function testHasPackage(): void
)));
}

public function testDistPackageRemove(): void
public function testSize(): void
{
$this->createTempFile($packagePath = $this->basePath.'/packagist.org/dist/buddy-works/repman/0.1.2.0_f0c896.zip');
$this->createTempFile($otherPath = $this->basePath.'/packagist.org/dist/buddy-works/other-package/0.1.2.0_f0c896.zip');

self::assertFileExists($packagePath);
self::assertFileExists($otherPath);

$this->storage->remove('buddy-works/repman');

self::assertFileNotExists($packagePath);
self::assertFileExists($otherPath);
self::assertEquals(7, $this->storage->size(new Dist(
'packagist.org',
'buddy-works/repman',
'0.1.2.0',
'f0c896',
'zip'
)));
}

public function testReturnEmptyPackagesListWhenDirNotExist(): void
public function testThrowHttpNotFoundExceptionWhenFileNotFound(): void
{
self::assertTrue(GenericList::empty()->equals($this->storage->packages('not-exist')));
$this->expectException(NotFoundHttpException::class);

$this->storage->download('https://some.domain/packagist.org/dist/not-found', new Dist(
'packagist.org',
'buddy-works/repman',
'0.1.2.0',
'f0c896',
'zip'
));
}

private function createTempFile(string $path): void
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Service/Proxy/MetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Tests\Unit\Service\Proxy;

use Buddy\Repman\Service\Proxy\Metadata;
use PHPUnit\Framework\TestCase;

final class MetadataTest extends TestCase
{
public function testStreamCheck(): void
{
$this->expectException(\RuntimeException::class);

Metadata::fromString('string', 'php://invalid');
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Service/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,26 @@ public function testDownloadDistWhenNotExists(): void
fclose($distribution->get());
unlink($distPath);
}

public function testDistRemove(): void
{
$distDir = __DIR__.'/../../Resources/packagist.org/dist/';
mkdir($distDir.'vendor/package', 0777, true);
file_put_contents($distDir.'vendor/package/some.zip', 'package-data');

$this->proxy->removeDist('vendor/package');

self::assertFileNotExists($distDir.'vendor/package/some.zip');
self::assertDirectoryNotExists($distDir.'vendor/package');

// test if remove package that not exist does not cause error
$this->proxy->removeDist('vendor/package');
}

public function testPreventRemoveDist(): void
{
$this->expectException(\InvalidArgumentException::class);

$this->proxy->removeDist('');
}
}

0 comments on commit ea14b9a

Please sign in to comment.