File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/FeedbackCompany/ScoreComposer.php
<?php
namespace App\KommaApp\FeedbackCompany;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
class ScoreComposer
{
static $scoreValue;
static $maxScore = 10;
static $reviews = 182;
private $feedbackCompanyService;
public function __construct(FeedbackCompanyService $feedbackCompanyService)
{
$this->feedbackCompanyService = $feedbackCompanyService;
}
public function compose(View $view)
{
// Only load the feedback company score once
if (!static::$scoreValue) {
// Get Feedback company score
if ( ! $feedbackCompanyScore = $this->feedbackCompanyService->getFeedbackCompanyScore()) {
// Fallback for when their service is unavailable
$this->getScoreFromDatabase();
}else {
// Set score and other available scores if isset
if(isset( $feedbackCompanyScore->value )) {
static::$scoreValue = $feedbackCompanyScore->value;
if(isset( $feedbackCompanyScore->maxscore ))static::$maxScore = $feedbackCompanyScore->maxscore;
if(isset( $feedbackCompanyScore->count ))static::$reviews = $feedbackCompanyScore->count;
$this->updateScoreInDatabase();
}
else{
// Fallback for when their service is return invalid response
$this->getScoreFromDatabase();
}
}
}
$score = (object)[
'value' => static::$scoreValue,
'maxValue' => static::$maxScore,
'reviews' => static::$reviews,
'stars' => ''
];
$roundedValue = floor($score->value);
$starAmount = 0;
while ($starAmount < 5) {
if ($roundedValue >= 2) {
$score->stars .= '<span class="full-star star-score"></span>';
$roundedValue -= 2;
}elseif ($roundedValue == 1) {
$score->stars .= '<span class="half-star star-score"><span class="inner-star"></span></span>';
$roundedValue -= 1;
}else {
$score->stars .= '<span class="empty-star star-score"></span>';
}
$starAmount++;
}
$view->with('score', $score);
}
private function getScoreFromDatabase(){
$dbScore = \App::getSetting('feedbackCompany');
$scoreValue = floatval($dbScore);
static::$scoreValue = $scoreValue;
}
private function updateScoreInDatabase(){
\DB::table('settings')
->where('name', 'feedbackCompany')
->update(['value' => static::$scoreValue]);
}
}