HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
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]);
        }
    }
}