Skip to content

Commit

Permalink
🥅 Throw exception when image_id is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Nov 7, 2021
1 parent 6bde051 commit aedf47f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Exceptions/PropertyDoesNotExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MarcReichel\IGDBLaravel\Exceptions;

use Exception;

class PropertyDoesNotExist extends Exception
{
//
}
12 changes: 11 additions & 1 deletion src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use MarcReichel\IGDBLaravel\Enums\Image\Size;
use MarcReichel\IGDBLaravel\Exceptions\PropertyDoesNotExist;
use ReflectionClass;

abstract class Image extends Model
{
protected const IMAGE_BASE_PATH = '//images.igdb.com/igdb/image/upload';

/**
* @throws PropertyDoesNotExist|InvalidArgumentException
*/
public function getUrl(string $size = 'thumb', bool $retina = false): string
{
$availableSizes = new ReflectionClass(Size::class);
Expand All @@ -24,7 +28,13 @@ public function getUrl(string $size = 'thumb', bool $retina = false): string
}

$basePath = static::IMAGE_BASE_PATH;
$id = '' . $this->getAttribute('image_id');
$id = $this->getAttribute('image_id');

if (is_null($id)) {
throw new PropertyDoesNotExist('Property [image_id] is missing from the response. Make sure you specify `image_id` inside the fields attribute.');
}

$id = '' . $id;

if ($retina) {
$size = Str::finish('' . $sizeFromEnum, '_2x');
Expand Down

0 comments on commit aedf47f

Please sign in to comment.