File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/SiteConfig/CapterraRatingComposer.php
<?php
namespace App\Komma\SiteConfig;
use App\Komma\SiteConfig\Models\SiteConfig;
use Illuminate\Contracts\View\View;
class CapterraRatingComposer
{
/**
* Bind data to the view.
*
* @param View $view
* @return View
*/
public function compose(View $view)
{
$stars = (object) ['full' => 0, 'half' => 0, 'empty' => 0];
$starCounter = 5;
$capterraStarRatingCounter = (int) config('site.capterraRating');
// Loop till we get a star counter is zero
while ($starCounter != 0) {
// Add full star
if ($capterraStarRatingCounter >= 2) {
$stars->full++;
$capterraStarRatingCounter -= 2;
} // Add half star
elseif ($capterraStarRatingCounter == 1) {
$stars->half++;
$capterraStarRatingCounter--;
} // Add empty start
else {
$stars->empty++;
}
$starCounter--;
}
// get rating logos
$ratingLogos = SiteConfig::where('key', 'rating_logos')->with('documents')->first();
if (isset($ratingLogos->documents)) {
$logoNames = $ratingLogos->documents->pluck('name')->all();
$links = SiteConfig::whereIn('key', $logoNames)->get();
$logoArray = [];
foreach ($ratingLogos->documents as $document) {
$path = str_replace('\\', '/', $document->path);
$url = $name = '';
foreach ($links as $link) {
if ($link->key === $document->name) {
$url = $link->value;
$name = $link->key;
}
}
$logoArray[] = ['image' => $path, 'url' => $url, 'name' => $name];
}
$order = ['capterraLink', 'software-advice', 'getapp', ''];
usort($logoArray, function ($a, $b) use ($order) {
$pos_a = array_search($a['name'], $order);
$pos_b = array_search($b['name'], $order);
return $pos_a - $pos_b;
});
return $view->with(['stars' => $stars, 'ratingLogos' => $logoArray]);
} else {
return $view->with(['stars' => $stars]);
}
}
}