2018-06-23 22:45:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Entity;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ORM\Table(name="invites", schema="users")
|
2018-06-24 22:42:26 +00:00
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\InviteRepository")
|
2018-06-23 22:45:18 +00:00
|
|
|
*/
|
|
|
|
class Invite
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @ORM\Id()
|
|
|
|
* @ORM\GeneratedValue(strategy="AUTO")
|
|
|
|
* @ORM\Column(name="id", type="integer")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*
|
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\User")
|
|
|
|
* @ORM\JoinColumn(name="user_id", nullable=false)
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*
|
|
|
|
* @ORM\Column(name="code", type="string", length=32)
|
|
|
|
*/
|
|
|
|
private $code;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User|null
|
|
|
|
*
|
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\User")
|
|
|
|
* @ORM\JoinColumn(name="used_by_id", nullable=true)
|
|
|
|
*/
|
|
|
|
private $usedBy;
|
|
|
|
|
2018-06-24 22:42:26 +00:00
|
|
|
public function __construct(User $forUser)
|
2018-06-23 22:45:18 +00:00
|
|
|
{
|
2018-06-24 22:42:26 +00:00
|
|
|
$this->user = $forUser;
|
2018-06-23 22:45:18 +00:00
|
|
|
$this->code = md5(random_bytes(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser(): User
|
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCode(): string
|
|
|
|
{
|
|
|
|
return $this->code;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUsedBy(): ?User
|
|
|
|
{
|
|
|
|
return $this->usedBy;
|
|
|
|
}
|
2018-06-24 22:42:26 +00:00
|
|
|
|
|
|
|
public function use(User $user): void
|
|
|
|
{
|
|
|
|
if ($this->usedBy) {
|
|
|
|
throw new \RuntimeException(sprintf(
|
|
|
|
'Invite #%d is already used by User#%d and can\'t be used by User#%d',
|
|
|
|
$this->id,
|
|
|
|
$this->usedBy->getId(),
|
|
|
|
$user->getId()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->usedBy = $user;
|
|
|
|
}
|
2018-06-23 22:45:18 +00:00
|
|
|
}
|