<?php
namespace crmeb\services\async\task;

use ReflectionClass;

class AsyncClass extends Task 
{
    public function getCmd():string
    {
        return 'async_class';
    }

    protected function _exec(array $params)
    {
        $className = $params['class'] ?? '';
        $classArgs = $params['classArgs'] ?? [];
        $methodName = $params['method'] ?? '';
        $methodArgs = $params['methodArgs'] ?? [];

        $class = new ReflectionClass($className);
        $method = $class->getMethod($methodName);
        if ($method->isStatic()) {
            return $method->invokeArgs(null, $methodArgs);
        }
        $inst = $class->newInstanceArgs($classArgs);
        return $method->invokeArgs($inst, $methodArgs);
    }

    public static function push(string $className, array $classArgs, string $methodName, array $methodArgs)
    {
        $inst = new self();
        return $inst->put([
            'class' => $className,
            'classArgs' => $classArgs,
            'method' => $methodName,
            'methodArgs' => $methodArgs,
        ]);
    }
}