Skip to content

Instantly share code, notes, and snippets.

@gmanricks
Last active December 15, 2015 05:29
Show Gist options
  • Save gmanricks/5209296 to your computer and use it in GitHub Desktop.
Save gmanricks/5209296 to your computer and use it in GitHub Desktop.
Using Exceptions for Code Flow
<?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";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment