-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: user can now update their profite picture
- Loading branch information
Showing
7 changed files
with
119 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
src/Controller/Api/Media/User/CreateUserProfilePictureAction.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Serializer\Denormalizer; | ||
|
||
use Symfony\Component\HttpFoundation\File\File; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
class UploadedFileDenormalizer implements DenormalizerInterface | ||
{ | ||
public function denormalize($data, string $type, string $format = null, array $context = []): File | ||
{ | ||
return $data; | ||
} | ||
|
||
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool | ||
{ | ||
return $data instanceof File; | ||
} | ||
|
||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return [ | ||
'object' => null, | ||
'*' => false, | ||
File::class => true, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Serializer\Encoder; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Serializer\Encoder\DecoderInterface; | ||
|
||
final class MultipartDecoder implements DecoderInterface | ||
{ | ||
public const FORMAT = 'multipart'; | ||
|
||
public function __construct(private readonly RequestStack $requestStack) | ||
{ | ||
} | ||
|
||
public function decode(string $data, string $format, array $context = []): ?array | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
if (!$request) { | ||
return null; | ||
} | ||
|
||
return array_map(static function (string $element) { | ||
// Multipart form values will be encoded in JSON. | ||
$decoded = json_decode($element, true); | ||
|
||
return \is_array($decoded) ? $decoded : $element; | ||
}, $request->request->all()) + $request->files->all(); | ||
} | ||
|
||
public function supportsDecoding(string $format): bool | ||
{ | ||
return self::FORMAT === $format; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\State\Processor\User; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProcessorInterface; | ||
use App\Entity\Image\UserProfilePicture; | ||
use App\Entity\User; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
|
||
class UserProfilePictureProcessor implements ProcessorInterface | ||
{ | ||
public function __construct( | ||
private readonly Security $security, | ||
private readonly EntityManagerInterface $entityManager | ||
) { | ||
} | ||
|
||
/** | ||
* @param UserProfilePicture $data | ||
*/ | ||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) | ||
{ | ||
dump($data); | ||
/** @var User $user */ | ||
$user = $this->security->getUser(); | ||
$previousProfilePicture = $user->getProfilePicture() ?: null; | ||
if ($previousProfilePicture) { | ||
$user->setProfilePicture(null); | ||
$this->entityManager->flush(); | ||
$this->entityManager->remove($previousProfilePicture); | ||
$this->entityManager->flush(); | ||
} | ||
|
||
$data->setUser($user); | ||
$user->setProfilePicture($data); | ||
$this->entityManager->flush(); | ||
|
||
return $data; | ||
} | ||
} |