<?php

namespace App\Observers;

use App\Models\SiteConfiguration;
use Artisan;

class MaintenanceModeObserver
{
    /**
     * Handle the AdminSettings "updated" event.
     *
     * @param SiteConfiguration $adminSettings
     * @return void
     */
    public function updated(SiteConfiguration $adminSettings)
    {
        $liveSite = $adminSettings->where('key', SiteConfiguration::LIVE_DOWN)->first()->value;
        $wholeSite = $adminSettings->where('key', SiteConfiguration::WHOLE_DOWN)->first()->value;
        if ($liveSite == 'on' || $wholeSite == 'on') {
            $sessions = glob(storage_path("framework/sessions/*"));
            foreach ($sessions as $file) {
                if (is_file($file)) {
                    unlink($file);
                }
            }
            Artisan::call('down');
        } else {
            Artisan::call('up');
            $sessions = glob(storage_path("framework/sessions/*"));
            foreach ($sessions as $file) {
                if (is_file($file)) {
                    unlink($file);
                }
            }
            Artisan::call('cache:clear');
        }
    }
}
