Completed
Push — master ( d7ece8...443c1d )
by Valentyn
12:07
created

UserController::postUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\User;
6
use App\Repository\UserRepository;
7
use App\Request\User\RegisterUserRequest;
8
use App\Service\User\RegisterService;
9
use Doctrine\ORM\EntityManagerInterface;
10
use FOS\RestBundle\Controller\FOSRestController;
11
use Symfony\Component\Form\FormFactoryInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Nelmio\ApiDocBundle\Annotation\Model;
15
use Nelmio\ApiDocBundle\Annotation\Security;
16
use Swagger\Annotations as SWG;
17
use Symfony\Component\Routing\Annotation\Route;
18
use App\Form\RegistrationForm;
19
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
20
use Symfony\Component\Validator\Validator\ValidatorInterface;
21
use FOS\RestBundle\View\ViewHandler;
22
use FOS\RestBundle\View\ViewHandlerInterface;
23
use FOS\RestBundle\View\View;
24
25
class UserController extends FOSRestController
26
{
27
    /**
28
     * @var ViewHandler
29
     */
30
    protected $viewHandler;
31
32
    /**
33
     * @var ValidatorInterface
34
     */
35
    protected $validator;
36
37
    /**
38
     * @var UserPasswordEncoderInterface
39
     */
40
    protected $passwordEncoder;
41
42
    /**
43
     * @var EntityManagerInterface
44
     */
45
    protected $em;
46
47
    /**
48
     * @var RegisterService
49
     */
50
    protected $registerService;
51
52
    public function __construct(
53
        ViewHandlerInterface $viewHandler,
54
        ValidatorInterface $validator,
55
        UserPasswordEncoderInterface $passwordEncoder,
56
        RegisterService $registerService,
57
        EntityManagerInterface $entityManager)
58
    {
59
        $this->viewHandler = $viewHandler;
0 ignored issues
show
Documentation Bug introduced by
$viewHandler is of type object<FOS\RestBundle\View\ViewHandlerInterface>, but the property $viewHandler was declared to be of type object<FOS\RestBundle\View\ViewHandler>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
60
        $this->validator = $validator;
61
        $this->passwordEncoder = $passwordEncoder;
62
        $this->em = $entityManager;
63
        $this->registerService = $registerService;
64
    }
65
66
    /**
67
     * Registration
68
     *
69
     * @Route("/api/users", methods={"POST"})
70
     * @SWG\Parameter(name="username", in="formData", type="string")
71
     * @SWG\Parameter(name="password", in="formData", type="string")
72
     * @SWG\Parameter(name="email", in="formData", type="string")
73
     * @SWG\Response(
74
     *     description="Registration.",
75
     *     response=202,
76
     *     @Model(type=User::class)
77
     * )
78
     */
79
    public function postUsers(RegisterUserRequest $request)
80
    {
81
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY');
82
83
        return $this->registerService->registerByRequest($request);
84
    }
85
86
    /**
87
     * Authentication
88
     *
89
     * @Route("/oauth/v2/token", methods={"POST"})
90
     * @SWG\Parameter(name="username", in="formData", type="string")
91
     * @SWG\Parameter(name="password", in="formData", type="string")
92
     * @SWG\Response(
93
     *     description="Authentication.",
94
     *     response=202
95
     * )
96
     */
97
    public function login()
98
    {
99
        throw new NotFoundHttpException('This action should not be called!');
100
    }
101
102
    /**
103
     * Get single user
104
     *
105
     * @Route("/api/users/{id}", methods={"GET"})
106
     * @SWG\Response(
107
     *     description="REST action which returns user by id.",
108
     *     response=200,
109
     *     @Model(type=User::class)
110
     * )
111
     *
112
     * @param int $id
113
     * @return User
114
     */
115
    public function getUsers($id)
116
    {
117
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
118
119
        /**
120
         * @var $userRepository UserRepository
121
         */
122
        $userRepository = $this->getDoctrine()->getRepository(User::class);
123
        $user = $userRepository->find($id);
124
125
        if ($user === null) {
126
            throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
127
        }
128
129
        return $user;
130
    }
131
132
    /**
133
     * Get all users
134
     *
135
     * @Route("/api/users", methods={"GET"})
136
     * @SWG\Response(
137
     *     description="REST action which returns user by id.",
138
     *     response=201,
139
     *     @SWG\Schema(
140
     *         type="array",
141
     *         @SWG\Items(ref=@Model(type=User::class, groups={"full"}))
142
     *     )
143
     * )
144
     *
145
     * @return User[]
146
     */
147
    public function getAll()
148
    {
149
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
150
151
        /**
152
         * @var $userRepository UserRepository
153
         */
154
        $userRepository = $this->getDoctrine()->getRepository(User::class);
155
        $users = $userRepository->findAll();
156
157
        return $users;
158
    }
159
}