<?php

namespace twa\htmlhelpers\controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Str;


class SiteMapController
{
    public $values;

    public static function redirect(){
        $sitemap = self::getSiteMapLocation();
        if(!$sitemap){ return false; }
        return redirect($sitemap);
    }

    public static function sync(){
        $sitemap = self::getSiteMapLocation();
        if(!$sitemap){ return false; }
        copy($sitemap,public_path()."/sitemap.xml");
    }


    public static  function getSiteMapLocation(){
        try{
            $robots = file_get_contents('robots.txt');
        }catch (\Throwable $e){
            exit();
        }

        $robots = explode("<br />",nl2br($robots));
        $sitemap = false;
        foreach ($robots AS $robot){
            $robot = strtolower($robot);
            if(Str::contains($robot,'sitemap:')){
                $sitemap = str_replace([' ','sitemap:'],'',$robot);
            }
        }
        return $sitemap;
    }
}
