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/topswtw.komma.pro/workbench/komma/kms/src/Komma/Kms/Core/Kms.php
<?php
/**
 * Short description for the file.
 *
 * @author      Jaap Faes <jaap@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Kms\Core;

use Carbon\Carbon;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Cache;
use Komma\Kms\Languages\Language;
use Komma\Kms\Shops\ShopsRepository;

class Kms
{
    protected $shop;

    protected $router;
    protected $shopsRepository;

    protected $shops;

    protected $shopLanguageIdIndex;
    protected $shopLanguageIndex;

    function __construct(Router $router, ShopsRepository $shopsRepository)
    {
        $this->router = $router;
        $this->shopsRepository = $shopsRepository;

        $shopSlug = $this->router->current()->getParameter('shop');

        $this->shop = $this->shopsRepository->findBySlug($shopSlug);
    }

    public function getDefaultLanguageId()
    {
        if($this->shop) {
            return $this->shop->default_language_id;
        }
        return \Config::get('kms::main.defaultLanguageId');
    }

    public function getShopDefaultLanguageId($shopId)
    {
        return $this->getShop($shopId)->default_language_id;
    }

    public function getDefaultLanguage()
    {
        return $this->getCurrentShopLanguages()->find($this->getDefaultLanguageId());
    }

    public function getCurrentShopId()
    {
        if($this->shop) {
            return $this->shop->id;
        }
        return null;
    }

    public function getCurrentShopSlug()
    {
        if($this->shop) {
            return $this->shop->slug;
        }
        return null;
    }

    public function getShops()
    {
        if(! $this->shops)
            $this->shops = $this->shopsRepository->findAll()->keyBy('id');

        return $this->shops;
    }

    public function getShop($shopId)
    {
        return $this->shopsRepository->find($shopId);
    }

    public function getCurrentShopLanguages()
    {
        if(!$this->shop) return false;
        return $this->getShopLanguages($this->shop->id);
    }

    public function getCurrentShopLanguageIds()
    {
        return $this->getShopLanguageIds($this->shop->id);
    }

    public function getShopLanguageIds($shopId)
    {
        if(!isset($this->shopLanguageIdIndex[$shopId])) {
            $this->shopLanguageIdIndex[$shopId] = \DB::table('shop_language')->where('shop_id', $shopId)->lists('language_id');
        }
        return $this->shopLanguageIdIndex[$shopId];
    }

    public function getShopLanguages($shopId)
    {
        $cacheKey = 'shopLanguageByShopId|'.$shopId;
        if(!Cache::has($cacheKey)) {
            $languages = \DB::table('shop_language')
                ->join('languages', 'languages.id', '=', 'shop_language.language_id')
                ->where('shop_language.shop_id', $shopId)
                ->get();
            Cache::put($cacheKey, $languages, Carbon::now()->addMinutes(10));
        } else {
            $languages = Cache::get($cacheKey);
        }
        return $languages;
    }

    public function getLanguageById($languageId)
    {
        $cacheKey = "languageById|".$languageId;
        if(!Cache::has($cacheKey))
        {
            $language = Language::find($languageId);
            Cache::put($cacheKey, $language, Carbon::now()->addMinutes(10));
        } else {
            $language = Cache::get($cacheKey);
        }

        return $language;
    }

}