Add exception handling while fixing typo

parent f24c5ecb
......@@ -39,56 +39,77 @@ abstract class TyposClientInterface
* Fixes a typo in an article from a $link url. Uses a context while
* fixing to determine a typo position.
*
* @param string $typo Typo to be fixed
* @param string $corrected Correct variant
* @param string $context Context of typo
* @param string $link Link where the typo exist
* @param string $typo Typo to be fixed
* @param string $corrected Correct variant
* @param string $context Context of typo
* @param string $link Link where the typo exist
*
* @return array Array contains error code and optional message
*/
public function fixTypo(string $typo, string $corrected, string $context, string $link) {
$response = [
"errorCode" => 200,
"message" => "success",
];
try {
$article = $this->getArticleFromLink($link);
$this->replaceTypoInArticle($typo, $corrected, $context, $article);
$this->saveArticle($article);
} catch(\Exception $e) {
return ["status" => "error", "message" => $e->getMessage()];
} catch (\Exception $e) {
$response["errorCode"] = $e->getCode();
$response["message"] = $e->getMessage();
return $response;
}
return ["status" => "success"];
return $response;
}
/**
* This method replaces a given typo in article, using the context to a correct
* variant.
*
* @param string $typo Typo to be replaced
* @param string $corrected Correct variant
* @param string $context Context where the typo found
* @param string $typo Typo to be replaced
* @param string $corrected Correct variant
* @param string $context Context where the typo found
* @param TyposArticle $article Article to fix the typo
*
* @throws \Exception
* 404 - Typo does not exist
* 405 - Context has been changed
*/
public function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) {
public function replaceTypoInArticle(string $typo, string $corrected, string $context, TyposArticle $article) {
// Strip all tags from text
$text = strip_tags($article->text);
$context = preg_quote($context);
$typo = preg_quote($typo);
// Find all typos in text, capture an offset of each typo
$typos = [];
preg_match_all("#{$typo}#", $text, $typos, PREG_OFFSET_CAPTURE);
$typos = $typos[0];
if (!isset($typos[0])) {
// Check for already fixed typo
preg_match_all("#{$corrected}#", $text, $typos, PREG_OFFSET_CAPTURE);
if (isset($typos[0])) {
throw new \Exception("Already fixed", 208);
}
throw new \Exception("Typo not found", 404);
}
// Find a context in text, capture it offset
$contextMatch = [];
preg_match_all("#{$context}#", $text, $contextMatch, PREG_OFFSET_CAPTURE);
$contextMatch = $contextMatch[0];
// If a context was changed then report an error,
// cannot locate typo in a new context, must be
// fixed manually
if (!isset($contextMatch[0])) {
throw new \Exception("Failed to find the context in article");
throw new \Exception("Context not found", 405);
}
$contextMatch = $contextMatch[0];
$contextOffset = $contextMatch[0][1];
// Find a concrete typo that we want to fix
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment