Skip to content

Instantly share code, notes, and snippets.

@dejwid
Last active February 28, 2018 09:33
Show Gist options
  • Save dejwid/47825933003657af7e7e3c43dc10f8b7 to your computer and use it in GitHub Desktop.
Save dejwid/47825933003657af7e7e3c43dc10f8b7 to your computer and use it in GitHub Desktop.
Letters class example with exceptions and api message returns
<?php
class Letters{
private function getLetter($letter)
{
// get a ketter from the database
// bla bla blatriming, and geting for db
$letter = getFromDB(trim($letter));
if (!$letter) throw new \LetterException('Letter "'.$letter.'" was not found in the database');
return $letter;
}
private function lettersPlay(..$letters)
{
$dominoFinished = playDomino($ltters);
if (!$dominoFinished) throw new \DominoException('Letters cant play domino');
}
public function playAction()
{
try {
$a = getLetter('a');
$b = getLetter('b');
$c = getLetter('c');
$d = getLetter('d');
$e = getLetter('e');
$f = getLetter('f');
$this->lettersPlay($a, $b, $c, $d, $e, $f);
$this->response('Play was successful');
} catch (\LetterException $e) {
$this->response('Dear user. We had a problem findin a letter. ' . $e->getMessage());
} catch (\DominoException $e) {
$this->response('Dear user. Domino play wan\'t successful' . $e->getMessage());
} catch (\Exception $e) {
$this->response('Dear user. Unexpected error occured: ' . $e->getMessage());
}
}
}
<?php
class Letters{
public function playAction()
{
$a = getLetter('a');
if (!$a) {
$this->response('Dear user. Letter a was not found');
}
$b = getLetter('b');
if (!$b) {
$this->response('Dear user. Letter b was not found');
}
$c = getLetter('c');
if (!$c) {
$this->response('Dear user. Letter c was not found');
}
$d = getLetter('d');
if (!$d) {
$this->response('Dear user. Letter d was not found');
}
$e = getLetter('e');
if (!$e) {
$this->response('Dear user. Letter e was not found');
}
$f = getLetter('f');
if (!$f) {
$this->response('Dear user. Letter f was not found');
}
$gameResult = $this->lettersPlay($a, $b, $c, $d, $e, $f);
if (!$gameResult) {
$this->response('Dear user. game failed');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment