Skip to content

Commit

Permalink
minor #15510 [Documentation] Update docs/book/architecture/state_mach…
Browse files Browse the repository at this point in the history
…ine.rst (benbd5, GSadee)

This PR was merged into the 1.12 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.12 <!-- see the comment below -->                  |
| Bug fix?        | no                                                     |
| New feature?    | no                                                       |
| BC breaks?      | no                                                      |
| Deprecations?   | no <!-- don't forget to update the UPGRADE-*.md file --> |
| Related tickets |                       |
| License         | MIT                                                          |

<!--
 - Bug fixes must be submitted against the 1.12 branch
 - Features and deprecations must be submitted against the 1.13 branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->

Update docs/book/architecture/state_machine.rst

Commits
-------

1c530ff Update docs/book/architecture/state_machine.rst
7980a47 [Documentation] Remove duplicated callback from state machine
  • Loading branch information
GSadee committed Apr 16, 2024
2 parents 8e3cb3d + 7980a47 commit 62fb2ec
Showing 1 changed file with 53 additions and 26 deletions.
79 changes: 53 additions & 26 deletions docs/book/architecture/state_machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ How to configure states? Let's see on the example from **Checkout** state machin
cart: ~
addressed: ~
shipping_selected: ~
shipping_skipped: ~
payment_skipped: ~
payment_selected: ~
completed: ~
Expand All @@ -45,8 +47,8 @@ Having states configured we can have a transition between the ``cart`` state to
sylius_order_checkout:
transitions:
address:
from: [cart, addressed, shipping_selected, payment_selected] # here you specify which state is the initial
to: addressed # there you specify which state is final for that transition
from: [cart, addressed, shipping_selected, shipping_skipped, payment_selected, payment_skipped] # here you specify which state is the initial
to: addressed # there you specify which state is final for that transition
Callbacks
---------
Expand All @@ -65,9 +67,10 @@ Having a configured transition, you can attach a callback to it either before or
# callbacks may be called before or after specified transitions, in the checkout state machine we've got callbacks only after transitions
after:
sylius_process_cart:
on: ["address", "select_shipping", "select_payment"]
on: ["select_shipping", "address", "select_payment", "skip_shipping", "skip_payment"]
do: ["@sylius.order_processing.order_processor", "process"]
args: ["object"]
priority: -200
Configuration
-------------
Expand All @@ -76,8 +79,8 @@ In order to use a state machine, you have to define a graph beforehand.
A graph is a definition of states, transitions and optionally callbacks - all attached on an object from your domain.
Multiple graphs may be attached to the same object.

In **Sylius** the best example of a state machine is the one from checkout. It has five states available:
``cart``, ``addressed``, ``shipping_selected``, ``payment_selected`` and ``completed`` - which can be achieved by applying some transitions to the entity.
In **Sylius** the best example of a state machine is the one from checkout. It has seven states available:
``cart``, ``addressed``, ``shipping_selected``, ``shipping_skipped``, ``payment_skipped``, ``payment_selected`` and ``completed`` - which can be achieved by applying some transitions to the entity.
For example, when selecting a shipping method during the shipping step of checkout we should apply the ``select_shipping`` transition, and after that the state
would become ``shipping_selected``.

Expand All @@ -95,42 +98,66 @@ would become ``shipping_selected``.
cart: ~
addressed: ~
shipping_selected: ~
shipping_skipped: ~
payment_skipped: ~
payment_selected: ~
completed: ~
# list of all possible transitions:
transitions:
address:
from: [cart, addressed, shipping_selected, payment_selected] # here you specify which state is the initial
to: addressed # there you specify which state is final for that transition
from: [cart, addressed, shipping_selected, shipping_skipped, payment_selected, payment_skipped] # here you specify which state is the initial
to: addressed # there you specify which state is final for that transition
select_shipping:
from: [addressed, shipping_selected, payment_selected]
from: [addressed, shipping_selected, payment_selected, payment_skipped]
to: shipping_selected
skip_payment:
from: [shipping_selected, shipping_skipped]
to: payment_skipped
select_payment:
from: [payment_selected, shipping_selected]
from: [payment_selected, shipping_skipped, shipping_selected]
to: payment_selected
complete:
from: [payment_selected]
from: [payment_selected, payment_skipped]
to: completed
# list of all callbacks:
callbacks:
# callbacks may be called before or after specified transitions, in the checkout state machine we've got callbacks only after transitions
after:
sylius_process_cart:
on: ["address", "select_shipping", "select_payment"]
do: ["@sylius.order_processing.order_processor", "process"]
args: ["object"]
sylius_create_order:
on: ["complete"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object", "event", "'create'", "'sylius_order'"]
sylius_hold_inventory:
on: ["complete"]
do: ["@sylius.inventory.order_inventory_operator", "hold"]
args: ["object"]
sylius_assign_token:
on: ["complete"]
do: ["@sylius.unique_id_based_order_token_assigner", "assignTokenValueIfNotSet"]
args: ["object"]
sylius_process_cart:
on: ["select_shipping", "address", "select_payment", "skip_shipping", "skip_payment"]
do: ["@sylius.order_processing.order_processor", "process"]
args: ["object"]
priority: -200
sylius_create_order:
on: ["complete"]
do: ["@sm.callback.cascade_transition", "apply"]
args: ["object", "event", "'create'", "'sylius_order'"]
priority: -400
sylius_save_checkout_completion_date:
on: ["complete"]
do: ["object", "completeCheckout"]
args: ["object"]
priority: -300
sylius_skip_shipping:
on: ["address"]
do: ["@sylius.state_resolver.order_checkout", "resolve"]
args: ["object"]
priority: -100
sylius_skip_payment:
on: ["select_shipping"]
do: ["@sylius.state_resolver.order_checkout", "resolve"]
args: ["object"]
priority: -100
sylius_control_payment_state:
on: ["complete"]
do: ["@sylius.state_resolver.order_payment", "resolve"]
args: ["object"]
priority: -200
sylius_control_shipping_state:
on: ["complete"]
do: ["@sylius.state_resolver.order_shipping", "resolve"]
args: ["object"]
priority: -100
Learn more
----------
Expand Down

0 comments on commit 62fb2ec

Please sign in to comment.