Skip to content

Commit bc611d9

Browse files
lchruscieldiimpp
andauthored
refactor #857 Move state machine with bc-layer (loic425)
This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Commits ------- a8c3854 Move state machine e786dc3 Add bc-layer 6ee71dd Add specs f7dcc51 Update state machine class 13981c8 Fix Psalm & PHPStan errors 91e94ed Add pull request object on dummy da0a99c Apply suggestions from code review Co-authored-by: Dmitri Perunov <[email protected]>
2 parents 40a7c03 + da0a99c commit bc611d9

File tree

10 files changed

+233
-39
lines changed

10 files changed

+233
-39
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ parameters:
3131
- %currentWorkingDirectory%/src/Component/legacy/src/Model/*
3232
- %currentWorkingDirectory%/src/Component/legacy/src/Reflection/ClassReflection.php
3333
- %currentWorkingDirectory%/src/Component/legacy/src/Repository/*
34+
- %currentWorkingDirectory%/src/Component/legacy/src/StateMachine/*
3435
- %currentWorkingDirectory%/src/Component/legacy/src/Storage/*
3536
- %currentWorkingDirectory%/src/Component/legacy/src/Translation/*
3637
- %currentWorkingDirectory%/src/Component/legacy/tests/*

psalm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<file name="src/Component/legacy/src/Model/TimestampableTrait.php" />
2020
<file name="src/Component/legacy/src/Model/ToggleableTrait.php" />
2121
<file name="src/Component/legacy/src/Model/TranslatableTrait.php" />
22+
<file name="src/Component/legacy/src/StateMachine/StateMachine.php" />
23+
<file name="src/Component/legacy/src/StateMachine/StateMachineInterface.php" />
2224
<file name="src/Bundle/Controller/ControllerTrait.php" />
2325
<file name="src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineODMDriver.php" />
2426
<file name="src/Bundle/DependencyInjection/Driver/Doctrine/DoctrinePHPCRDriver.php" />

src/Bundle/Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</imports>
3131

3232
<parameters>
33-
<parameter key="sylius.state_machine.class">Sylius\Component\Resource\StateMachine\StateMachine</parameter>
33+
<parameter key="sylius.state_machine.class">Sylius\Resource\StateMachine\StateMachine</parameter>
3434
</parameters>
3535

3636
<services>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\Component\Resource\StateMachine;
15+
16+
use PhpSpec\ObjectBehavior;
17+
use Sylius\Component\Resource\StateMachine\StateMachine;
18+
use Sylius\Component\Resource\StateMachine\StateMachineInterface as LegacyStateMachineInterface;
19+
use Sylius\Resource\StateMachine\StateMachine as NewStateMachine;
20+
use Sylius\Resource\StateMachine\StateMachineInterface;
21+
use Sylius\Resource\Tests\Dummy\PullRequest;
22+
23+
final class StateMachineSpec extends ObjectBehavior
24+
{
25+
function let(): void
26+
{
27+
$this->beConstructedWith(new PullRequest(), [
28+
'graph' => 'pull_request',
29+
'property_path' => 'currentPlace',
30+
'places' => [
31+
'start',
32+
'test',
33+
],
34+
'transitions' => [
35+
'submit' => [
36+
'from' => ['start'],
37+
'to' => 'test',
38+
],
39+
],
40+
]);
41+
}
42+
43+
function it_is_initializable(): void
44+
{
45+
$this->shouldHaveType(StateMachine::class);
46+
}
47+
48+
function it_implements_state_machine_interface(): void
49+
{
50+
$this->shouldImplement(StateMachineInterface::class);
51+
}
52+
53+
function it_implements_legacy_state_machine_interface(): void
54+
{
55+
$this->shouldImplement(LegacyStateMachineInterface::class);
56+
}
57+
58+
function it_should_be_an_alias_of_state_machine(): void
59+
{
60+
$this->shouldImplement(NewStateMachine::class);
61+
}
62+
}

src/Component/legacy/src/StateMachine/StateMachine.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,10 @@
1313

1414
namespace Sylius\Component\Resource\StateMachine;
1515

16-
use SM\StateMachine\StateMachine as BaseStateMachine;
16+
class_exists(\Sylius\Resource\StateMachine\StateMachine::class);
1717

18-
final class StateMachine extends BaseStateMachine implements StateMachineInterface
19-
{
20-
public function getTransitionFromState(string $fromState): ?string
18+
if (false) {
19+
final class StateMachine extends \Sylius\Resource\StateMachine\StateMachine
2120
{
22-
foreach ($this->getPossibleTransitions() as $transition) {
23-
$config = $this->config['transitions'][$transition];
24-
if (in_array($fromState, $config['from'], true)) {
25-
return $transition;
26-
}
27-
}
28-
29-
return null;
30-
}
31-
32-
public function getTransitionToState(string $toState): ?string
33-
{
34-
foreach ($this->getPossibleTransitions() as $transition) {
35-
$config = $this->config['transitions'][$transition];
36-
if ($toState === $config['to']) {
37-
return $transition;
38-
}
39-
}
40-
41-
return null;
4221
}
4322
}

src/Component/legacy/src/StateMachine/StateMachineInterface.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,10 @@
1313

1414
namespace Sylius\Component\Resource\StateMachine;
1515

16-
use SM\StateMachine\StateMachineInterface as BaseStateMachineInterface;
16+
interface_exists(\Sylius\Resource\StateMachine\StateMachineInterface::class);
1717

18-
interface StateMachineInterface extends BaseStateMachineInterface
19-
{
20-
/**
21-
* Returns the possible transition from given state
22-
* Returns null if no transition is possible
23-
*/
24-
public function getTransitionFromState(string $fromState): ?string;
25-
26-
/**
27-
* Returns the possible transition to the given state
28-
* Returns null if no transition is possible
29-
*/
30-
public function getTransitionToState(string $toState): ?string;
18+
if (false) {
19+
interface StateMachineInterface extends \Sylius\Resource\StateMachine\StateMachineInterface
20+
{
21+
}
3122
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\Resource\StateMachine;
15+
16+
use PhpSpec\ObjectBehavior;
17+
use Sylius\Resource\StateMachine\StateMachine;
18+
use Sylius\Resource\Tests\Dummy\PullRequest;
19+
20+
final class StateMachineSpec extends ObjectBehavior
21+
{
22+
function let(): void
23+
{
24+
$this->beConstructedWith(new PullRequest(), [
25+
'graph' => 'pull_request',
26+
'property_path' => 'currentPlace',
27+
'places' => [
28+
'start',
29+
'test',
30+
],
31+
'transitions' => [
32+
'submit' => [
33+
'from' => ['start'],
34+
'to' => 'test',
35+
],
36+
],
37+
]);
38+
}
39+
40+
function it_is_initializable(): void
41+
{
42+
$this->shouldHaveType(StateMachine::class);
43+
}
44+
45+
function it_gets_transition_from_a_state(): void
46+
{
47+
$this->getTransitionFromState('start')->shouldReturn('submit');
48+
}
49+
50+
function it_gets_transition_to_a_state(): void
51+
{
52+
$this->getTransitionToState('test')->shouldReturn('submit');
53+
}
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Resource\StateMachine;
15+
16+
use SM\StateMachine\StateMachine as BaseStateMachine;
17+
18+
final class StateMachine extends BaseStateMachine implements StateMachineInterface
19+
{
20+
public function getTransitionFromState(string $fromState): ?string
21+
{
22+
foreach ($this->getPossibleTransitions() as $transition) {
23+
$config = $this->config['transitions'][$transition];
24+
if (in_array($fromState, $config['from'], true)) {
25+
return $transition;
26+
}
27+
}
28+
29+
return null;
30+
}
31+
32+
public function getTransitionToState(string $toState): ?string
33+
{
34+
foreach ($this->getPossibleTransitions() as $transition) {
35+
$config = $this->config['transitions'][$transition];
36+
if ($toState === $config['to']) {
37+
return $transition;
38+
}
39+
}
40+
41+
return null;
42+
}
43+
}
44+
45+
class_alias(StateMachine::class, \Sylius\Component\Resource\StateMachine\StateMachine::class);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Resource\StateMachine;
15+
16+
use SM\StateMachine\StateMachineInterface as BaseStateMachineInterface;
17+
18+
interface StateMachineInterface extends BaseStateMachineInterface
19+
{
20+
/**
21+
* Returns the possible transition from given state or null if no transition is possible
22+
*/
23+
public function getTransitionFromState(string $fromState): ?string;
24+
25+
/**
26+
* Returns the possible transition to the given state or null if no transition is possible
27+
*/
28+
public function getTransitionToState(string $toState): ?string;
29+
}
30+
31+
class_alias(StateMachineInterface::class, \Sylius\Component\Resource\StateMachine\StateMachineInterface::class);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Resource\Tests\Dummy;
15+
16+
class PullRequest
17+
{
18+
private string $currentPlace = 'start';
19+
20+
public function getCurrentPlace(): string
21+
{
22+
return $this->currentPlace;
23+
}
24+
25+
public function setCurrentPlace(string $currentPlace): void
26+
{
27+
$this->currentPlace = $currentPlace;
28+
}
29+
}

0 commit comments

Comments
 (0)