Skip to content

Commit

Permalink
Add types to the interface / implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Aug 30, 2021
1 parent 33d909f commit acaf8e1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
12 changes: 6 additions & 6 deletions src/EventListener/ImageVariations/Database/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ interface DatabaseInterface {
* @param int $width The width of the variation
* @param int $height The height of the variation
* @throws DatabaseException
* @return boolean
* @return bool
*/
function storeImageVariationMetadata($user, $imageIdentifier, $width, $height);
function storeImageVariationMetadata(string $user, string $imageIdentifier, int $width, int $height): bool;

/**
* Fetch the best match of an image
*
* @param string $user The user which the image belongs to
* @param string $imageIdentifier The image identifier of the original
* @param int $width The width we want to resize the image to
* @return int|null Returns the closest width, or null
* @return ?array{width:int,height:int} Returns the closest width, or null
*/
function getBestMatch($user, $imageIdentifier, $width);
function getBestMatch(string $user, string $imageIdentifier, int $width): ?array;

/**
* Remove all metadata about image variations for an image
*
* @param string $user The user which the image belongs to
* @param string $imageIdentifier The image identifier
* @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;
}
19 changes: 5 additions & 14 deletions src/EventListener/ImageVariations/Database/Doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ public function __construct(array $params) {
$this->params = array_merge($this->params, $params);
}

/**
* {@inheritdoc}
*/
public function storeImageVariationMetadata($user, $imageIdentifier, $width, $height) {
return (boolean) $this->getConnection()->insert($this->params['tableName'], [
public function storeImageVariationMetadata(string $user, string $imageIdentifier, int $width, int $height): bool {
return (bool) $this->getConnection()->insert($this->params['tableName'], [
'added' => time(),
'user' => $user,
'imageIdentifier' => $imageIdentifier,
Expand All @@ -59,10 +56,7 @@ public function storeImageVariationMetadata($user, $imageIdentifier, $width, $he
]);
}

/**
* {@inheritdoc}
*/
public function getBestMatch($user, $imageIdentifier, $width) {
public function getBestMatch(string $user, string $imageIdentifier, int $width): ?array {
$qb = $this->getConnection()->createQueryBuilder();
$qb->select('width', 'height')
->from($this->params['tableName'], 'iv')
Expand All @@ -83,10 +77,7 @@ public function getBestMatch($user, $imageIdentifier, $width) {
return $row ? array_map('intval', $row) : null;
}

/**
* {@inheritdoc}
*/
public function deleteImageVariations($user, $imageIdentifier, $width = null) {
public function deleteImageVariations(string $user, string $imageIdentifier, int $width = null): bool {
$qb = $this->getConnection()->createQueryBuilder();

$qb->delete($this->params['tableName'])
Expand All @@ -102,7 +93,7 @@ public function deleteImageVariations($user, $imageIdentifier, $width = null) {
->setParameter(':width', $width);
}

return (boolean) $qb->execute();
return (bool) $qb->execute();
}

/**
Expand Down
15 changes: 3 additions & 12 deletions src/EventListener/ImageVariations/Database/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function __construct(array $params = null, MongoClient $client = null, Mo
}
}

/**
* {@inheritdoc}
*/
public function storeImageVariationMetadata($user, $imageIdentifier, $width, $height) {
public function storeImageVariationMetadata(string $user, string $imageIdentifier, int $width, int $height): bool {
try {
$this->getCollection()->insertOne([
'added' => time(),
Expand All @@ -86,10 +83,7 @@ public function storeImageVariationMetadata($user, $imageIdentifier, $width, $he
return true;
}

/**
* {@inheritdoc}
*/
public function getBestMatch($user, $imageIdentifier, $width) {
public function getBestMatch(string $user, string $imageIdentifier, int $width): ?array {
$query = [
'user' => $user,
'imageIdentifier' => $imageIdentifier,
Expand All @@ -113,10 +107,7 @@ public function getBestMatch($user, $imageIdentifier, $width) {
return $result ? $result->getArrayCopy() : null;
}

/**
* {@inheritdoc}
*/
public function deleteImageVariations($user, $imageIdentifier, $width = null) {
public function deleteImageVariations(string $user, string $imageIdentifier, int $width = null): bool {
$query = [
'user' => $user,
'imageIdentifier' => $imageIdentifier,
Expand Down

0 comments on commit acaf8e1

Please sign in to comment.