src_legacy/Tionvel/WorkflowBundle/Entity/WorkflowState.php line 24

Open in your IDE?
  1. <?php
  2. namespace Tionvel\WorkflowBundle\Entity;
  3. use App\Entity\User;
  4. use App\Util\DateUtils;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Exception;
  10. /**
  11. * Class WorkflowState
  12. * @package Tionvel\WorkflowBundle\Entity
  13. * @ORM\Entity()
  14. * @ORM\Table(
  15. * name="workflow_states",
  16. * indexes={
  17. * @ORM\Index(name="idx_ws_state_current", columns={"state", "current"})
  18. * }
  19. * )
  20. */
  21. class WorkflowState
  22. {
  23. const START_STATE = '_process_start';
  24. const START_TRANS = '_process_start';
  25. /**
  26. * @var int
  27. * @ORM\Id @ORM\Column(type="integer")
  28. * @ORM\GeneratedValue(strategy="AUTO")
  29. */
  30. private $id;
  31. /**
  32. * @var string
  33. * @ORM\Column(type="string")
  34. */
  35. private $state;
  36. /**
  37. * @var WorkflowProcess
  38. * @ORM\ManyToOne(
  39. * targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowProcess",
  40. * inversedBy="states"
  41. * )
  42. * @ORM\JoinColumn(
  43. * onDelete="CASCADE"
  44. * )
  45. */
  46. private $process;
  47. /**
  48. * @var ArrayCollection|WorkflowAssignee[]
  49. * @ORM\OneToMany(
  50. * targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowAssignee",
  51. * mappedBy="state",
  52. * fetch="EAGER",
  53. * cascade={"persist", "remove"},
  54. * orphanRemoval=true
  55. * )
  56. */
  57. private $assignees;
  58. /**
  59. * @var bool
  60. * @ORM\Column(type="boolean")
  61. */
  62. private $current = false;
  63. /**
  64. * @var DateTime
  65. * @ORM\Column(type="datetime")
  66. */
  67. private $startedAt;
  68. /**
  69. * @var DateTime
  70. * @ORM\Column(type="datetime", nullable=true)
  71. */
  72. private $completedAt;
  73. /**
  74. * @var DateTime
  75. * @ORM\Column(type="datetime", nullable=true)
  76. */
  77. private $deadline;
  78. /**
  79. * @var integer
  80. * @ORM\Column(type="bigint")
  81. */
  82. private $elapsedTime = 0;
  83. /**
  84. * @var WorkflowState
  85. * @ORM\OneToOne(targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowState", cascade={"persist"})
  86. * @ORM\JoinColumn(name="prev_id", nullable=true, onDelete="SET NULL")
  87. */
  88. private $prev;
  89. /**
  90. * @var WorkflowState
  91. * @ORM\OneToOne(targetEntity="Tionvel\WorkflowBundle\Entity\WorkflowState", cascade={"persist"})
  92. * @ORM\JoinColumn(name="next_id", nullable=true, onDelete="SET NULL")
  93. */
  94. private $next;
  95. /**
  96. * @var string
  97. * @ORM\Column(type="string", nullable=true)
  98. */
  99. private $inTransition;
  100. /**
  101. * @var string
  102. * @ORM\Column(type="string", nullable=true)
  103. */
  104. private $outTransition;
  105. /**
  106. * @var User
  107. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  108. * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
  109. */
  110. private $startedBy;
  111. /**
  112. * @var User
  113. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  114. * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
  115. */
  116. private $completedBy;
  117. /**
  118. * @var array
  119. * @ORM\Column(type="array")
  120. */
  121. private $metadata;
  122. /**
  123. * WorkflowState constructor.
  124. * @throws Exception
  125. */
  126. public function __construct()
  127. {
  128. $this->assignees = new ArrayCollection;
  129. $this->startedAt = new DateTime;
  130. }
  131. /**
  132. * @param WorkflowProcess $process
  133. * @param $state
  134. * @param User $startedBy
  135. * @param string $transition
  136. * @return WorkflowState
  137. * @throws Exception
  138. */
  139. public static function start(
  140. WorkflowProcess $process,
  141. $startedBy,
  142. $state = WorkflowState::START_STATE,
  143. $transition = WorkflowState::START_TRANS
  144. ) {
  145. $self = new self;
  146. $self->setProcess($process);
  147. $self->setState($state);
  148. $self->setCurrent(true);
  149. $self->setStartedBy($startedBy);
  150. $self->setStartedAt(new DateTime);
  151. $self->setInTransition($transition);
  152. return $self;
  153. }
  154. /**
  155. * @param WorkflowState $next
  156. * @param User $completedBy
  157. * @param $transition
  158. * @throws Exception
  159. */
  160. public function finish(WorkflowState $next, $completedBy, $transition)
  161. {
  162. $this->setCurrent(false);
  163. $this->setNext($next);
  164. $this->setCompletedAt(new DateTime);
  165. $this->setCompletedBy($completedBy);
  166. $this->setOutTransition($transition);
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function __toString()
  172. {
  173. return $this->state;
  174. }
  175. /**
  176. * @return int
  177. */
  178. public function getId()
  179. {
  180. return $this->id;
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getState()
  186. {
  187. return $this->state;
  188. }
  189. /**
  190. * @param string $state
  191. */
  192. public function setState(string $state)
  193. {
  194. $this->state = $state;
  195. }
  196. /**
  197. * @param $state
  198. * @return bool
  199. */
  200. public function isState($state)
  201. {
  202. return $this->state === $state;
  203. }
  204. /**
  205. * @return WorkflowProcess
  206. */
  207. public function getProcess()
  208. {
  209. return $this->process;
  210. }
  211. /**
  212. * @param WorkflowProcess $process
  213. */
  214. public function setProcess(WorkflowProcess $process)
  215. {
  216. $this->process = $process;
  217. }
  218. /**
  219. * @return ArrayCollection
  220. */
  221. public function getAssignees()
  222. {
  223. return $this->assignees;
  224. }
  225. /**
  226. * @param ArrayCollection $assignees
  227. */
  228. public function setAssignees(ArrayCollection $assignees)
  229. {
  230. $this->assignees = $assignees;
  231. }
  232. /**
  233. * @param WorkflowAssignee $assignee
  234. */
  235. public function addAssignee(WorkflowAssignee $assignee)
  236. {
  237. if (!$this->assignees->contains($assignee)) {
  238. $this->assignees->add($assignee);
  239. $assignee->setState($this);
  240. }
  241. }
  242. /**
  243. * @param WorkflowAssignee $assignee
  244. */
  245. public function removeAssignee(WorkflowAssignee $assignee)
  246. {
  247. if ($this->assignees->contains($assignee)) {
  248. $this->assignees->removeElement($assignee);
  249. }
  250. }
  251. /**
  252. * @param $key
  253. */
  254. public function removeAssigneeByKey($key)
  255. {
  256. $criteria = Criteria::create()
  257. ->where(Criteria::expr()->eq('name', $key))
  258. ;
  259. $this->assignees->matching($criteria)
  260. ->map(function (WorkflowAssignee $assignee) {
  261. $this->removeAssignee($assignee);
  262. })
  263. ;
  264. }
  265. /**
  266. * @return bool
  267. */
  268. public function isCurrent()
  269. {
  270. return $this->current;
  271. }
  272. /**
  273. * @param bool $current
  274. */
  275. public function setCurrent(bool $current)
  276. {
  277. $this->current = $current;
  278. }
  279. /**
  280. * @return DateTime
  281. */
  282. public function getStartedAt()
  283. {
  284. return $this->startedAt;
  285. }
  286. /**
  287. * @param DateTime $startedAt
  288. */
  289. public function setStartedAt(DateTime $startedAt)
  290. {
  291. $this->startedAt = $startedAt;
  292. }
  293. /**
  294. * @return DateTime
  295. */
  296. public function getCompletedAt()
  297. {
  298. return $this->completedAt;
  299. }
  300. /**
  301. * @param DateTime|null $completedAt
  302. */
  303. public function setCompletedAt(?DateTime $completedAt)
  304. {
  305. $this->completedAt = $completedAt;
  306. if($completedAt) {
  307. $this->elapsedTime = $completedAt->getTimestamp() - $this->startedAt->getTimestamp();
  308. }
  309. else {
  310. $this->elapsedTime = 0;
  311. }
  312. }
  313. /**
  314. * @return DateTime
  315. */
  316. public function getDeadline()
  317. {
  318. return $this->deadline;
  319. }
  320. /**
  321. * @param DateTime $deadline
  322. * @return WorkflowState
  323. */
  324. public function setDeadline(DateTime $deadline)
  325. {
  326. $this->deadline = $deadline;
  327. return $this;
  328. }
  329. /**
  330. * @return int
  331. */
  332. public function getElapsedTime()
  333. {
  334. return $this->elapsedTime;
  335. }
  336. /**
  337. * @return string
  338. * @throws Exception
  339. */
  340. public function getElapsedTimeFormatted()
  341. {
  342. return DateUtils::dateIntervalFormat(($this->completedAt ?: new DateTime)->diff($this->startedAt));
  343. }
  344. /**
  345. * @return WorkflowState
  346. */
  347. public function getPrev()
  348. {
  349. return $this->prev;
  350. }
  351. /**
  352. * @param WorkflowState $prev
  353. */
  354. public function setPrev(WorkflowState $prev = null)
  355. {
  356. $this->prev = $prev;
  357. }
  358. /**
  359. * @return WorkflowState
  360. */
  361. public function getNext()
  362. {
  363. return $this->next;
  364. }
  365. /**
  366. * @param WorkflowState $next
  367. */
  368. public function setNext(WorkflowState $next = null)
  369. {
  370. $this->next = $next;
  371. }
  372. /**
  373. * @return string
  374. */
  375. public function getInTransition()
  376. {
  377. return $this->inTransition;
  378. }
  379. /**
  380. * @param string $inTransition
  381. */
  382. public function setInTransition(string $inTransition)
  383. {
  384. $this->inTransition = $inTransition;
  385. }
  386. /**
  387. * @return string
  388. */
  389. public function getOutTransition()
  390. {
  391. return $this->outTransition;
  392. }
  393. /**
  394. * @param string $outTransition
  395. */
  396. public function setOutTransition(string $outTransition)
  397. {
  398. $this->outTransition = $outTransition;
  399. }
  400. /**
  401. * @return User
  402. */
  403. public function getStartedBy()
  404. {
  405. return $this->startedBy;
  406. }
  407. /**
  408. * @param User $startedBy
  409. */
  410. public function setStartedBy($startedBy = null)
  411. {
  412. $this->startedBy = $startedBy;
  413. }
  414. /**
  415. * @return User
  416. */
  417. public function getCompletedBy()
  418. {
  419. return $this->completedBy;
  420. }
  421. /**
  422. * @param User $completedBy
  423. */
  424. public function setCompletedBy($completedBy = null)
  425. {
  426. $this->completedBy = $completedBy;
  427. }
  428. /**
  429. * @return array
  430. */
  431. public function getMetadata()
  432. {
  433. return $this->metadata;
  434. }
  435. /**
  436. * @param array $metadata
  437. */
  438. public function setMetadata(array $metadata)
  439. {
  440. $this->metadata = $metadata;
  441. }
  442. /**
  443. * @param $key
  444. * @param $value
  445. * @return WorkflowState
  446. */
  447. public function addMetadata($key, $value)
  448. {
  449. $this->metadata[$key] = $value;
  450. return $this;
  451. }
  452. }