324 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Entity;
use ArrayAccess;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\HasCenterInterface;
use Chill\MainBundle\Entity\HasScopeInterface;
use Chill\MainBundle\Entity\Scope;
use Chill\PersonBundle\Entity\Person;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use RuntimeException;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use function in_array;
/**
* Class Participation.
*
* @ORM\Entity(
* repositoryClass="Chill\EventBundle\Repository\ParticipationRepository")
* @ORM\Table(name="chill_event_participation")
* @ORM\HasLifecycleCallbacks
*/
class Participation implements ArrayAccess, HasCenterInterface, HasScopeInterface
{
/**
* @ORM\ManyToOne(
* targetEntity="Chill\EventBundle\Entity\Event",
* inversedBy="participations")
*/
private ?\Chill\EventBundle\Entity\Event $event = null;
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private ?\DateTime $lastUpdate = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\Person")
*/
private ?\Chill\PersonBundle\Entity\Person $person = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\EventBundle\Entity\Role")
*/
private ?\Chill\EventBundle\Entity\Role $role = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\EventBundle\Entity\Status")
*/
private ?\Chill\EventBundle\Entity\Status $status = null;
/**
* @return Center
*/
public function getCenter()
{
if ($this->getEvent() === null) {
throw new RuntimeException('The event is not linked with this instance. '
. 'You should initialize the event with a valid center before.');
}
return $this->getEvent()->getCenter();
}
/**
* Get event.
*
* @return Event
*/
public function getEvent()
{
return $this->event;
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get lastUpdate.
*
* @return DateTime
*/
public function getLastUpdate()
{
return $this->lastUpdate;
}
/**
* Get person.
*
* @return Person
*/
public function getPerson()
{
return $this->person;
}
/**
* Get role.
*
* @return Role
*/
public function getRole()
{
return $this->role;
}
/**
* @return Scope
*/
public function getScope()
{
if ($this->getEvent() === null) {
throw new RuntimeException('The event is not linked with this instance. '
. 'You should initialize the event with a valid center before.');
}
return $this->getEvent()->getCircle();
}
/**
* Get status.
*
* @return Status
*/
public function getStatus()
{
return $this->status;
}
/**
* Check that :.
*
* - the role can be associated with this event type
* - the status can be associated with this event type
*/
public function isConsistent(ExecutionContextInterface $context)
{
if ($this->getEvent() === null || $this->getRole() === null || $this->getStatus() === null) {
return;
}
if (
$this->getRole()->getType()->getId() !==
$this->getEvent()->getType()->getId()
) {
$context->buildViolation('The role is not allowed with this event type')
->atPath('role')
->addViolation();
}
if (
$this->getStatus()->getType()->getId() !==
$this->getEvent()->getType()->getId()
) {
$context->buildViolation('The status is not allowed with this event type')
->atPath('status')
->addViolation();
}
}
public function offsetExists(mixed $offset): bool
{
return in_array($offset, [
'person', 'role', 'status', 'event',
], true);
}
/**
* @return Event|Person|Role|Status
*/
public function offsetGet(mixed $offset): mixed
{
return match ($offset) {
'person' => $this->getPerson(),
'role' => $this->getRole(),
'status' => $this->getStatus(),
'event' => $this->getEvent(),
default => throw new \LogicException("this offset does not exists : " . $offset),
};
}
public function offsetSet(mixed $offset, mixed $value): void
{
switch ($offset) {
case 'person':
$this->setPerson($value);
break;
case 'role':
$this->setRole($value);
break;
case 'status':
$this->setStatus($value);
break;
case 'event':
$this->setEvent($value);
break;
}
}
public function offsetUnset(mixed $offset): void
{
$this->offsetSet($offset, null);
}
/**
* Set event.
*
* @param Event $event
*
* @return Participation
*/
public function setEvent(?Event $event = null)
{
if ($this->event !== $event) {
$this->update();
}
$this->event = $event;
return $this;
}
/**
* Set person.
*
* @param Person $person
*
* @return Participation
*/
public function setPerson(?Person $person = null)
{
if ($person !== $this->person) {
$this->update();
}
$this->person = $person;
return $this;
}
/**
* Set role.
*
* @param Role $role
*
* @return Participation
*/
public function setRole(?Role $role = null)
{
if ($role !== $this->role) {
$this->update();
}
$this->role = $role;
return $this;
}
/**
* Set status.
*
* @param Status $status
*
* @return Participation
*/
public function setStatus(?Status $status = null)
{
if ($this->status !== $status) {
$this->update();
}
$this->status = $status;
return $this;
}
/**
* Set lastUpdate.
*
* @return Participation
*/
protected function update()
{
$this->lastUpdate = new DateTime('now');
return $this;
}
}