Skip to content

Commit

Permalink
fix(api): add status code to content store file uploader log (#280)
Browse files Browse the repository at this point in the history
* fix(api): add `ContentStoreFileUploader` status code to log

* fix(api): add logging to `ContentStoreFileUploader`

* fix(api): fix unit test

* chore: 😫
  • Loading branch information
JoshuaLicense committed Aug 28, 2024
1 parent f9e9916 commit 3209a44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 13 additions & 2 deletions app/api/module/Api/src/Service/File/ContentStoreFileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dvsa\Olcs\DocumentShare\Data\Object\File as ContentStoreFile;
use Dvsa\Olcs\DocumentShare\Service\DocumentStoreInterface;
use Laminas\Http\Response;
use Laminas\Log\Logger;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;

Expand All @@ -16,13 +17,18 @@
*/
class ContentStoreFileUploader implements FileUploaderInterface, FactoryInterface
{
public const ERR_UNABLE_UPLOAD = 'Unable to store uploaded file: %s';
public const ERR_UNABLE_UPLOAD = 'Unable to store uploaded file: %s (HTTP status code: %s)';

/**
* @var DocumentStoreInterface
*/
private $contentStoreClient;

/**
* @var Logger
*/
private $logger;

/**
* Upload file to remote storage
*
Expand All @@ -37,8 +43,12 @@ public function upload($identifier, ContentStoreFile $file)
{
$file->setIdentifier($identifier);

$this->logger->debug(__METHOD__, ['identifier' => $identifier, 'file' => $file]);

$response = $this->write($identifier, $file);

$this->logger->debug(__METHOD__, ['response' => $response]);

if ($response->isSuccess()) {
return $file;
}
Expand All @@ -47,7 +57,7 @@ public function upload($identifier, ContentStoreFile $file)
throw new MimeNotAllowedException();
}

throw new Exception(sprintf(self::ERR_UNABLE_UPLOAD, $response->getBody()));
throw new Exception(sprintf(self::ERR_UNABLE_UPLOAD, $response->getBody(), $response->getStatusCode()));
}

/**
Expand Down Expand Up @@ -89,6 +99,7 @@ private function write($identifier, ContentStoreFile $file)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$this->contentStoreClient = $container->get('ContentStore');
$this->logger = $container->get('Logger');
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Dvsa\Olcs\DocumentShare\Data\Object\File as DsFile;
use Dvsa\Olcs\DocumentShare\Service\WebDavClient as ContentStoreClient;
use Laminas\Http\Response;
use Laminas\Log\Logger;
use Laminas\ServiceManager\ServiceManager;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase;
Expand Down Expand Up @@ -43,6 +44,7 @@ function ($alias, $service) use ($sm) {
);

$sm->setService('ContentStore', $this->mockContentStoreCli);
$sm->setService('Logger', $this->createMock(Logger::class));

static::assertSame($this->sut, $this->sut->__invoke($sm, null));
}
Expand Down Expand Up @@ -97,12 +99,12 @@ public function testUploadFail()
$respBody = 'unit_RespBody';

$this->expectException(Exception::class);
$this->expectExceptionMessage(sprintf(ContentStoreFileUploader::ERR_UNABLE_UPLOAD, $respBody));
$this->expectExceptionMessage(sprintf(ContentStoreFileUploader::ERR_UNABLE_UPLOAD, $respBody, 500));

$response = m::mock(Response::class);
$response
->shouldReceive('isSuccess')->once()->andReturn(false)
->shouldReceive('getStatusCode')->once()->andReturn(Response::STATUS_CODE_500)
->shouldReceive('getStatusCode')->andReturn(Response::STATUS_CODE_500)
->shouldReceive('getBody')->once()->andReturn($respBody);

$this->mockContentStoreCli->shouldReceive('write')
Expand Down

0 comments on commit 3209a44

Please sign in to comment.