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/Http/Controllers/Controller.php
<?php

namespace App\Http\Controllers;

use App\Komma\Integrations\IntegrationService;
use App\Komma\OneSky\OneSkyExporterService;
use App\Komma\Pages\PageService;
use App\Komma\Products\ProductService;
use App\Komma\Routes\Models\Route;
use App\Komma\Sites\Models\Site;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Solutions\SolutionService;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Str;
use Jenssegers\Agent\Agent;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $links;

    protected $pageService;

    protected $solutionService;

    protected $productService;

    protected $integrationService;

    protected $oneSkyExporterService;

    protected $baseViewPath = 'site.';

    /** @var SiteServiceInterface */
    private $siteService;

    /** @var Site */
    protected $site;

    public function __construct()
    {
        if (\App::runningInConsole()) {
            return;
        }

        $this->preventRestRouteFromWorking();

        // Prepare the needed service for the Links object
        // We make them protected so a child could also access them
        $this->pageService = new PageService();
        $this->productService = new ProductService();
        $this->solutionService = new SolutionService();
        $this->integrationService = new IntegrationService();
        $this->oneSkyExporterService = new OneSkyExporterService();

        // Make link object
        // TODO: Make a own class or service for this, now it's splintered around the services
        $this->links = $this->makeLinksObject();

        $this->siteService = \App::make(SiteServiceInterface::class);
        $this->site = $this->siteService->getCurrentSite();

        $agent = new Agent();
        \View::share('agent', $agent);
        \View::share('isLighthouse', Str::contains($agent->getUserAgent(), 'Chrome-Lighthouse'));

        if (! $this->site) {
            throw new \RuntimeException("Site isn't defined something is wrong...");
        }
    }

    // TODO: Add this to the linker / Links class
    protected function keepTrackOfPagination(string $modelPaginationKey, $paginationKey = 'page')
    {
        // Create object for pagination
        $pagination = (object) [
            'key'  => $paginationKey,
            'page' => \Input::get($paginationKey, 1),
        ];

        // Store and save session
        session([$modelPaginationKey => $pagination]);
        session()->save();
    }

    protected function createPreviousRoute(string $modelPaginationKey, string $route)
    {
        // If session is null or on the first page, we need the normal route
        if (! $pagination = session($modelPaginationKey, null)) {
            return $route;
        }
        if ($pagination->page == 1) {
            return $route;
        }

        return $route.'?'.$pagination->key.'='.$pagination->page;
    }

    protected function checkIfModelShouldThrowAbort($model, $relation = BelongsTo::class)
    {
        // If there isn't a translation abort (shouldn't be possible anyway)
        if (! isset($model->translation)) {
            \App::abort(404);
        }

        switch ($relation) {
            case BelongsTo::class:
                // This checks if the product belongs to the set site
                if ($model->site_id != $this->site->id) {
                    abort(404);
                }
                break;

            case BelongsToMany::class:
                if (! $model->sites->contains($this->site)) {
                    \App::abort(404);
                }
                break;
        }

        // If the post translation is marked as inactive and preview is also off, throw 404
        if (! $model->translation->active && ! $model->translation->preview) {
            \App::abort(404);
        }
    }

    /**
     * This method will prevent that REST routes are working with the application
     * They should be resolved by the middleware
     */
    private function preventRestRouteFromWorking()
    {
        if (\App::runningInConsole()) {
            return;
        }

        $request = request();

        // Normally it should be resolved or it's a hard defined route
        // Example. contact/process
        if (isset($request->resolved) && $request->resolved) {
            return;
        } else {
            // Get segments and path
            $path = \Request::path();
            $segments = \Request::segments();

            // Check if current path is a restfull path, or the first segment of it is
            $restRoutes = Route::whereIn('route', [$path, $segments[0]])
                ->get();

            // Rest routes should be empty, or it already should be resolved
            if ($restRoutes->count() == 0) {
                return;
            } else {
                // If production throw 404
                if (\App::environment() == 'production') {
                    abort(404);
                }

                // Else Argument Exception with explanation
                else {
                    throw new \InvalidArgumentException('Route path or the first segment is listed as an REST route');
                }
            }
        }
    }

    private function makeLinksObject()
    {
        $links = (object) [
            'subnav' => (object) [],
        ];

        // Append pages to links object
        $this->pageService->appendPagesToLinks($links);

        //==== Create submenu's

        // Append products to links object
        $this->productService->appendProductsToLinks($links);

        // Append products to links object
        $this->integrationService->appendIntegrationsToLinks($links);

        // Append solutions to links object
        $this->solutionService->appendSolutionsToLinks($links);

        return $links;
    }
}