Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. // Controller
  2. class SecurityController extends AbstractFOSRestController
  3. {
  4. /**
  5. * @Rest\Post("/register", name="user_register")
  6. *
  7. * @param Request $request
  8. * @param Register $register
  9. * @param FormErrorsConverter $converter
  10. *
  11. * @return Response
  12. */
  13. public function register(
  14. Request $request,
  15. Register $register,
  16. FormErrorsConverter $converter
  17. ): Response {
  18. $form = $this->createForm(UserType::class, null);
  19. $form->submit($request->request->all());
  20.  
  21. if ($form->isValid()) {
  22. $data = $form->getData();
  23.  
  24. $registerAction = $register->registerUser($data);
  25.  
  26. return $this->json([
  27. 'success' => $registerAction
  28. ], Response::HTTP_OK);
  29. }
  30.  
  31.  
  32. return $this->json([
  33. 'error' => "Form is not valid",
  34. 'errors_fields' => $converter->convertErrorsFromFrom($form),
  35. ], Response::HTTP_BAD_REQUEST);
  36. }
  37. }
  38. // Service
  39. class Register
  40. {
  41. /** @var EntityManagerInterface */
  42. private $entityManager;
  43.  
  44. /** @var UserPasswordEncoderInterface */
  45. private $passwordEncoder;
  46.  
  47. /**
  48. * @param EntityManagerInterface $entityManager
  49. * @param UserPasswordEncoderInterface $encoder
  50. */
  51. public function __construct(
  52. EntityManagerInterface $entityManager,
  53. UserPasswordEncoderInterface $encoder
  54. ) {
  55. $this->entityManager = $entityManager;
  56. $this->passwordEncoder = $encoder;
  57. }
  58.  
  59. /**
  60. * @param User $user
  61. * @return int|null
  62. */
  63. public function registerUser(User $user)
  64. {
  65. $user->setPassword(
  66. $this->passwordEncoder->encodePassword($user, $user->getPlainPassword())
  67. );
  68.  
  69. $this->entityManager->persist($user);
  70. $this->entityManager->flush();
  71.  
  72. return $user->getId();
  73. }
  74. }
  75.  
  76. // Entity
  77.  
  78. class User implements UserInterface, \Serializable
  79. {
  80. /**
  81. * @ORM\Id()
  82. * @ORM\GeneratedValue()
  83. * @ORM\Column(type="integer")
  84. */
  85. private $id;
  86.  
  87. /**
  88. * @ORM\Column(type="string", length=50, unique=true)
  89. */
  90. private $username;
  91.  
  92. /**
  93. * @ORM\Column(type="string", length=60)
  94. */
  95. private $password;
  96.  
  97. /**
  98. * @ORM\Column(type="string", length=100, unique=true)
  99. */
  100. private $email;
  101.  
  102. /**
  103. * @ORM\Column(type="boolean")
  104. */
  105. private $isActive;
  106.  
  107. /**
  108. * @ORM\Column(type="datetime")
  109. */
  110. private $createdAt;
  111.  
  112. /**
  113. * Field used to encode the password
  114. *
  115. * @var string
  116. */
  117. private $plainPassword;
  118.  
  119. public function __construct()
  120. {
  121. $this->isActive = true;
  122. }
  123.  
  124. public function getId(): ?int
  125. {
  126. return $this->id;
  127. }
  128.  
  129. public function getUsername(): ?string
  130. {
  131. return $this->username;
  132. }
  133.  
  134. public function setUsername(string $username): self
  135. {
  136. $this->username = $username;
  137.  
  138. return $this;
  139. }
  140.  
  141. public function getPassword(): ?string
  142. {
  143. return $this->password;
  144. }
  145.  
  146. public function setPassword(string $password): self
  147. {
  148. $this->password = $password;
  149.  
  150. return $this;
  151. }
  152.  
  153. public function getEmail(): ?string
  154. {
  155. return $this->email;
  156. }
  157.  
  158. public function setEmail(string $email): self
  159. {
  160. $this->email = $email;
  161.  
  162. return $this;
  163. }
  164.  
  165. public function getIsActive(): ?bool
  166. {
  167. return $this->isActive;
  168. }
  169.  
  170. public function setIsActive(bool $isActive): self
  171. {
  172. $this->isActive = $isActive;
  173.  
  174. return $this;
  175. }
  176.  
  177. public function getSalt()
  178. {
  179. return null;
  180. }
  181.  
  182. public function getRoles()
  183. {
  184. return array('ROLE_USER');
  185. }
  186.  
  187. /**
  188. * @ORM\PrePersist()
  189. */
  190. public function setCreatedAt()
  191. {
  192. $this->createdAt = new \DateTime();
  193. }
  194.  
  195. public function getPlainPassword(): ?string
  196. {
  197. return $this->plainPassword;
  198. }
  199.  
  200. public function setPlainPassword(?string $plainPassword): self
  201. {
  202. $this->plainPassword = $plainPassword;
  203.  
  204. return $this;
  205. }
  206.  
  207. public function eraseCredentials()
  208. {
  209. $this->plainPassword = null;
  210. }
  211.  
  212. /** @see \Serializable::serialize() */
  213. public function serialize()
  214. {
  215. return serialize(array(
  216. $this->id,
  217. $this->username,
  218. $this->password,
  219. ));
  220. }
  221.  
  222. /** @see \Serializable::unserialize()
  223. * @param $serialized
  224. */
  225. public function unserialize($serialized)
  226. {
  227. list (
  228. $this->id,
  229. $this->username,
  230. $this->password,
  231. ) = unserialize($serialized, array('allowed_classes' => false));
  232. }
  233. }
  234.  
  235.  
  236. // FormType
  237. class UserType extends AbstractType
  238. {
  239. /**
  240. * @param FormBuilderInterface $builder
  241. * @param array $options
  242. */
  243. public function buildForm(FormBuilderInterface $builder, array $options)
  244. {
  245. $builder
  246. ->add('username', TextType::class)
  247. ->add('plainPassword', PasswordType::class)
  248. ->add('email', EmailType::class)
  249. ;
  250. }
  251.  
  252. /**
  253. * @param OptionsResolver $resolver
  254. */
  255. public function configureOptions(OptionsResolver $resolver)
  256. {
  257. $resolver->setDefaults([
  258. 'data_class' => User::class,
  259. ]);
  260. }
  261. }
  262. // Testy
  263. class RegisterTest extends TestCase
  264. {
  265. /** @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  266. private $entityManagerInterface;
  267.  
  268. /** @var UserPasswordEncoderInterface */
  269. private $passwordEncoder;
  270.  
  271. /** @var Register */
  272. private $registerService;
  273.  
  274. protected function setUp()
  275. {
  276. $this->entityManagerInterface = $this->getMockBuilder(EntityManagerInterface::class)
  277. ->disableOriginalConstructor()
  278. ->getMock();
  279. $this->passwordEncoder = $this->createMock(UserPasswordEncoderInterface::class);
  280.  
  281. $this->registerService = new Register($this->entityManagerInterface, $this->passwordEncoder);
  282. }
  283.  
  284. protected function tearDown()
  285. {
  286. $this->entityManagerInterface = null;
  287. $this->passwordEncoder = null;
  288. }
  289.  
  290. public function testRegisterUser()
  291. {
  292. $user = new User();
  293. $user->setUsername("ExampleUsername");
  294. $user->setPlainPassword("ExamplePlainPassword");
  295. $user->setPassword("ExamplePassword");
  296. $user->setEmail("Example@Email.com");
  297.  
  298. $this->entityManagerInterface
  299. ->expects($this->once())
  300. ->method('persist')
  301. ->with($user);
  302. $this->entityManagerInterface
  303. ->expects($this->once())
  304. ->method('flush');
  305.  
  306. $result = $this->registerService->registerUser($user);
  307.  
  308. $this->assertEquals(1, $result);
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement