namespace World\Earth\Europe\Hungary;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Models\Question;
use App\Models\Solution;
use App\Models\Knowledge;
use Udemy;
use Google;
use StackOverFlow;
use GPT;
class Labodi_David extends People
{
use HasMotivation, HasGoals;
protected $fillable = ['coffee', 'knowledge'];
public function __construct()
{
$this->middleware(['PreventRequestsWithoutCoffee', 'IT_Administrator', 'Programmer']);
}
/**
* Find the solution to a question.
*
* @param Question $question
* @return Solution
*/
public function findSolution(Question $question): Solution
{
if ($this->knowledge->contains($question)) {
return $this->knowledge()->getSolution($question);
}
return $this->learn($question)->getSolution();
}
/**
* Learn about a question and gather knowledge.
*
* @param Question $question
* @return Knowledge
* @throws CoffeeNotFoundException
*/
private function learn(Question $question): Knowledge
{
$knowledge = new Knowledge();
if ($this->hasDrankCoffee()) {
while (!$knowledge->hasInformation()){
$knowledge->collectInformation([
Google::search($question),
StackOverFlow::search($question),
GPT::ask($question)
]);
}
return $this->knowledge()->save($knowledge);
} else {
throw new CoffeeNotFoundException();
}
}
/**
* Get the knowledge relation.
*
* @return BelongsToMany
*/
public function knowledge(): BelongsToMany
{
return $this->belongsToMany(Knowledge::class);
}
/**
* Check if David has drank coffee.
*
* @return bool
*/
private function hasDrankCoffee(): bool
{
return !empty($this->coffee);
}
}