Skip to content

Instantly share code, notes, and snippets.

@DaRaFF
Last active December 22, 2015 03:58
Show Gist options
  • Save DaRaFF/6413591 to your computer and use it in GitHub Desktop.
Save DaRaFF/6413591 to your computer and use it in GitHub Desktop.
Example that the rule "return early as possible" leads to more readable code The original code has been taken from: https://github.com/nzzdev/epaper/blob/7d0488f6fd7f716fb2b73ebd0450a8f1780b4c97/src/NZZ/EpaperBundle/Import/IssueUploader.php#L32-L56
<?php
public function upload($sourceFileName, $amazonS3IsAclPrivate = false)
{
$metadata = array();
$uploadPath = $this->calculateUploadPath($sourceFileName);
if ($this->sameFileWithSameHashExists($uploadPath, $sourceFileName)) {
$this->logger->info("File " . $uploadPath . " with same hash already exists. File will not be uploaded.");
return $uploadpath;
}
$size = $this->s3Adapter->create($uploadPath, file_get_contents($sourceFileName), $metadata);
if ($size > 0) {
$this->logger->info("File " . $uploadPath . " has been uploaded to amazon S3 successfully.");
return $uploadpath;
}
$this->logger->error("File " . $uploadPath . " has not been uploaded to amazon S3!");
return false;
}
<?php
public function upload($sourceFileName, $amazonS3IsAclPrivate = false)
{
$metadata = array();
$uploadPath = $this->calculateUploadPath($sourceFileName);
if ($this->sameFileWithSameHashExists($uploadPath, $sourceFileName)) {
$this->logger->info("File " . $uploadPath . " with same hash already exists. File will not be uploaded.");
} else {
$size = $this->s3Adapter->create($uploadPath, file_get_contents($sourceFileName), $metadata);
if ($size > 0) {
$this->logger->info("File " . $uploadPath . " has been uploaded to amazon S3 successfully.");
} else {
$uploadPath = false;
$this->logger->error("File " . $uploadPath . " has not been uploaded to amazon S3!");
}
}
return $uploadPath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment