1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Api; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\S3\Image; |
6
|
|
|
use AppBundle\Entity\User; |
7
|
|
|
use AppBundle\Exception\JsonHttpException; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
13
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
14
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
15
|
|
|
|
16
|
|
|
class UserController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @Route("/avatar", name="api_avatar") |
20
|
|
|
* @Method({"PUT"}) |
21
|
|
|
*/ |
22
|
|
|
public function avatarAction(Request $request) |
23
|
|
|
{ |
24
|
|
|
$headers = $request->headers; |
25
|
|
|
/** @var User $user */ |
26
|
|
|
$user = $this->getUser(); |
27
|
|
|
|
28
|
|
|
$image = new Image(sprintf('user/%d/avatar', $user->getId())); |
29
|
|
|
$image |
30
|
|
|
->setContentType($headers->get('Content-Type')) |
31
|
|
|
->setContent($request->getContent()); |
32
|
|
|
/** @var ConstraintViolationList $errors */ |
33
|
|
|
$errors = $this->get('validator')->validate($image, null, ['Api']); |
34
|
|
|
|
35
|
|
|
if ($errors->count()) { |
36
|
|
|
$outErrors = []; |
37
|
|
|
|
38
|
|
|
/** @var ConstraintViolation $error */ |
39
|
|
|
foreach ($errors as $error) { |
40
|
|
|
$outErrors['headers'][$error->getPropertyPath()] = $error->getMessage(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
throw new JsonHttpException(400, 'Bad Request', $outErrors); |
44
|
|
|
} |
45
|
|
|
$user->setImage($image); |
|
|
|
|
46
|
|
|
$this->getDoctrine()->getManager()->flush(); |
47
|
|
|
|
48
|
|
|
return $this->json(['user' => $user], 201, [], [AbstractNormalizer::GROUPS => ['Short']]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @Route("/user") |
54
|
|
|
* @Method({"GET"}) |
55
|
|
|
*/ |
56
|
1 |
|
public function userAction() |
57
|
|
|
{ |
58
|
1 |
|
return $this->json(['user' => $this->getUser()], 200, [], [AbstractNormalizer::GROUPS => ['Detail']]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: