src_legacy/Tionvel/WorkflowBundle/Entity/WorkflowUnitWorkflow.php line 13

Open in your IDE?
  1. <?php
  2. namespace Tionvel\WorkflowBundle\Entity;
  3. use App\Entity\User;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Exception;
  7. /**
  8. * @ORM\Entity()
  9. */
  10. class WorkflowUnitWorkflow
  11. {
  12. /**
  13. * @var int
  14. * @ORM\Id @ORM\Column(type="integer")
  15. * @ORM\GeneratedValue(strategy="AUTO")
  16. */
  17. private $id;
  18. /**
  19. * @var WorkflowUnitConfig
  20. * @ORM\ManyToOne(
  21. * targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowUnitConfig",
  22. * inversedBy="configs"
  23. * )
  24. * @ORM\JoinColumn(
  25. * onDelete="CASCADE"
  26. * )
  27. */
  28. private $unitConfig;
  29. /**
  30. * @var string
  31. * @ORM\Column(type="string")
  32. */
  33. private $workflowId;
  34. /**
  35. * @var ArrayCollection
  36. * @ORM\OneToMany(
  37. * targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowUnitRole",
  38. * mappedBy="unitWorkflow",
  39. * indexBy="role",
  40. * fetch="EAGER",
  41. * cascade={"persist"},
  42. * orphanRemoval=true
  43. * )
  44. */
  45. private $roles;
  46. public function __construct()
  47. {
  48. $this->roles = new ArrayCollection();
  49. }
  50. public static function create($workflowId, array $roles = [])
  51. {
  52. $self = new self;
  53. $self->setWorkflowId($workflowId);
  54. foreach ($roles as $role) {
  55. $self->addRole(WorkflowUnitRole::create($role));
  56. }
  57. return $self;
  58. }
  59. public function getId(): int
  60. {
  61. return $this->id;
  62. }
  63. public function getUnitConfig(): WorkflowUnitConfig
  64. {
  65. return $this->unitConfig;
  66. }
  67. public function setUnitConfig(WorkflowUnitConfig $unitConfig)
  68. {
  69. $this->unitConfig = $unitConfig;
  70. }
  71. public function getWorkflowId(): string
  72. {
  73. return $this->workflowId;
  74. }
  75. public function setWorkflowId(string $workflowId)
  76. {
  77. $this->workflowId = $workflowId;
  78. }
  79. public function getRoles()
  80. {
  81. return $this->roles;
  82. }
  83. public function setRoles(ArrayCollection $roles)
  84. {
  85. $this->roles->clear();
  86. $this->roles = $roles;
  87. }
  88. public function getRole($role)
  89. {
  90. if (!isset($this->roles[$role])) {
  91. return null;
  92. }
  93. try {
  94. return $this->roles[$role]->getUser();
  95. } catch (Exception $e) {
  96. return null;
  97. }
  98. }
  99. public function setRole($role, User $user)
  100. {
  101. if (isset($this->roles[$role])) {
  102. /** @var WorkflowUnitRole $unitRole */
  103. $unitRole = $this->roles[$role];
  104. $unitRole->setUser($user);
  105. return;
  106. }
  107. $unitRole = WorkflowUnitRole::create($role);
  108. $unitRole->setUser($user);
  109. $this->addRole($unitRole);
  110. }
  111. public function addRole(WorkflowUnitRole $role)
  112. {
  113. if (!isset($this->roles[$role->getRole()])) {
  114. $role->setUnitWorkflow($this);
  115. $this->roles[$role->getRole()] = $role;
  116. }
  117. }
  118. }