public
Last active

Using Exceptions for Code Flow

  • Download Gist
ImageBatchProgram.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
<?php
 
Class Image //Represents a single image file
{
public $type;
public $uri;
 
public function __construct($pathToImage)
{
$this->uri = $pathToImage;
$this->type = getTypeFromPath($pathToImage);
}
public function loadRawData()
{
try
{
return openImageAndReadData($this->uri); //Attempt to read the image data from file
}
catch (FileOpenException $e) //on file error throw a Transform error
{
throw TransformErrorException("Image Could not be opened");
}
}
}
 
//I know this wasn't neccasary but it felt wrong creating multiple transforms and not connecting them
Interface TransformInterface
{
public function validate($image); //This will validate the source image works with the transform
public function process($image); //This handles the actual processing
}
 
Class StacheTransform implements TransformInterface
{
public function validate($image)
{
return ($image->type === "PNG"); //This transform only works on PNGs
}
 
public function process($imageData)
{
try
{
return sendImageToAPI($imageData, "http://mustacheAppender.io"); //Attempt to Stache Image
}
catch (ServiceUnavailableException $e) //If it's a temporary error (e.g. we know the service works) try again
{
sleep(1);
return $this->process($imageData);
}
catch (RateLimitReachedException $e) //If there is no point trying throw an error
{
throw TransformErrorException("Rate Limit Reached");
}
}
}
 
Class MemeTextTransform implements TransformInterface
{
public function validate($image)
{
return (strpos($image->uri, "meme") === false); //You can't Meme a Meme
}
 
public function process($imageData)
{
try
{
return sendImageToAPI($imageData, "http://addMemeText.org?text=I_Has_Cheezburgers"); //Attempt to add text
}
catch (ServiceUnavailableException $e) //Same as before
{
sleep(1);
return $this->process($imageData);
}
catch (ImageRejectedByMemePolice $e) //Different issue similar solution
{
throw TransformErrorException("Image Did not Pass Meme Quality Control");
}
}
}
 
 
Class ImageTransformer //Handles the processing
{
private $images; //Images of the class above
private $failedImages; //Associative arrays representing a failed transform
 
private $transforms; //Array that holds the transforms (pretend it has one of each from above)
 
public function __construct($images, $transformsArr) //Assign the images and transforms and initialize the failed array
{
$this->images = $imagesArr;
$this->failedImages = array();
$this->transforms = $transformsArr;
$this->process(); //Call process
}
 
public function process()
{
foreach ($this->images as $image) { //Cycle through Images
try
{
$imageData = $image->loadRawData();
foreach ($this->transforms as $transform) { //Cycle through transforms
if ($transform->validate($image)) { //Check if they validate the specific transforms test
$imageData = $transform->process($imageData); //transform the image storing it for the next transform to work off of
} else { //in the event the image fails a transforms test (validate()) then throw a transform exception
throw TransformErrorException("Image did not meet transformation's requirements");
}
}
saveNewImage($imageData, $image->uri); //Save the final image on success
}
catch (TransformErrorException $e) //Handle all the different transform exceptions from above
{
$d = (Object) array(
"path" => $image->uri,
"error" => $e->message
);
array_push($this->failedImages, $d); //Store the error for later
}
}
 
foreach ($this->failedImages as $failed) { //Once all transforms are done, let the user know which failed and why
echo "Image " . $failed->path . " failed transforming - Reason: " . $failed->error . "\n";
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.