Skip to content

Commit

Permalink
Add types to the image variation storage interface
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Jul 16, 2020
1 parent cf1dd79 commit 9f3e802
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 87 deletions.
17 changes: 7 additions & 10 deletions src/EventListener/ImageVariations/Storage/Filesystem.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Imbo\EventListener\ImageVariations\Storage;

use Imbo\Exception\StorageException;
Expand All @@ -17,7 +17,7 @@ class Filesystem implements StorageInterface {
*
* @var array
*/
private $params = [
private array $params = [
'dataDir' => null,
];

Expand All @@ -32,10 +32,7 @@ public function __construct(array $params = null) {
}
}

/**
* {@inheritdoc}
*/
public function storeImageVariation($user, $imageIdentifier, $blob, $width) {
public function storeImageVariation(string $user, string $imageIdentifier, string $blob, int $width) : bool {
if (!is_writable($this->params['dataDir'])) {
throw new StorageException('Could not store image variation (directory not writable)', 500);
}
Expand All @@ -57,7 +54,7 @@ public function storeImageVariation($user, $imageIdentifier, $blob, $width) {
/**
* {@inheritdoc}
*/
public function getImageVariation($user, $imageIdentifier, $width) {
public function getImageVariation(string $user, string $imageIdentifier, int $width) : ?string {
$variationPath = $this->getImagePath($user, $imageIdentifier, $width);

if (file_exists($variationPath)) {
Expand All @@ -70,7 +67,7 @@ public function getImageVariation($user, $imageIdentifier, $width) {
/**
* {@inheritdoc}
*/
public function deleteImageVariations($user, $imageIdentifier, $width = null) {
public function deleteImageVariations(string $user, string $imageIdentifier, int $width = null) : bool {
// If width is specified, delete only the specific image
if ($width !== null) {
return unlink($this->getImagePath($user, $imageIdentifier, $width, true));
Expand All @@ -92,11 +89,11 @@ public function deleteImageVariations($user, $imageIdentifier, $width = null) {
* @param string $user The user which the image belongs to
* @param string $imageIdentifier Image identifier
* @param int $width Width of the image, in pixels
* @param boolean $includeFilename Whether or not to include the last part of the path
* @param bool $includeFilename Whether or not to include the last part of the path
* (the filename itself)
* @return string
*/
private function getImagePath($user, $imageIdentifier, $width = null, $includeFilename = true) {
private function getImagePath(string $user, string $imageIdentifier, int $width = null, bool $includeFilename = true) : string {
$userPath = str_pad($user, 3, '0', STR_PAD_LEFT);
$parts = [
$this->params['dataDir'],
Expand Down
46 changes: 12 additions & 34 deletions src/EventListener/ImageVariations/Storage/GridFS.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
<?php declare(strict_types=1);
namespace Imbo\EventListener\ImageVariations\Storage;

use Imbo\Exception\StorageException;
use MongoDB\Client;
use MongoDB\GridFS\Database;
use MongoDB\Database;
use MongoDB\GridFS\Bucket;
use MongoDB\Driver\Exception\Exception as MongoDBException;

Expand All @@ -20,27 +20,16 @@
* - `array bucketOptions`: Options for the internal Bucket instance. Defaults to []
*/
class GridFS implements StorageInterface {
/**
* @var Client
*/
private $mongoClient;

/**
* @var Database
*/
private $database;

/**
* @var Bucket
*/
private $bucket;
private Client $client;
private Database $database;
private Bucket $bucket;

/**
* Parameters for the driver
*
* @var array
*/
private $params = [
private array $params = [
'uri' => 'mongodb://localhost:27017',
'uriOptions' => [],
'clientOptions' => [
Expand All @@ -55,8 +44,6 @@ class GridFS implements StorageInterface {
* Class constructor
*
* @param array $params Parameters for the driver
* @param MongoClient $client Mongo client instance
* @param MongoGridFS $grid MongoGridFS instance
*/
public function __construct(array $params = null) {
if ($params !== null) {
Expand All @@ -77,10 +64,7 @@ public function __construct(array $params = null) {
$this->bucket = $this->database->selectGridFSBucket($this->params['bucketOptions']);
}

/**
* {@inheritdoc}
*/
public function storeImageVariation($user, $imageIdentifier, $blob, $width) {
public function storeImageVariation(string $user, string $imageIdentifier, string $blob, int $width) : bool {
$this->bucket->uploadFromStream(
$this->getImageFilename($user, $imageIdentifier, (int) $width),
$this->createStream($blob),
Expand All @@ -97,10 +81,7 @@ public function storeImageVariation($user, $imageIdentifier, $blob, $width) {
return true;
}

/**
* {@inheritdoc}
*/
public function getImageVariation($user, $imageIdentifier, $width) {
public function getImageVariation(string $user, string $imageIdentifier, int $width) : ?string {
try {
return stream_get_contents($this->bucket->openDownloadStreamByName(
$this->getImageFilename($user, $imageIdentifier, (int) $width)
Expand All @@ -110,10 +91,7 @@ public function getImageVariation($user, $imageIdentifier, $width) {
}
}

/**
* {@inheritdoc}
*/
public function deleteImageVariations($user, $imageIdentifier, $width = null) {
public function deleteImageVariations(string $user, string $imageIdentifier, int $width = null) : bool {
$filter = [
'metadata.user' => $user,
'metadata.imageIdentifier' => $imageIdentifier
Expand All @@ -136,7 +114,7 @@ public function deleteImageVariations($user, $imageIdentifier, $width = null) {
* @param string $data The string to use in the stream
* @return resource
*/
private function createStream($data) {
private function createStream(string $data) {
$stream = fopen('php://temp', 'w+b');
fwrite($stream, $data);
rewind($stream);
Expand All @@ -152,7 +130,7 @@ private function createStream($data) {
* @param int $width
* @return string
*/
private function getImageFilename($user, $imageIdentifier, $width) {
return sprintf('%s.%s.%d', $user, $imageIdentifier, (int) $width);
private function getImageFilename(string $user, string $imageIdentifier, int $width) : string {
return $user . '.' . $imageIdentifier . '.' . $width;
}
}
40 changes: 9 additions & 31 deletions src/EventListener/ImageVariations/Storage/S3.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php
<?php declare(strict_types=1);
namespace Imbo\EventListener\ImageVariations\Storage;

use Imbo\Exception\StorageException;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use DateTime;
use DateTimeZone;

/**
* S3 storage driver for the image variations
Expand All @@ -20,19 +18,8 @@
* you.
*/
class S3 implements StorageInterface {
/**
* S3 client
*
* @var S3Client
*/
private $client;

/**
* Parameters for the driver
*
* @var array
*/
private $params = [
private S3Client $client;
private array $params = [
// Access key
'key' => null,

Expand Down Expand Up @@ -65,10 +52,7 @@ public function __construct(array $params = null, S3Client $client = null) {
}
}

/**
* {@inheritdoc}
*/
public function storeImageVariation($user, $imageIdentifier, $imageData, $width) {
public function storeImageVariation(string $user, string $imageIdentifier, string $imageData, int $width) : bool {
try {
$this->getClient()->putObject([
'Bucket' => $this->params['bucket'],
Expand All @@ -82,10 +66,7 @@ public function storeImageVariation($user, $imageIdentifier, $imageData, $width)
return true;
}

/**
* {@inheritdoc}
*/
public function getImageVariation($user, $imageIdentifier, $width) {
public function getImageVariation(string $user, string $imageIdentifier, int $width) : ?string {
try {
$model = $this->getClient()->getObject([
'Bucket' => $this->params['bucket'],
Expand All @@ -98,10 +79,7 @@ public function getImageVariation($user, $imageIdentifier, $width) {
return (string) $model->get('Body');
}

/**
* {@inheritdoc}
*/
public function deleteImageVariations($user, $imageIdentifier, $width = null) {
public function deleteImageVariations(string $user, string $imageIdentifier, int $width = null) : bool {
// If width is specified, delete only the specific image
if ($width !== null) {
$this->getClient()->deleteObject([
Expand Down Expand Up @@ -138,11 +116,11 @@ public function deleteImageVariations($user, $imageIdentifier, $width = null) {
* @param string $user The user which the image belongs to
* @param string $imageIdentifier Image identifier
* @param int $width Width of the image, in pixels
* @param boolean $includeFilename Whether or not to include the last part of the path
* @param bool $includeFilename Whether or not to include the last part of the path
* (the filename itself)
* @return string
*/
private function getImagePath($user, $imageIdentifier, $width = null, $includeFilename = true) {
private function getImagePath(string $user, string $imageIdentifier, int $width = null, bool $includeFilename = true) : string {
$userPath = str_pad($user, 3, '0', STR_PAD_LEFT);
$parts = [
'imageVariation',
Expand All @@ -169,7 +147,7 @@ private function getImagePath($user, $imageIdentifier, $width = null, $includeFi
*
* @return S3Client
*/
private function getClient() {
private function getClient() : S3Client {
if ($this->client === null) {
$params = [
'credentials' => [
Expand Down
20 changes: 10 additions & 10 deletions src/EventListener/ImageVariations/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Imbo\EventListener\ImageVariations\Storage;

/**
Expand All @@ -9,30 +9,30 @@ interface StorageInterface {
* Store an image variation
*
* @param string $user The user which the image belongs to
* @param string $imageIdentifier The image identifier of the original
* @param string $imageIdentifier The image identifier of the original image
* @param string $blob The image blob to store
* @param int $width The width of the variation
* @return boolean
* @return bool
*/
function storeImageVariation($user, $imageIdentifier, $blob, $width);
function storeImageVariation(string $user, string $imageIdentifier, string $blob, int $width) : bool;

/**
* Get the blob of an image variation
*
* @param string $user The user which the image belongs to
* @param string $imageIdentifier The image identifier of the original
* @param string $imageIdentifier The image identifier of the original image
* @param int $width The width of the variation
* @return string
* @return ?string
*/
function getImageVariation($user, $imageIdentifier, $width);
function getImageVariation(string $user, string $imageIdentifier, int $width) : ?string;

/**
* Remove an image variation
*
* @param string $user The user which the image belongs to
* @param string $imageIdentifier The image identifier
* @param string $imageIdentifier The image identifier of the original image
* @param int $width Only delete the variation with this width
* @return boolean
* @return bool
*/
function deleteImageVariations($user, $imageIdentifier, $width = null);
function deleteImageVariations(string $user, string $imageIdentifier, int $width = null) : bool;
}

0 comments on commit 9f3e802

Please sign in to comment.