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/SBogers10/rentman.komma.pro/app/Komma/CustomerLogos/CountryLogosComposer.php
<?php

namespace Komma\CustomerLogos;

use Illuminate\Foundation\Composer;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Ixudra\Curl\Facades\Curl;
use Komma\CustomerLogos\Models\CustomerLogo;
use Komma\Kms\Countries\Country;

class CountryLogosComposer
{

    const LOGO_AMOUNT = 10;

    public function compose($view)
    {

        $serverIp = \Request::server('REMOTE_ADDR');

        $logos = null;

        if (isset($serverIp)) {
            $country = $this->getCountryByIP($serverIp);

            if($country){
                $logos = $country->customerLogos()
                    ->with('images')
                    ->get();

                $logos->shuffle();
                $logos = $logos->take(self::LOGO_AMOUNT);
                $logoCount = $logos->count();

                // If there are less logo's then 10 we will fill it with more logo's based upon the language
                if($logoCount != 0 && $logoCount < self::LOGO_AMOUNT){

                    $amountNeeded = self::LOGO_AMOUNT - $logoCount;
                    $backupLogos = $this->getCustomerLogosByLanguage()->take($amountNeeded);

                    if($logoCount <= 3) {

                        $preFiller = $backupLogos->pop();
                        $logos->prepend($preFiller);
                        $logos = $logos->merge($backupLogos);

                    }
                    else{
                        $logos = $logos->merge($backupLogos);
                    }
                }
            }
        }

        // If logos is still null or empty then we get them to old fashioned way
        if(!$logos || (is_array($logos) && sizeof($logos) == 0) || (is_object($logos) && $logos->count() == 0)) $logos = $this->getCustomerLogosByLanguage()->take(self::LOGO_AMOUNT);

        $view->with(['customerLogos' => $logos]);
    }

    private function getCountryByIP($serverIp)
    {

        $response = Curl::to('https://ipinfo.io/' . $serverIp . '/json')
            ->asJson()
            ->get();

        if ($response && isset($response->country)) {

            $countryIso2 = strtoupper(trim($response->country));
            $country = Country::where('iso_2', $countryIso2)->first();

            return $country;
        }else {
            return null;
        }

    }

    private function getCustomerLogosByLanguage()
    {

        $logos = CustomerLogo::with('translation')
            ->with('images')
            ->get();

        $logos->shuffle();

        $homeLogos = [];
        foreach ($logos as $key => $logo) {
            if ( ! isset($logo->translation) || ! $logo->translation->active || ! $logo->translation->show_on_home) {
                continue;
            }
            $homeLogos[] = $logo;
        }

        return $logos = new Collection($homeLogos);
    }
}