<?php namespace Tussendoor\Reloadify\Concerns\Http;

use ReflectionMethod;
use WC_REST_Authentication;

trait HasAuthentication {

    /**
     * Perform check via official WC REST auth class by reflecting method accessibility.
     * Uses official auth class, so it triggers all hooks as on normal 'wp-json/wc/v3/' routes
     *
     * @return bool
     * @throws ReflectionException
     */
    public function checkPermissions(): bool
    {
        // WC not enabled, target reflection class is probably not registered
        if ( ! function_exists( 'WC' ) ) {
            return false;
        }

        $method = new ReflectionMethod('WC_REST_Authentication', 'perform_basic_authentication');
        $method->setAccessible(true);

        return $method->invoke( new WC_REST_Authentication ) !== false;
    }

}
