1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mothership
22: * @package Mothership_StateMachine
23: * @author Maurizio Brioschi <brioschi@mothership.de>
24: * @copyright Copyright (c) 2015 Mothership GmbH
25: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26: * @link http://www.mothership.de/
27: */
28:
29: namespace Mothership\StateMachine;
30:
31:
32: use Mothership\Exception\StateMachine\StatusException;
33: use Mothership\Exception\StateMachine\TransitionException;
34: use Mothership\Exception\WorkflowException;
35: use Mothership\StateMachine\StateMachine;
36: use Mothership\StateMachine\TransitionInterface;
37:
38: class Transition implements TransitionInterface
39: {
40:
41: protected $name;
42: protected $status;
43: protected $transition_from;
44: protected $hasCondition;
45: protected $condition;
46:
47: public function __construct(StatusInterface $status, $transition_from)
48: {
49: $this->status = $status;
50: $this->name = $status->getName();
51: if (!is_array($transition_from)) {
52: $this->transiction_from = $transition_from;
53: $this->hasCondition = false;
54: $this->condition = null;
55: } else {
56: $this->transiction_from = $transition_from['status'];
57: $this->hasCondition = true;
58: $this->condition = $transition_from['result'];
59: }
60: }
61:
62: /**
63: * Returns the state resulting of this transition
64: *
65: * @return StatusInterface
66: */
67: public function getStatus()
68: {
69: return $this->status;
70: }
71:
72:
73: /**
74: * @return mixed
75: * @throws TransitionException
76: */
77: public function process()
78: {
79: try {
80: $method = $this->getMethodToRun();
81: $result = $this->getStatus()->getWorkflow()->$method();
82: $this->getStatus()->setInternalStatus($result);
83: return $this->getStatus();
84: } catch (WorkflowException $ex) {
85: throw new TransitionException("error processing transiction " . $this->getName(), 100, $ex,
86: $this->getStatus()->getWorkflow()->getOutput());
87: }
88: }
89:
90: /**
91: * Returns the name of the transition
92: *
93: * @return string
94: */
95: public function getName()
96: {
97: return $this->name;
98: }
99:
100: /**
101: * Get the starting point of the transiction
102: * @return mixed
103: */
104: public function getTransitionFrom()
105: {
106: return $this->transiction_from;
107: }
108:
109: /**
110: * ending point of the transiction
111: * @return mixed
112: */
113: public function getTransitionTo()
114: {
115: return $this->name;
116: }
117:
118: /**
119: * method that will be execute in the workflow
120: * @return mixed
121: */
122: public function getMethodToRun()
123: {
124: /**
125: * @todo if we want to have different switch we must parametrize in this point
126: */
127: return $this->name;
128: }
129:
130: /**
131: * If the transition has a condition to be executed
132: * @return bool
133: */
134: public function hasCondition()
135: {
136: return $this->hasCondition;
137: }
138:
139: /**
140: * Get the condition to be executed
141: * @return mixed
142: */
143: public function getCondition()
144: {
145: return $this->condition;
146: }
147: }
148: