Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

php - Symfony -Return json output of Command Class from Controller

In my Symfony project I have created a Command class to delete specific user.

I injected required parameter "email" in the Command class constructor.

I have never tried to implement command in the Controller so I have problem there.

I want to trigger the API call in the Controller which will return desired json output if command is successful.

How can I accomplish that?

My Command class:

protected static $defaultName = 'user:delete';

$entityManager;
private $userService;
private $email;

public function __construct(string $email = null, EntityManagerInterface $entityManager, KeycloakApi $keycloakApi)
{
    parent::__construct($email);

    $this->entityManager = $entityManager;
    $this->userService = $userService;
}

protected function configure()
{
    $this
        ->setDescription('Deletion of selected user.')
        ->addArgument('email', InputArgument::REQUIRED, 'User email');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $user = $this->userService->getUserByEmail($this->email);
    if (empty($user)) {
        throw new Exception('USER_DOESNT_EXIST');
    }

    $this->userService->deleteUser($user['id']);
    $output->writeln('Done!');
}

And my try in controller to get what I want:

/**
 * @Route("/delete/test", name="delete_test")
 */
public function testDelete(): JsonResponse
{

    $application = new Application($this->kernel);
    $application->setAutoExit(false);

    $input = new ArrayInput(array("user:delete"));
    $output = new BufferedOutput();
    // Run the command
    $retval = $application->run($input, $output);

    dump($retval);die;
}

And the main question is how to pass email parameter in command that is needed to be provided for this endpoint?

question from:https://stackoverflow.com/questions/65901719/symfony-return-json-output-of-command-class-from-controller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the comments said, you shouldn't call a command from a controller, both are a different entry point of your application. Controller are used to access from the web and Command to be executed from a cli.

You should put your domain logic in neither of those files.

Here's an example which is possible :

Controller :

/**
 * @Route("/delete/{email}", name="delete")
 */
public function delete(string $email, DeleteUserHandler $handler): JsonResponse
{
    $handler->handle($email);

    return new JsonReponse(null, 204);
}

DeleteUserHandler :

...
public function handle(string $email): void
{
    $user = $this->userService->getUserByEmail($this->email);
    if (empty($user)) {
        throw new Exception('USER_DOESNT_EXIST');
    }

    $this->userService->deleteUser($user['id']);
}

I kept a bit of you code but IMO you don't even need to find before the delete (just maybe add a security to avoid anyone to delete anyone)

With this kind of code you can reuse the "DeleteUserHandler" in a command (or wherever) if you need it and the code which really delete a user isn't coupled with the entry point anymore


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...