<?php

namespace Clonable;

use Clonable\Services\ApiService;
use Exception;

class Auth implements MiddlewareInterface {
    private $has_succeeded = false;

    public function handle() {
        if ($this->has_succeeded) {
            return true;
        }

        $api_key = get_option('clonable_api_key');
        if (!$api_key) {
            return false;
        }

        if (!preg_match("/^clonable_[a-z0-9]{8}_[a-z0-9]{24}$/", $api_key)) {
            return false;
        }

        try {
            $api_response = ApiService::get_user($api_key);
        } catch (Exception $e) {
            return false;
        }

        if ($api_response->get_code() !== 200) {
            return false;
        }

        $this->has_succeeded = true;
        return true;
    }
}