<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Traits\AddressTrait;
use App\Entity\Traits\GroupsCollectionTrait;
use App\Entity\Traits\TimestampableTrait;
use App\Serializer\Annotation\ApiKeyAlias;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Controller\Api\EditProfile;
use Doctrine\ORM\Mapping as ORM;
use Faker\Core\Uuid;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Serializer\Annotation\Groups as SerializerGroups;
/**
* Customer.
*
* @ApiResource(
* collectionOperations={},
* itemOperations={
* "get",
* "update_profile"= {
* "method"="PUT",
* "validation_groups"={"profile"},
* "controller"="EditProfile::class"
* },
* "update_iban"= {
* "method"="PUT",
* "validation_groups"={"edit_iban"},
* "controller"="EditIban::class"
* },
* "change_pwd"= {"method"="PUT", "path"="customers/{id}/change-pwd", "validation_groups"={"changePwd"}}
* },
* attributes={
* "normalization_context"={"groups"={"get"}},
* "access_control"="is_granted('ROLE_USER')"
* })
*
* @ORM\Table(name="customer")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"partner" = "Customer", "groups" = "Groupement"})
*
* @UniqueEntity("email")
* @UniqueEntity("companyId")
*
* @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
*/
class Customer extends AbstractUser
{
const UPLOAD_CSV_PATH = 'resources/uploads/operation/';
use TimestampableTrait, GroupsCollectionTrait, AddressTrait;
/**
* @var bool
*
* @ORM\Column(name="opt_in", type="boolean")
*/
protected $optIn = false;
/**
* @var string
*
* @ORM\Column(name="company_id", type="string", length=25, nullable=true)
*
* @SerializerGroups({
* "get",
* "get_participation_coll",
* })
*
* @ApiKeyAlias("id")
*/
protected $companyId;
/**
* @var Sector
*
* @ORM\ManyToOne(targetEntity="App\Entity\Sector")
*/
private $sector;
/**
* @var Collection
*
* @ORM\ManyToMany(targetEntity="Groupement", inversedBy="customers")
*/
private $groupements;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="App\Entity\Signature", mappedBy="customer", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $signatures;
/**
* @var string
*
* @Assert\NotBlank(groups={"changePwd"})
*/
protected $plainPassword;
/**
* @var string
*
* @SecurityAssert\UserPassword(
* message = "Votre mot de passe est incorrect",
* groups={"profile", "changePwd", "edit_iban"}
* )
*/
protected $currentPwd;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="App\Entity\CustomerOperation", mappedBy="customer", cascade={"persist"})
*/
protected $customerOperations;
/**
* @var \App\Entity\CustomerBrand
*
* @ORM\ManyToOne(targetEntity="CustomerBrand", inversedBy="customers")
*
*/
private $brand;
/**
* @ORM\Column(type="string", length=50, unique=true, nullable=true)
* @Assert\NotBlank()
*/
private $sogecId = null;
/**
* @return string
*/
public function getCurrentPwd(): ?string
{
return $this->currentPwd;
}
/**
* @param string $currentPwd
*/
public function setCurrentPwd(string $currentPwd)
{
$this->currentPwd = $currentPwd;
}
/**
* Customer constructor.
*
* @param string|null $username
* @param array|null $roles
* @param string|null $email
*/
public function __construct($username = null, array $roles = null, $email = null)
{
$this->companyId = $username;
$this->roles = $roles;
$this->email = $email;
$this->signatures = new ArrayCollection();
$this->customerOperations = new ArrayCollection();
$this->groupements = new ArrayCollection();
}
/**
* Get username.
*
* @return string
*/
public function getUsername()
{
return $this->companyId;
}
/**
* @return bool
*/
public function isOptIn()
{
return $this->optIn;
}
/**
* Set optIn.
*
* @param bool $optIn
*
* @return Customer
*/
public function setOptIn(bool $optIn)
{
$this->optIn = $optIn;
return $this;
}
/**
* @return string
*/
public function getCompanyId()
{
return $this->companyId;
}
/**
* @param string $companyId
*
* @return Customer
*/
public function setCompanyId($companyId)
{
$this->companyId = $companyId;
return $this;
}
/**
* Set sector.
*
* @param Sector $sector
*
* @return Customer
*/
public function setSector(Sector $sector = null)
{
$this->sector = $sector;
return $this;
}
/**
* Get sector.
*
* @return Sector
*/
public function getSector()
{
return $this->sector;
}
/**
* Add operation.
*
* @param \App\Entity\Signature $signature
*
* @return Customer
*/
public function addSignature(\App\Entity\Signature $signature)
{
if ($this->signatures->contains($signature)) {
return;
}
$this->signatures->add($signature);
$signature->setCustomer($this);
return $this;
}
/**
* Remove operation.
*
* @param \App\Entity\Signature $signature
*/
public function removeSignature(\App\Entity\Signature $signature)
{
if (!$this->signatures->contains($signature)) {
return;
}
$this->signatures->removeElement($signature);
}
/**
* Get operations.
*
* @return Collection
*/
public function getSignatures()
{
return $this->signatures;
}
/**
* {@inheritdoc}
*/
public static function createFromPayload($username, array $payload)
{
return new self(
$username,
$payload['roles'],
$payload['email']
);
}
/**
* Add customer.
*
* @param \App\Entity\CustomerOperation $customerOperation
*
* @return Customer
*/
public function addCustomerOperation(\App\Entity\CustomerOperation $customerOperation)
{
if ($this->customerOperations->contains($customerOperation)) {
return;
}
$this->customerOperations->add($customerOperation);
return $this;
}
/**
* Remove operation.
*
* @param \App\Entity\CustomerOperation $customerOperation
*/
public function removeCustomerOperation(\App\Entity\CustomerOperation $customerOperation)
{
if (!$this->customerOperations->contains($customerOperation)) {
return;
}
$this->customerOperations->removeElement($customerOperation);
}
/**
* Get operations.
*
* @return Collection
*/
public function getCustomerOperations()
{
return $this->customerOperations;
}
/**
* @return CustomerBrand
*/
public function getBrand(): ?CustomerBrand
{
return $this->brand;
}
/**
* @param CustomerBrand $brand
*
* @return self
*/
public function setBrand(CustomerBrand $brand): self
{
$this->brand = $brand;
return $this;
}
/**
* @return null
*/
public function getSogecId()
{
return $this->sogecId;
}
/**
* @param null $sogecId
* @return Customer
*/
public function setSogecId($sogecId)
{
$this->sogecId = $sogecId;
return $this;
}
}