Last active
February 28, 2018 09:33
-
-
Save dejwid/47825933003657af7e7e3c43dc10f8b7 to your computer and use it in GitHub Desktop.
Letters class example with exceptions and api message returns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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