<?phpnamespace Tionvel\WorkflowBundle\Entity;use App\Entity\User;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\ORM\Mapping as ORM;use Exception;/** * @ORM\Entity() */class WorkflowUnitWorkflow{ /** * @var int * @ORM\Id @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var WorkflowUnitConfig * @ORM\ManyToOne( * targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowUnitConfig", * inversedBy="configs" * ) * @ORM\JoinColumn( * onDelete="CASCADE" * ) */ private $unitConfig; /** * @var string * @ORM\Column(type="string") */ private $workflowId; /** * @var ArrayCollection * @ORM\OneToMany( * targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowUnitRole", * mappedBy="unitWorkflow", * indexBy="role", * fetch="EAGER", * cascade={"persist"}, * orphanRemoval=true * ) */ private $roles; public function __construct() { $this->roles = new ArrayCollection(); } public static function create($workflowId, array $roles = []) { $self = new self; $self->setWorkflowId($workflowId); foreach ($roles as $role) { $self->addRole(WorkflowUnitRole::create($role)); } return $self; } public function getId(): int { return $this->id; } public function getUnitConfig(): WorkflowUnitConfig { return $this->unitConfig; } public function setUnitConfig(WorkflowUnitConfig $unitConfig) { $this->unitConfig = $unitConfig; } public function getWorkflowId(): string { return $this->workflowId; } public function setWorkflowId(string $workflowId) { $this->workflowId = $workflowId; } public function getRoles() { return $this->roles; } public function setRoles(ArrayCollection $roles) { $this->roles->clear(); $this->roles = $roles; } public function getRole($role) { if (!isset($this->roles[$role])) { return null; } try { return $this->roles[$role]->getUser(); } catch (Exception $e) { return null; } } public function setRole($role, User $user) { if (isset($this->roles[$role])) { /** @var WorkflowUnitRole $unitRole */ $unitRole = $this->roles[$role]; $unitRole->setUser($user); return; } $unitRole = WorkflowUnitRole::create($role); $unitRole->setUser($user); $this->addRole($unitRole); } public function addRole(WorkflowUnitRole $role) { if (!isset($this->roles[$role->getRole()])) { $role->setUnitWorkflow($this); $this->roles[$role->getRole()] = $role; } }}