Skip to content

Instantly share code, notes, and snippets.

@juniorb2ss
Last active May 23, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniorb2ss/eff1c1f6b0abb3c4edf52cdf4c22480a to your computer and use it in GitHub Desktop.
Save juniorb2ss/eff1c1f6b0abb3c4edf52cdf4c22480a to your computer and use it in GitHub Desktop.
<?php
$studentFirstPoints = $_GET['a'] ?? null;
$studentSecondPoints = $_GET['b'] ?? null;
try {
$studentPointsCalculator = new StudentPointsCalculator($studentFirstPoints, $studentSecondPoints);
$studentPointsCalculator->studentWasApproved();
echo 'Você foi aprovado!';
} catch (StudentCalculatorException $exception) {
echo $exception->getMessage();
}
class StudentCalculatorException extends Exception {}
class StudentPointWasNotAvailable extends StudentCalculatorException
{
public function __construct()
{
parent::__construct(StudentPointsCalculator::STUDENT_POINTS_WAS_NOT_AVALIABLE);
}
}
class StudentWasReproved extends StudentCalculatorException
{
public function __construct()
{
parent::__construct(StudentPointsCalculator::STUDENT_WAS_REPROVED);
}
}
class StudentPointsCalculator
{
const STUDENT_WAS_REPROVED = 'Voce foi reprovado!';
const STUDENT_POINTS_WAS_NOT_AVALIABLE = 'Nota indisponível.';
const MIN_FINAL_POINTS_TO_BE_APPROVED = 10;
const MAX_FINAL_POINTS_TO_BE_APPROVED = 20;
/**
* @var int
*/
private $studentFirstPoints;
/**
* @var int
*/
private $studentSecondPoints;
/**
* @var int
*/
private $finalPoints;
/**
* Student constructor.
* @param int|null $studentFirstPoints
* @param int|null $studentSecondPoints
*/
function __construct(?int $studentFirstPoints, ?int $studentSecondPoints)
{
$this->studentFirstPoints = $studentFirstPoints;
$this->studentSecondPoints = $studentSecondPoints;
$this->finalPoints = ($this->studentFirstPoints + $this->studentSecondPoints);
}
/**
* @return bool
* @throws StudentPointWasNotAvailable
*/
private function studentFinalPointsWasAvailable(): bool
{
if(is_null($this->studentFirstPoints) || is_null($this->studentSecondPoints)) {
throw new StudentPointWasNotAvailable();
}
return true;
}
/**
* @return bool
* @throws StudentWasReproved
* @throws StudentPointWasNotAvailable
*/
public function studentWasApproved(): bool
{
$this->studentFinalPointsWasAvailable();
if($this->finalPoints <= self::MIN_FINAL_POINTS_TO_BE_APPROVED && $this->finalPoints >= self::MAX_FINAL_POINTS_TO_BE_APPROVED) {
throw new StudentWasReproved();
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment