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/deensekroon.komma-mediadesign.nl/wwwroot/App/Routes/RouteRedirector.php
<?php


namespace App\Routes;


use App\Brands\BrandRepository;
use App\Categories\CategoryRepository;

class RouteRedirector
{
    /**
     * @var RoutesRepository
     */
    private $routeRepository;

    /**
     * @var CategoryRepository
     */
    private $categoryRepository;
    /**
     * @var BrandRepository
     */
    private $brandRepository;


    /**
     * RouteResolver constructor.
     */
    public function __construct()
    {
        $this->routeRepository = new RoutesRepository();
        $this->categoryRepository = new CategoryRepository();
        $this->brandRepository = new BrandRepository();
    }

    /**
     * Redirect old route
     *
     * @param $type
     * @param $path
     */
    public function redirectOldRoute($type, $path)
    {
        // Make sure path does nog end with a slash
        // Todo: cleanup for multiple slashes
        if(substr($path,-2,2) == '//') $path = substr($path,0,-2);
        if(substr($path,-1,1) == '/') $path = substr($path,0,-1);

        // Get last path value
        $paths = explode('/',$path);
        $slug = end($paths);

        // Find new route
        switch($type)
        {
            case 'product':
                $route = $this->findNewProductRoute($slug);
                break;
            case 'category':
                $route = $this->findNewCategoryRoute($slug);
                break;
            case 'brand':
                $route = $this->findNewBrandRoute($slug);
                break;
        }

        // Header
        header('location: /' . $route->route_nl,true,301);
        exit;
    }

    /**
     * Find new product route
     *
     * @param $slug
     * @return bool|object|\stdClass
     */
    private function findNewProductRoute($slug)
    {
        // Get product id
        $parts = explode('-',$slug);
        $productId = end($parts);

        // Find route
        if( ! $route = $this->routeRepository->findByRouteable($productId,'product')) $this->abort();

        return $route;
    }

    /**
     * Find new category route
     *
     * @param $slug
     * @return bool|object|\stdClass
     */
    private function findNewCategoryRoute($slug)
    {
        // We don't have a category id, only the slug.
        $categoryId = null;
        // The slug is not saved in the database, so we need to get all categories and find it.
        $categories = $this->categoryRepository->categories();

        // Find slug
        foreach($categories as $category)
        {
            if(linkname($category->title_nl) == $slug)
            {
                $categoryId = $category->id;
                break;
            }
        }

        // Abort when no category is found
        if($categoryId == null) $this->abort();

        // Find route
        if( ! $route = $this->routeRepository->findByRouteable($categoryId,'category')) $this->abort();

        return $route;
    }

    /**
     * Find new brand route
     *
     * @param $slug
     * @return bool|object|\stdClass
     */
    private function findNewBrandRoute($slug)
    {
        // We don't have a category id, only the slug.
        $brandId = null;
        // The slug is not saved in the database, so we need to get all categories and find it.
        $brands = $this->brandRepository->brands();
        // Find slug
        foreach($brands as $brand)
        {
            if(linkname($brand->name) == $slug)
            {
                $brandId = $brand->id;
                break;
            }
        }

        // Abort when no category is found
        if($brandId == null) $this->abort();

        // Find route
        if( ! $route = $this->routeRepository->findByRouteable($brandId,'brand')) $this->abort();

        return $route;
    }

    /**
     * Show 404 page
     */
    private function abort()
    {
        header('HTTP/1.0 404 Not Found');
        echo 'Pagina niet gevonden, keer terug naar de <a href="/">Deense Kroon homepagina</a>';
        exit;
    }
}