Your IP : 216.73.216.79


Current Path : /var/www/html/dynaweb-starter/plugin/dynaweb-site/src/Classes/
Upload File :
Current File : /var/www/html/dynaweb-starter/plugin/dynaweb-site/src/Classes/GenerateMeta.php

<?php

namespace Dynaweb\DynawebSite\Classes;

use Illuminate\Support\Facades\Storage;
use Filament\Notifications\Notification;

class GenerateMeta
{
    public $site;

    public $page;

    public $page_component_repo;

    public static $instance;

    private function __construct(
    ) {
    }

    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new GenerateMeta();
        }

        return self::$instance;
    }

    public function setSiteObject($site)
    {
        $this->site = $site;
        return $this;
    }

    public function setPageObject($page)
    {
        $this->page = $page;
        return $this;
    }

    public function setComponentObject($component)
    {
        $this->page_component_repo = $component;
        return $this;
    }

    public function handle()
    {
        $this->stub();
        $this->generateMeta();

        //dd(json_decode($this->getSiteMeta()));
    }

    public function generateMeta()
    {
        $site_meta = Storage::disk('local')->get('site-meta.json');

        if(empty($site_meta)) {
            $this->createSiteMeta($this->initSiteMeta());
        } else {
            $this->readSiteMeta($site_meta);
        }
    }

    public function triggerPageMetaRefresh($site_slug, $page_slug, $timestamp=null)
    {
        $site_meta = json_decode($this->getSiteMeta());

        if(
            !property_exists($site_meta, $site_slug) ||
            !property_exists($site_meta->{$site_slug}->page, $page_slug)
        ) {
            // throw new \Exception("The '{$site_slug}' or '{$page_slug}' slug doesn't exist");
            Notification::make()
            ->title('New Page Created, Holding update.')
            ->success()
            ->send();

            return;
        }
        $site_meta->{$site_slug}->page->{$page_slug}->updated_at = $timestamp ? $timestamp : time();

        $this->deleteSiteMeta();
        $this->createSiteMeta($site_meta);
    }

    public function readSiteMeta($site_meta)
    {
        $site_meta = json_decode($site_meta);

        /***
         * check site exist, if not create it
         **/
        $refresh = 0;
        if(!property_exists($site_meta, $this->site->slug)) {
            $site_meta = $this->insertSiteMeta($site_meta);
            $refresh = 1;
        } else {
            /***
             * update site meta
             **/
            $site_meta_obj = $site_meta->{$this->site->slug};
            if($site_meta_obj->updated_at > $site_meta_obj->created_at) {
                //update site meta
                /*$site_meta = $this->insertSiteMeta($site_meta);
                $refresh = 1;*/
            }
        }

        /***
         * check page exist, if not create it
         **/
        if(!property_exists($site_meta->{$this->site->slug}->page, $this->page->slug)) {
            $site_meta = $this->insertPageMeta($site_meta);
            $refresh = 1;
        } else {
            /***
             * refresh page meta
             **/
            $page_meta_obj = $site_meta->{$this->site->slug}->page->{$this->page->slug};
            if($page_meta_obj->updated_at > $page_meta_obj->created_at) {
                //refresh site meta
                $site_meta = $this->insertPageMeta($site_meta);
                $refresh = 1;
            }
        }

        if($refresh) {
            echo '<script>console.info({site_meta_refresh: true})</script>';
            $this->deleteSiteMeta();
            $this->createSiteMeta($site_meta);
        }

    }

    public function getSiteMeta()
    {
        return Storage::disk('local')->get('site-meta.json');
    }

    public function initSiteMeta()
    {
        $site_meta = (object) [];
        $site_meta->{$this->site->slug} = (object) $this->createSiteMetaConfig();

        return $site_meta;
    }

    public function insertSiteMeta($site_meta)
    {
        $site_meta->{$this->site->slug} = (object) $this->createSiteMetaConfig();

        return $site_meta;
    }

    public function insertPageMeta($site_meta)
    {
        $site_meta->{$this->site->slug}->page->{$this->page->slug} = (object) $this->createPageMetaConfig();

        return $site_meta;
    }

    public function createSiteMeta($site_meta)
    {
        Storage::disk('local')->put('site-meta.json', json_encode($site_meta));
    }

    public function deleteSiteMeta()
    {
        Storage::disk('local')->delete('site-meta.json');
    }

    public function createSiteMetaConfig()
    {
        $time = time();

        if(empty($this->page)) {
            throw new \Exception('Please Set a Page / Default');
        }

        return [
                'header' => $this->site->header->code,
                'footer' => $this->site->footer->code,
                'created_at' => $time,
                'updated_at' => $time,
                'page' => (object) [
                        $this->page->slug =>  (object) [
                            'created_at' => $time,
                            'updated_at' => $time,
                            'page_id' => $this->page->id,
                            'type' => $this->page->type,
                            'component' => $this->createComponentsConfig(),
                        ],
                ],
            ];
    }

    public function createPageMetaConfig()
    {
        $time = time();
        return [
                'created_at' => $time,
                'updated_at' => $time,
                'page_id' => $this->page->id,
                'type' => $this->page->type,
                'component' => $this->createComponentsConfig(),
        ];
    }

    public function createComponentsConfig()
    {
        $components = $this->getPageComponents();
        $component_arr = [];

        $i = 0;
        foreach ($components as $com) {
            $component_arr[$i]['component_id'] = $com->component_fk;
            $component_arr[$i]['component_code'] = $com->component->code;
            $component_arr[$i]['type'] = $com->component->type;

            $i++;
        }

        return $component_arr;
    }

    public function getPageComponents()
    {
        return $this->page_component_repo->getComponents($this->page->id);
    }

    public function stub()
    {
        // $this->triggerPageMetaRefresh('site-2', 'hellow-mellow', time());
    }

}