Completed
Push — dev ( 5a4ca5...388e34 )
by
unknown
03:44 queued 03:41
created

UserController::avatarAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
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
    /**
20
     * @Route("/avatar", name="api_avatar")
21
     * @Method({"PUT"})
22
     */
23
    public function avatarAction(Request $request)
24
    {
25
        $headers = $request->headers;
26
        /** @var User $user */
27
        $user = $this->getUser();
28
29
        $image = new Image(sprintf('user/%d/avatar', $user->getId()));
30
        $image
31
            ->setContentType($headers->get('Content-Type'))
32
            ->setContent($request->getContent());
33
        /** @var ConstraintViolationList $errors */
34
        $errors = $this->get('validator')->validate($image, null, ['Api']);
35
36
        if ($errors->count()) {
37
            $outErrors = [];
38
39
            /** @var ConstraintViolation $error */
40
            foreach ($errors as $error) {
41
                $outErrors['headers'][$error->getPropertyPath()] = $error->getMessage();
42
            }
43
44
            throw new JsonHttpException(400, 'Bad Request', $outErrors);
45
        }
46
47
        $user->setImage($image);
0 ignored issues
show
Documentation introduced by
$image is of type object<AppBundle\Entity\S3\Image>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
        $this->getDoctrine()->getManager()->flush();
49
50
        return $this->json(['user' => $user], 201, [], [AbstractNormalizer::GROUPS => ['Short']]);
51
    }
52
}
53