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/straffer.komma.nl/app/Komma/Seo/SitemapService.php
<?php


namespace Komma\Seo;


use Komma\Routes\RouteRepository;
use Roumen\Sitemap\Sitemap;
use Carbon\Carbon;

class SitemapService
{
    /**
     * @var RouteRepository
     */
    private $routeRepository;

    /**
     * SitemapService constructor.
     * @param RouteRepository $routeRepository
     */
    public function __construct(RouteRepository $routeRepository)
    {
        $this->routeRepository = $routeRepository;
    }

    /***
     * This method will generate the sitemap based on the routes table
     * It will collect the routes based on the given groups array.
     * Then it will build the sitemap grouped by the linked id
     *
     * @param array $groups , which routes we want in the sitemap
     * @param float $weight_from / the minimum weight of the sitemap items
     * @return Sitemap object
     */

    public function getSitemap(
        $groups = ['pages'],
        $weight_from = 0.1)
    {
        //Create the sitemap class
        $sitemap = \App::make('sitemap');

        //Loop trough the routes based on the given parameters
        foreach ($this->routes($groups, $weight_from) as $type => $route)
        {
            $sitemap->add(
                \URL::to($route['route']),
                Carbon::now(),
                $route['weight'],
                'monthly',
                [],
                $route['name']);
        }
        return $sitemap;
    }


    /**
     * Return all (cleaned up) routes
     *
     * @param array $groups
     * @return array
     */
    public function routes($groups = ['pages'], $minWeight = 0.1)
    {
        // Get all routes from database
        $routes = $this->routeRepository->routesForSitemap($minWeight);

        // Clean up route structure
        $cleanRoutes = [];

        foreach ($routes as $route)
        {
            //dd($route);
            // Get pages
            if ($route->routable_type == 'Komma\Kms\Pages\Models\PageTranslation' && in_array('pages', $groups))
            {
                $cleanRoutes['page-' . $route->page_id] = $this->cleanRoute($route, 'page');
            }

            // Get projects
            if ($route->routable_type == 'Komma\Kms\Projects\Models\ProjectTranslation' && in_array('projects', $groups))
            {
                $cleanRoutes['our-work-' . $route->id] = $this->cleanRoute($route, 'project');
            }
        }

        return $cleanRoutes;

    }

    /**
     * Return only necessary properties
     *
     * @param $route
     * @param $prefix
     * @return array
     */
    private function cleanRoute($route, $prefix)
    {
        return [
            'route' => $route->route,
            'weight' => $route->sitemap_weight,
            'name' => $route->{$prefix . '_name'}
        ];
    }
}