|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright (c) KUBO Atsuhiro <[email protected]>, |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This file is part of PHPMentorsWorkflowerBundle. |
| 7 | + * |
| 8 | + * This program and the accompanying materials are made available under |
| 9 | + * the terms of the BSD 2-Clause License which accompanies this |
| 10 | + * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PHPMentors\WorkflowerBundle\Process; |
| 14 | + |
| 15 | +use PHPMentors\Workflower\Process\Process; |
| 16 | + |
| 17 | +/** |
| 18 | + * @since Class available since Release 1.3.0 |
| 19 | + */ |
| 20 | +class ProcessFactory |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + private $processes = array(); |
| 26 | + |
| 27 | + /** |
| 28 | + * @param Process $process |
| 29 | + */ |
| 30 | + public function addProcess(Process $process) |
| 31 | + { |
| 32 | + $workflowContext = $process->getWorkflowContext(); |
| 33 | + assert($workflowContext instanceof WorkflowContext); |
| 34 | + |
| 35 | + $this->processes[$workflowContext->getWorkflowContextId()][$workflowContext->getWorkflowId()] = $process; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param int|string $workflowContextId |
| 40 | + * @param int|string $workflowId |
| 41 | + * |
| 42 | + * @return Process |
| 43 | + * |
| 44 | + * @throws \InvalidArgumentException |
| 45 | + */ |
| 46 | + public function create($workflowContextId, $workflowId) |
| 47 | + { |
| 48 | + if (!array_key_exists($workflowContextId, $this->processes)) { |
| 49 | + throw new \InvalidArgumentException(sprintf('The workflow context "%s" is not found.', $workflowContextId)); |
| 50 | + } |
| 51 | + |
| 52 | + if (!array_key_exists($workflowId, $this->processes[$workflowContextId])) { |
| 53 | + throw new \InvalidArgumentException(sprintf('The workflow "%s" is not found in the context "%s".', $workflowId, $workflowContextId)); |
| 54 | + } |
| 55 | + |
| 56 | + return $this->processes[$workflowContextId][$workflowId]; |
| 57 | + } |
| 58 | +} |
0 commit comments