I have this controller
<?php
namespace App\Infrastructure\Controller;
use App\Application\Command\CreateUserCommand;
use Ramsey\Uuid\Uuid;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\MessageBusInterface;
class UserController extends AbstractController
{
public function __construct(private MessageBusInterface $bus){}
public function createUser(Request $request):JsonResponse
{
$data = json_decode($request->getContent(),true);
$userCommand = new CreateUserCommand(
$id = Uuid::uuid4(),
$data['email'],
$data['name'],
$data['surname'],
$data['password']
);
$this->bus->dispatch($userCommand);
return new JsonResponse($data);
}
}
But I get this error
App\Application\Command\CreateUserCommand::__construct(): Argument #1 ($id) must be of type Ramsey\Uuid\Uuid, Ramsey\Uuid\Lazy\LazyUuidFromString given, called in /var/www/html/src/User/Infrastructure/Controller/UserController.php on line 28
Its suposed that Uuid::uuid4()
generates a Ramsey\Uuid\Uuid
but its generating a Ramsey\Uuid\Lazy\LazyUuidFromString
I haven't changed anything on the config.
Not sure what is wrong on this code or config
They use LazyUuidFromString for performance reasons compare with documentation:
Lazy version of a UUID: its format has not been determined yet, so it is mostly only usable for string/bytes conversion. This object optimizes instantiation, serialization and string conversion time, at the cost of increased overhead for more advanced UUID operations. Internal: this type is used internally for performance reasons, and is not supposed to be directly referenced in consumer libraries.
Uuid::uuid4()
returns an UuidInterface
it would be best if you require Ramsey\Uuid\UuidInterface
in your CreateUserCommand
instead of Ramsey\Uuid\Uuid