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

fix(api): handle errors better in WebDavClient.php #283

Merged
merged 4 commits into from
Aug 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$adapter = new WebDAVAdapter($sabreClient, $clientOptions['workspace']);
$fileSystem = new Filesystem($adapter);

return new WebDavClient($fileSystem);
$logger = $container->get('Logger');

return new WebDavClient($fileSystem, $logger);
}
}
42 changes: 31 additions & 11 deletions app/api/module/DocumentShare/src/Service/WebDavClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dvsa\Olcs\DocumentShare\Service;

use Dvsa\Olcs\DocumentShare\Data\Object\File;
use Laminas\Log\Logger;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
Expand All @@ -11,11 +12,6 @@ class WebDavClient implements DocumentStoreInterface
{
public const DS_DOWNLOAD_FILE_PREFIX = 'ds_dwnld_';

/**
* @var FilesystemInterface
*/
protected $filesystem;

/**
* @var string
*/
Expand All @@ -31,23 +27,36 @@ class WebDavClient implements DocumentStoreInterface
*
* @param FilesystemInterface $filesystem File System
*/
public function __construct(FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
}
public function __construct(
protected FilesystemInterface $filesystem,
protected Logger $logger
) {}

/**
* Read content from document store
*
* @param string $path Path
* @throws \Exception
*/
public function read($path): File | false
{
$tmpFileName = tempnam(sys_get_temp_dir(), self::DS_DOWNLOAD_FILE_PREFIX);

if ($tmpFileName === false) {
$this->logger->err('Failed to create temp file', ['path' => $path, 'tmpDir' => sys_get_temp_dir()]);
return false;
}

$this->logger->debug('Temp file created', ['tmpFileName' => $tmpFileName, 'is_file' => is_file($tmpFileName), 'is_readable' => is_readable($tmpFileName), 'is_writable' => is_writable($tmpFileName)]);

try {
$readStream = $this->filesystem->readStream($path);
file_put_contents($tmpFileName, $readStream);
$fpc = file_put_contents($tmpFileName, $readStream);

if ($fpc === false) {
$this->logger->err('Failed to write file to temp location', ['path' => $path, 'tmpFileName' => $tmpFileName]);
return false;
}

$file = new File();
$file->setContentFromStream($tmpFileName);
Expand Down Expand Up @@ -97,8 +106,19 @@ public function write($path, File $file)
{
$response = new WebDavResponse();
try {
$this->logger->debug('Opening file for reading', ['file' => $file->getResource(), 'path' => $path]);

$this->logger->debug('File contents', ['contents' => file_get_contents($file->getResource())]);

$fh = fopen($file->getResource(), 'rb');
$response->setResponse($this->filesystem->writeStream($path, $fh));

if ($fh === false) {
$this->logger->err('Failed to open file for reading', ['file' => $file->getResource(), 'path' => $path]);

$response->setResponse(false);
} else {
$response->setResponse($this->filesystem->writeStream($path, $fh));
}
} catch (FileExistsException) {
$response->setResponse(false);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Dvsa\Olcs\DocumentShare\Service\ClientFactory;
use Dvsa\Olcs\DocumentShare\Service\WebDavClient;
use Laminas\Log\Logger;
use Psr\Container\ContainerInterface;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase;
Expand All @@ -30,6 +31,7 @@ public function testInvoke(): void

$mockContainer = m::mock(ContainerInterface::class);
$mockContainer->expects('get')->with('Configuration')->andReturn($config);
$mockContainer->expects('get')->with('Logger')->andReturn($this->createMock(Logger::class));

$service = $sut->__invoke($mockContainer, WebDavClient::class);
$this->assertInstanceOf(WebDavClient::class, $service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setUp(): void
{
$this->mockFileSystem = m::mock(FilesystemInterface::class);

$this->sut = new Client($this->mockFileSystem);
$this->sut = new Client($this->mockFileSystem, $this->createMock(\Laminas\Log\Logger::class));

$this->mockFile = m::mock(DsFile::class);

Expand Down Expand Up @@ -97,7 +97,7 @@ public function testWriteSuccess()

/** @var DsFile $mockFile */
$mockFile = m::mock(DsFile::class)
->shouldReceive('getResource')->once()->andReturn($res)
->shouldReceive('getResource')->andReturn($res)
->getMock();

$this->mockFileSystem->expects('writeStream')->with($expectPath, new IsTypeOf('resource'))->andReturn(true);
Expand All @@ -119,7 +119,7 @@ public function testWriteFail()

/** @var DsFile $mockFile */
$mockFile = m::mock(DsFile::class)
->shouldReceive('getResource')->once()->andReturn($res)
->shouldReceive('getResource')->andReturn($res)
->getMock();

$this->mockFileSystem->expects('writeStream')->with($expectPath, new IsTypeOf('resource'))->andReturn(false);
Expand All @@ -141,7 +141,7 @@ public function testWriteFileAlreadyExists()

/** @var DsFile $mockFile */
$mockFile = m::mock(DsFile::class)
->shouldReceive('getResource')->once()->andReturn($res)
->shouldReceive('getResource')->andReturn($res)
->getMock();

$this->mockFileSystem->expects('writeStream')->with($expectPath, new IsTypeOf('resource'))->andThrow(
Expand Down