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/inzigd.komma.pro/app/Http/Wildcards/CoursesWildcard.php
<?php

namespace App\Http\Wildcards;

use App\Komma\Categories\Models\Category;
use App\Komma\Courses\Models\CourseTranslation;
use Illuminate\Support\Facades\Log;

class CoursesWildcard implements WildcardInterface
{

    /**
     * @param $request
     * @param $wildcard
     * @return mixed
     */
    public function handle($request, $wildcard)
    {

        // Get the type of the course
        $typeCodeName = $wildcard->type->pageTranslation->translatable->getParent()->code_name;
        switch ($typeCodeName) {
            case 'teaching':
                $categoryId = Category::TEACHING;
                break;
            case 'enterprising':
                $categoryId = Category::ENTERPRICING;
                break;
            default:
                Log::alert('CoursesWildcard: Category not found ' . $typeCodeName);
                return $request;
        }


        // Get the courses with that are the same as the found slug
        $courseTranslations = CourseTranslation::where('slug', $wildcard->tail[0])
            ->with('translatable', 'translatable.category')
            ->has('translatable')
            ->has('translatable.category')
            ->get();

        // If there isn't a translation found continue request
        if($courseTranslations->isEmpty()) return $request;

        // Loop through the courses with the right slug
        // We do this because course can be in both categories so we need to make sure the categories matches the wildcard type
        foreach ($courseTranslations as $courseTranslation) {

            $courseCategory = $courseTranslation->translatable->category->first();

            // If the category of the course doesn't match continue
            if($courseCategory->id != $categoryId) continue;

            // Category match so this the correct course

            //Set the request URI and the original path
            $request->server->set('REQUEST_URI', 'courses/'.$courseTranslation->course_id);
            return $request;
        }

        // Fallback continue
        return $request;
    }

}