<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class InstallFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $isInstalled = file_exists(WRITEPATH . 'installed.lock');
        $currentPath = $request->getUri()->getPath();

        // If not installed and trying to access anything other than /install/*, redirect to installer
        if (!$isInstalled && strpos($currentPath, 'install') === false) {
            return redirect()->to('/install');
        }

        // If installed and trying to access installer, redirect to home
        if ($isInstalled && strpos($currentPath, 'install') !== false) {
            return redirect()->to('/');
        }
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
    }
}
