I have a User object which has a a profilePicturePresignedUrl property. The value of the profilePicturePresignedUrl property needs to be computed using a service.
It would seem that the most efficient way of doing this would be to create a method in the User entity class that would inject the required service like so:
Entity/User.php
public function __construct(FileDownloader $fileDownloader) {
$this->fileDownloader = $fileDownloader;
}
public function getProfilePicturePresignedUrl(): string
{
return $this->fileDownloader->generatePresignedUrl($this->profilePicturePath);
}
However, I have read that it is considered bad practice to inject services in entity classes and can find no references in the documentation of how to do so.
The alternative then would be to generate this value in the controller:
Controller/UserController.php
public function getUserInfo(FileDownloader $fileDownloader)
{
$user = $this->getUser();
$user->setProfilePicturePresignedUrl($fileDownloader->generatePresignedUrl($user->getProfilePicturePath()));
return $this->createResponse($user);
}
However, there are many controller methods which call getUser, and it does not seem correct to duplicate this logic in all of them... Is this a situation where it makes sense to inject the service required to generate the presigned URL in the entity class or is there alternative, better approach?