src/Entity/User.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\SuperClass\BaseEntity;
  4. use App\Repository\UserRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. /**
  13. * @ORM\Entity(repositoryClass=UserRepository::class)
  14. * @UniqueEntity("email" , message="Email is already registered")
  15. */
  16. class User extends BaseEntity implements UserInterface, PasswordAuthenticatedUserInterface
  17. {
  18. /**
  19. * @ORM\Column(type="string", length=180, unique=true)
  20. * @Assert\NotBlank
  21. * @Assert\Email(
  22. * message = "The email '{{ value }}' is not a valid email."
  23. * )
  24. */
  25. private $email;
  26. /**
  27. * @ORM\Column(type="json")
  28. */
  29. private $roles = [];
  30. /**
  31. * @var string The hashed password
  32. * @ORM\Column(type="string")
  33. * @Assert\NotBlank
  34. * @Assert\Length(
  35. * min = 8,
  36. * max = 100
  37. * )
  38. */
  39. private $password;
  40. /**
  41. * @ORM\OneToMany(targetEntity=Blog::class, mappedBy="created_by", cascade={"persist"})
  42. */
  43. private $blogs;
  44. /**
  45. * @ORM\OneToMany(targetEntity=Templates::class, mappedBy="created_by", cascade={"persist"})
  46. */
  47. private $templates;
  48. /**
  49. * @ORM\OneToMany(targetEntity=MediaObject::class, mappedBy="created_by")
  50. */
  51. private $mediaObjects;
  52. /**
  53. * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="created_by")
  54. */
  55. private $comments;
  56. /**
  57. * @ORM\OneToMany(targetEntity=Category::class, mappedBy="created_by")
  58. */
  59. private $categories;
  60. /**
  61. * @ORM\OneToMany(targetEntity=Post::class, mappedBy="created_by")
  62. */
  63. private $posts;
  64. /**
  65. * @ORM\OneToMany(targetEntity=Product::class, mappedBy="created_by")
  66. */
  67. private $products;
  68. /**
  69. * @ORM\OneToOne(targetEntity=Customer::class, mappedBy="owner", cascade={"persist", "remove"})
  70. */
  71. private $customer;
  72. /**
  73. * @ORM\Column(type="boolean")
  74. */
  75. private $enabled = 0;
  76. /**
  77. * @ORM\OneToOne(targetEntity=Profile::class, mappedBy="owner", cascade={"persist", "remove"})
  78. */
  79. private $profile;
  80. /**
  81. * @ORM\OneToMany(targetEntity=Address::class, mappedBy="owner", cascade={"persist"})
  82. */
  83. private $addresses;
  84. /**
  85. * @ORM\OneToMany(targetEntity=Source::class, mappedBy="owner")
  86. */
  87. private $sources;
  88. /**
  89. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="created_by")
  90. */
  91. private $user;
  92. /**
  93. * @ORM\OneToMany(targetEntity=User::class, mappedBy="user")
  94. */
  95. private $created_by;
  96. /**
  97. * @ORM\OneToMany(targetEntity=Cart::class, mappedBy="created_by")
  98. */
  99. private $carts;
  100. /**
  101. * @ORM\OneToMany(targetEntity=Cart::class, mappedBy="owner")
  102. */
  103. private $cart;
  104. /**
  105. * @ORM\OneToMany(targetEntity=Profile::class, mappedBy="created_by")
  106. */
  107. private $profiles;
  108. public function __construct()
  109. {
  110. $this->blogs = new ArrayCollection();
  111. $this->templates = new ArrayCollection();
  112. $this->mediaObjects = new ArrayCollection();
  113. $this->comments = new ArrayCollection();
  114. $this->categories = new ArrayCollection();
  115. $this->posts = new ArrayCollection();
  116. $this->products = new ArrayCollection();
  117. $this->addresses = new ArrayCollection();
  118. $this->sources = new ArrayCollection();
  119. $this->created_by = new ArrayCollection();
  120. $this->carts = new ArrayCollection();
  121. $this->cart = new ArrayCollection();
  122. $this->profiles = new ArrayCollection();
  123. }
  124. public function getId(): ?int
  125. {
  126. return $this->id;
  127. }
  128. public function getEmail(): ?string
  129. {
  130. return $this->email;
  131. }
  132. public function setEmail(string $email): self
  133. {
  134. $this->email = $email;
  135. return $this;
  136. }
  137. /**
  138. * A visual identifier that represents this user.
  139. *
  140. * @see UserInterface
  141. */
  142. public function getUserIdentifier(): string
  143. {
  144. return (string) $this->email;
  145. }
  146. /**
  147. * @deprecated since Symfony 5.3, use getUserIdentifier instead
  148. */
  149. public function getUsername(): string
  150. {
  151. return (string) $this->email;
  152. }
  153. /**
  154. * @see UserInterface
  155. */
  156. public function getRoles(): array
  157. {
  158. $roles = $this->roles;
  159. // guarantee every user at least has ROLE_USER
  160. $roles[] = 'ROLE_USER';
  161. return array_unique($roles);
  162. }
  163. public function setRoles(array $roles): self
  164. {
  165. $this->roles = $roles;
  166. return $this;
  167. }
  168. /**
  169. * @see PasswordAuthenticatedUserInterface
  170. */
  171. public function getPassword(): string
  172. {
  173. return $this->password;
  174. }
  175. public function setPassword(string $password): self
  176. {
  177. $this->password = $password;
  178. return $this;
  179. }
  180. /**
  181. * Returning a salt is only needed, if you are not using a modern
  182. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  183. *
  184. * @see UserInterface
  185. */
  186. public function getSalt(): ?string
  187. {
  188. return null;
  189. }
  190. /**
  191. * @see UserInterface
  192. */
  193. public function eraseCredentials()
  194. {
  195. // If you store any temporary, sensitive data on the user, clear it here
  196. // $this->plainPassword = null;
  197. }
  198. /**
  199. * @return Collection|Blog[]
  200. */
  201. public function getBlogs(): Collection
  202. {
  203. return $this->blogs;
  204. }
  205. public function addBlog(Blog $blog): self
  206. {
  207. if (!$this->blogs->contains($blog)) {
  208. $this->blogs[] = $blog;
  209. $blog->setCreatedBy($this);
  210. }
  211. return $this;
  212. }
  213. public function removeBlog(Blog $blog): self
  214. {
  215. if ($this->blogs->removeElement($blog)) {
  216. // set the owning side to null (unless already changed)
  217. if ($blog->getCreatedBy() === $this) {
  218. $blog->setCreatedBy(null);
  219. }
  220. }
  221. return $this;
  222. }
  223. /**
  224. * @return Collection|Templates[]
  225. */
  226. public function getTemplates(): Collection
  227. {
  228. return $this->templates;
  229. }
  230. public function addTemplate(Templates $template): self
  231. {
  232. if (!$this->templates->contains($template)) {
  233. $this->templates[] = $template;
  234. $template->setCreatedBy($this);
  235. }
  236. return $this;
  237. }
  238. public function removeTemplate(Templates $template): self
  239. {
  240. if ($this->templates->removeElement($template)) {
  241. // set the owning side to null (unless already changed)
  242. if ($template->getCreatedBy() === $this) {
  243. $template->setCreatedBy(null);
  244. }
  245. }
  246. return $this;
  247. }
  248. /**
  249. * @return Collection|MediaObject[]
  250. */
  251. public function getMediaObjects(): Collection
  252. {
  253. return $this->mediaObjects;
  254. }
  255. public function addMediaObject(MediaObject $mediaObject): self
  256. {
  257. if (!$this->mediaObjects->contains($mediaObject)) {
  258. $this->mediaObjects[] = $mediaObject;
  259. $mediaObject->setCreatedBy($this);
  260. }
  261. return $this;
  262. }
  263. public function removeMediaObject(MediaObject $mediaObject): self
  264. {
  265. if ($this->mediaObjects->removeElement($mediaObject)) {
  266. // set the owning side to null (unless already changed)
  267. if ($mediaObject->getCreatedBy() === $this) {
  268. $mediaObject->setCreatedBy(null);
  269. }
  270. }
  271. return $this;
  272. }
  273. /**
  274. * @return Collection|Comment[]
  275. */
  276. public function getComments(): Collection
  277. {
  278. return $this->comments;
  279. }
  280. public function addComment(Comment $comment): self
  281. {
  282. if (!$this->comments->contains($comment)) {
  283. $this->comments[] = $comment;
  284. $comment->setCreatedBy($this);
  285. }
  286. return $this;
  287. }
  288. public function removeComment(Comment $comment): self
  289. {
  290. if ($this->comments->removeElement($comment)) {
  291. // set the owning side to null (unless already changed)
  292. if ($comment->getCreatedBy() === $this) {
  293. $comment->setCreatedBy(null);
  294. }
  295. }
  296. return $this;
  297. }
  298. /**
  299. * @return Collection|Category[]
  300. */
  301. public function getCategories(): Collection
  302. {
  303. return $this->categories;
  304. }
  305. public function addCategory(Category $category): self
  306. {
  307. if (!$this->categories->contains($category)) {
  308. $this->categories[] = $category;
  309. $category->setCreatedBy($this);
  310. }
  311. return $this;
  312. }
  313. public function removeCategory(Category $category): self
  314. {
  315. if ($this->categories->removeElement($category)) {
  316. // set the owning side to null (unless already changed)
  317. if ($category->getCreatedBy() === $this) {
  318. $category->setCreatedBy(null);
  319. }
  320. }
  321. return $this;
  322. }
  323. /**
  324. * @return Collection|Post[]
  325. */
  326. public function getPosts(): Collection
  327. {
  328. return $this->posts;
  329. }
  330. public function addPost(Post $post): self
  331. {
  332. if (!$this->posts->contains($post)) {
  333. $this->posts[] = $post;
  334. $post->setCreatedBy($this);
  335. }
  336. return $this;
  337. }
  338. public function removePost(Post $post): self
  339. {
  340. if ($this->posts->removeElement($post)) {
  341. // set the owning side to null (unless already changed)
  342. if ($post->getCreatedBy() === $this) {
  343. $post->setCreatedBy(null);
  344. }
  345. }
  346. return $this;
  347. }
  348. /**
  349. * @return Collection|Product[]
  350. */
  351. public function getProducts(): Collection
  352. {
  353. return $this->products;
  354. }
  355. public function addProduct(Product $product): self
  356. {
  357. if (!$this->products->contains($product)) {
  358. $this->products[] = $product;
  359. $product->setCreatedBy($this);
  360. }
  361. return $this;
  362. }
  363. public function removeProduct(Product $product): self
  364. {
  365. if ($this->products->removeElement($product)) {
  366. // set the owning side to null (unless already changed)
  367. if ($product->getCreatedBy() === $this) {
  368. $product->setCreatedBy(null);
  369. }
  370. }
  371. return $this;
  372. }
  373. public function getCustomer(): ?Customer
  374. {
  375. return $this->customer;
  376. }
  377. public function setCustomer(Customer $customer): self
  378. {
  379. // set the owning side of the relation if necessary
  380. if ($customer->getOwner() !== $this) {
  381. $customer->setOwner($this);
  382. }
  383. $this->customer = $customer;
  384. return $this;
  385. }
  386. public function getEnabled(): ?bool
  387. {
  388. return $this->enabled;
  389. }
  390. public function setEnabled(bool $enabled): self
  391. {
  392. $this->enabled = $enabled;
  393. return $this;
  394. }
  395. public function getProfile(): ?Profile
  396. {
  397. return $this->profile;
  398. }
  399. public function setProfile(?Profile $profile): self
  400. {
  401. // unset the owning side of the relation if necessary
  402. if ($profile === null && $this->profile !== null) {
  403. $this->profile->setOwner(null);
  404. }
  405. // set the owning side of the relation if necessary
  406. if ($profile !== null && $profile->getCreatedBy() !== $this) {
  407. $profile->setCreatedBy($this);
  408. }
  409. $this->profile = $profile;
  410. return $this;
  411. }
  412. /**
  413. * @return Collection|Address[]
  414. */
  415. public function getAddresses(): Collection
  416. {
  417. return $this->addresses;
  418. }
  419. public function addAddress(Address $address): self
  420. {
  421. if (!$this->addresses->contains($address)) {
  422. $this->addresses[] = $address;
  423. $address->setOwner($this);
  424. }
  425. return $this;
  426. }
  427. public function removeAddress(Address $address): self
  428. {
  429. if ($this->addresses->removeElement($address)) {
  430. // set the owning side to null (unless already changed)
  431. if ($address->getOwner() === $this) {
  432. $address->setOwner(null);
  433. }
  434. }
  435. return $this;
  436. }
  437. public function __toString()
  438. {
  439. return $this->getEmail();
  440. }
  441. /**
  442. * @return Collection|Source[]
  443. */
  444. public function getSources(): Collection
  445. {
  446. return $this->sources;
  447. }
  448. public function addSource(Source $source): self
  449. {
  450. if (!$this->sources->contains($source)) {
  451. $this->sources[] = $source;
  452. $source->setOwner($this);
  453. }
  454. return $this;
  455. }
  456. public function removeSource(Source $source): self
  457. {
  458. if ($this->sources->removeElement($source)) {
  459. // set the owning side to null (unless already changed)
  460. if ($source->getOwner() === $this) {
  461. $source->setOwner(null);
  462. }
  463. }
  464. return $this;
  465. }
  466. public function getUser(): ?self
  467. {
  468. return $this->user;
  469. }
  470. public function setUser(?self $user): self
  471. {
  472. $this->user = $user;
  473. return $this;
  474. }
  475. /**
  476. * @return Collection<int, self>
  477. */
  478. public function getCreatedBy(): Collection
  479. {
  480. return $this->created_by;
  481. }
  482. public function addCreatedBy(self $createdBy): self
  483. {
  484. if (!$this->created_by->contains($createdBy)) {
  485. $this->created_by[] = $createdBy;
  486. $createdBy->setUser($this);
  487. }
  488. return $this;
  489. }
  490. public function removeCreatedBy(self $createdBy): self
  491. {
  492. if ($this->created_by->removeElement($createdBy)) {
  493. // set the owning side to null (unless already changed)
  494. if ($createdBy->getUser() === $this) {
  495. $createdBy->setUser(null);
  496. }
  497. }
  498. return $this;
  499. }
  500. /**
  501. * @return Collection<int, Cart>
  502. */
  503. public function getCarts(): Collection
  504. {
  505. return $this->carts;
  506. }
  507. public function addCart(Cart $cart): self
  508. {
  509. if (!$this->carts->contains($cart)) {
  510. $this->carts[] = $cart;
  511. $cart->setCreatedBy($this);
  512. }
  513. return $this;
  514. }
  515. public function removeCart(Cart $cart): self
  516. {
  517. if ($this->carts->removeElement($cart)) {
  518. // set the owning side to null (unless already changed)
  519. if ($cart->getCreatedBy() === $this) {
  520. $cart->setCreatedBy(null);
  521. }
  522. }
  523. return $this;
  524. }
  525. /**
  526. * @return Collection<int, Cart>
  527. */
  528. public function getCart(): Collection
  529. {
  530. return $this->cart;
  531. }
  532. /**
  533. * @return Collection<int, Profile>
  534. */
  535. public function getProfiles(): Collection
  536. {
  537. return $this->profiles;
  538. }
  539. public function addProfile(Profile $profile): self
  540. {
  541. if (!$this->profiles->contains($profile)) {
  542. $this->profiles[] = $profile;
  543. $profile->setCreatedBy($this);
  544. }
  545. return $this;
  546. }
  547. public function removeProfile(Profile $profile): self
  548. {
  549. if ($this->profiles->removeElement($profile)) {
  550. // set the owning side to null (unless already changed)
  551. if ($profile->getCreatedBy() === $this) {
  552. $profile->setCreatedBy(null);
  553. }
  554. }
  555. return $this;
  556. }
  557. }