File: D:/HostingSpaces/SBogers10/wingssprayer.komma.pro/app/Composers/FeedbackCompanyComposer.php
<?php
namespace App\Composers;
use Illuminate\Support\Facades\Log;
use Komma\FeedbackCompany\FeedbackCompanyApi;
class FeedbackCompanyComposer
{
public static $score;
public function compose($view)
{
if(!isset(self::$score)) {
try {
$feedbackCompanyApi = new FeedbackCompanyApi();
$feedbackCompanySummary = $feedbackCompanyApi->reviews->summary(10);
$feedbackCompanyStatistics = collect($feedbackCompanySummary->statistics);
$finalScore = $feedbackCompanyStatistics->where('type', 'final_score')->first();
$finalScore->stars = $this->mapScoreIntoStars($finalScore->value, $finalScore->maxScore);
self::$score = $finalScore;
}
catch (\Exception $e) {
Log::alert($e);
}
}
$view->with('feedbackCompany', self::$score);
}
/**
* Map a score into a star rating object
*
* @param int $score
* @param int $maxScore
* @return object
*/
private function mapScoreIntoStars(int $score, int $maxScore) : object
{
$stars = (object)['full' => 0, 'half' => 0, 'empty' => 0];
$starCounter = 5;
// Loop till we get a star counter is zero
while($starCounter != 0)
{
// Add full star
if($score >= 2){
$stars->full++;
$score -= 2;
}
// Add half star
elseif($score == 1)
{
$stars->half++;
$score--;
}
// Add empty start
else{
$stars->empty++;
}
$starCounter--;
}
return $stars;
}
}