8000 [Workflow] Better code · mikeSimonson/symfony@f261af3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f261af3

Browse files
mikeSimonsonlyrixx
authored andcommitted
[Workflow] Better code
1 parent 2ae7f60 commit f261af3

21 files changed

+662
-110
lines changed

src/Symfony/Component/Workflow/Definition.php

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Workflow;
1313

14-
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
1514
use Symfony\Component\Workflow\Exception\LogicException;
1615

1716
/**
@@ -20,66 +19,118 @@
2019
*/
2120
class Definition
2221
{
22+
/**
23+
* @var Place[]
24+
*/
2325
private $places = array();
26+
27+
/**
28+
* @var Transition[]
29+
*/
2430
private $transitions = array();
31+
32+
/**
33+
* @var Place
34+
*/
2535
private $initialPlace;
2636

37+
38+
/**
39+
* Definition constructor.
40+
* @param Place[] $places
41+
* @param Transition[] $transitions
42+
*/
43+
public function __construct(array $places, array $transitions)
44+
{
45+
$this->addPlaces($places);
46+
$this->addTransitions($transitions);
47+
}
48+
49+
/**
50+
* @return Place|null
51+
*/
2752
public function getInitialPlace()
2853
{
2954
return $this->initialPlace;
3055
}
3156

57+
/**
58+
* @return Place[]|null
59+
*/
3260
public function getPlaces()
3361
{
3462
return $this->places;
3563
}
3664

65+
/**
66+
* @return Transition[]|null
67+
*/
3768
public function getTransitions()
3869
{
3970
return $this->transitions;
4071
}
4172

42-
public function setInitialPlace($name)
73+
/**
74+
* @param Place $place
75+
*/
76+
public function setInitialPlace(Place $place)
4377
{
44-
if (!isset($this->places[$name])) {
45-
throw new LogicException(sprintf('Place "%s" cannot be the initial place as it does not exist.', $name));
78+
if (!isset($this->places[$place->getName()])) {
79+
throw new LogicException(sprintf('Place "%s" cannot be the initial place as it does not exist.', $place->getName()));
4680
}
4781

48-
$this->initialPlace = $name;
82+
$this->initialPlace = $place;
4983
}
5084

51-
public function addPlace($name)
85+
/**
86+
* @param Place $place
87+
*/
88+
public function addPlace(Place $place)
5289
{
53-
if (!preg_match('{^[\w\d_-]+$}', $name)) {
54-
throw new InvalidArgumentException(sprintf('The place "%s" contains invalid characters.', $name));
55-
}
56-
5790
if (!count($this->places)) {
58-
$this->initialPlace = $name;
91+
$this->initialPlace = $place;
5992
}
6093

61-
$this->places[$name] = $name;
94+
$this->places[$place->getName()] = $place;
6295
}
6396

97+
/**
98+
* @param Place[] $places
99+
*/
64100
public function addPlaces(array $places)
65101
{
66102
foreach ($places as $place) {
67103
$this->addPlace($place);
68104
}
69105
}
70106

107+
/**
108+
* @param Transition[] $transitions
109+
* @throws LogicException
110+
*/
111+
public function addTransitions(array $transitions)
112+
{
113+
foreach ($transitions as $transition) {
114+
$this->addTransition($transition);
115+
}
116+
}
117+
118+
/**
119+
* @param Transition $transition
120+
* @throws LogicException
121+
*/
71122
public function addTransition(Transition $transition)
72123
{
73124
$name = $transition->getName();
74125

75126
foreach ($transition->getFroms() as $from) {
76-
if (!isset($this->places[$from])) {
127+
if (!isset($this->places[$from->getName()])) {
77128
throw new LogicException(sprintf('Place "%s" referenced in transition "%s" does not exist.', $from, $name));
78129
}
79130
}
80131

81132
foreach ($transition->getTos() as $to) {
82-
if (!isset($this->places[$to])) {
133+
if (!isset($this->places[$to->getName()])) {
83134
throw new LogicException(sprintf('Place "%s" referenced in transition "%s" does not exist.', $to, $name));
84135
}
85136
}

src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Workflow\Definition;
1515
use Symfony\Component\Workflow\Marking;
16+
use Symfony\Component\Workflow\Place;
17+
use Symfony\Component\Workflow\Transition;
1618

1719
/**
1820
* GraphvizDumper dumps a workflow as a graphviz file.
@@ -71,6 +73,7 @@ private function findPlaces(Definition $definition, Marking $marking = null)
7173
{
7274
$places = array();
7375

76+
/** @var Place $place */
7477
foreach ($definition->getPlaces() as $place) {
7578
$attributes = [];
7679
if ($place === $definition->getInitialPlace()) {
@@ -80,7 +83,7 @@ private function findPlaces(Definition $definition, Marking $marking = null)
8083
$attributes['color'] = '#FF0000';
8184
$attributes['shape'] = 'doublecircle';
8285
}
83-
$places[$place] = array(
86+
$places[$place->getName()] = array(
8487
'attributes' => $attributes,
8588
);
8689
}
@@ -150,18 +153,21 @@ private function findEdges(Definition $definition)
150153
{
151154
$dotEdges = array();
152155

156+
/** @var Transition $transition */
153157
foreach ($definition->getTransitions() as $transition) {
158+
/** @var Place $from */
154159
foreach ($transition->getFroms() as $from) {
155160
$dotEdges[] = array(
156-
'from' => $from,
161+
'from' => $from->getName(),
157162
'to' => $transition->getName(),
158163
'direction' => 'from',
159164
);
160165
}
166+
/** @var Place $to */
161167
foreach ($transition->getTos() as $to) {
162168
$dotEdges[] = array(
163169
'from' => $transition->getName(),
164-
'to' => $to,
170+
'to' => $to->getName(),
165171
'direction' => 'to',
166172
);
167173
}

src/Symfony/Component/Workflow/Event/Event.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,53 @@
2121
*/
2222
class Event extends BaseEvent
2323
{
24+
/**
25+
* @var object
26+
*/
2427
private $subject;
28+
29+
/**
30+
* @var Marking
31+
*/
2532
private $marking;
33+
34+
/**
35+
* @var Transition
36+
*/
2637
private $transition;
2738

39+
/**
40+
* Event constructor.
41+
* @param mixed $subject
42+
* @param Marking $marking
43+
* @param Transition $transition
44+
*/
2845
public function __construct($subject, Marking $marking, Transition $transition)
2946
{
3047
$this->subject = $subject;
3148
$this->marking = $marking;
3249
$this->transition = $transition;
3350
}
3451

52+
/**
53+
* @return Marking
54+
*/
3555
public function getMarking()
3656
{
3757
return $this->marking;
3858
}
3959

60+
/**
61+
* @return mixed
62+
*/
4063
public function getSubject()
4164
{
4265
return $this->subject;
4366
}
4467

68+
/**
69+
* @return Transition
70+
*/
4571
public function getTransition()
4672
{
4773
return $this->transition;

src/Symfony/Component/Workflow/EventListener/AuditTrailListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(LoggerInterface $logger)
2828
public function onLeave(Event $event)
2929
{
3030
foreach ($event->getTransition()->getFroms() as $place) {
31-
$this->logger->info(sprintf('leaving "%s" for subject of class "%s"', $place, get_class($event->getSubject())));
31+
$this->logger->info(sprintf('leaving "%s" for subject of class "%s"', $place->getName(), get_class($event->getSubject())));
3232
}
3333
}
3434

@@ -40,7 +40,7 @@ public function onTransition(Event $event)
4040
public function onEnter(Event $event)
4141
{
4242
foreach ($event->getTransition()->getTos() as $place) {
43-
$this->logger->info(sprintf('entering "%s" for subject of class "%s"', $place, get_class($event->getSubject())));
43+
$this->logger->info(sprintf('entering "%s" for subject of class "%s"', $place->getName(), get_class($event->getSubject())));
4444
}
4545
}
4646

src/Symfony/Component/Workflow/Marking.php

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,93 @@
1616
*
1717
* @author Grégoire Pineau <lyrixx@lyrixx.info>
1818
*/
19+
/**
20+
* Class Marking
21+
* @package Symfony\Component\Workflow
22+
*/
1923
class Marking
2024
{
25+
/**
26+
* @var string[]
27+
*/
2128
private $places = array();
2229

2330
/**
24-
* @param array $places keys are the place name and values should be 1
31+
* @param string[] $places keys are the place name and values should be 1
2532
*/
2633
public function __construct(array $places = array())
2734
{
2835
foreach ($places as $place => $nbToken) {
29-
$this->mark($place);
36+
$this->markPlaceNamed($place);
3037
}
3138
}
3239

33-
public function mark($place)
40+
/**
41+
* @param Place[] $places
42+
* @return Marking
43+
*/
44+
public static function fromPlaces(array $places = array())
3445
{
35-
$this->places[$place] = 1;
46+
$placeNames = [];
47+
foreach($places as $place) {
48+
$placeNames[$place->getName()] = 1;
49+
}
50+
return new self($placeNames);
3651
}
3752

38-
public function unmark($place)
53+
/**
54+
* @param Place $place place
55+
*/
56+
public function mark(Place $place)
3957
{
40-
unset($this->places[$place]);
58+
$this->places[$place->getName()] = 1;
4159
}
4260

43-
public function has($place)
61+
/**
62+
* @param Place $place place
63+
*/
64+
public function unmark(Place $place)
65+
{
66+
unset($this->places[$place->getName()]);
67+
}
68+
69+
/**
70+
* @param string $placename place name
71+
*/
72+
public function markPlaceNamed($placeName)
4473
{
45-
return isset($this->places[$place]);
74+
$this->places[$placeName] = 1;
4675
}
4776

77+
/**
78+
* @param string $placeName place name
79+
*/
80+
public function unmarkPlaceNamed($placeName)
81+
{
82+
unset($this->places[$placeName]);
83+
}
84+
85+
/**
86+
* @param Place $place
87+
* @return bool
88+
*/
89+
public function has(Place $place)
90+
{
91+
return isset($this->places[$place->getName()]);
92+
}
93+
94+
/**
95+
* @param string $place place name
96+
* @return bool
97+
*/
98+
public function hasPlaceNamed($name)
99+
{
100+
return isset($this->places[$name]);
101+
}
102+
103+
/**
104+
* @return Place[]
105+
*/
48106
public function getPlaces()
49107
{
50108
return $this->places;

0 commit comments

Comments
 (0)
0