<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sakshi Goraniya on Medium]]></title>
        <description><![CDATA[Stories by Sakshi Goraniya on Medium]]></description>
        <link>https://medium.com/@sakshigoraniya13?source=rss-d1836beb6000------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4TITyJImOX3H7AMNHnOXtA.jpeg</url>
            <title>Stories by Sakshi Goraniya on Medium</title>
            <link>https://medium.com/@sakshigoraniya13?source=rss-d1836beb6000------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:58:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sakshigoraniya13/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unleashing the Power of Finite State Machines with Everlution/fsm - Symfony]]></title>
            <link>https://medium.com/simform-engineering/unleashing-the-power-of-finite-state-machines-with-everlution-fsm-symfony-e9841b233f9a?source=rss-d1836beb6000------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9841b233f9a</guid>
            <category><![CDATA[doctrine]]></category>
            <category><![CDATA[event-driven-systems]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[finite-state-machine]]></category>
            <dc:creator><![CDATA[Sakshi Goraniya]]></dc:creator>
            <pubDate>Tue, 02 Jan 2024 05:45:29 GMT</pubDate>
            <atom:updated>2024-01-02T05:45:29.947Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Elevate app performance with Finite State Machines for a responsive user experience and heightened visibility in dynamic workflows.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ShFriJnHtlbrrAXqPxe2_Q.png" /></figure><h4><strong>What is a Finite State Machine?</strong></h4><p>A Finite State Machine (FSM) is a powerful tool that models and manages the state transitions of objects in a system. It is particularly useful in event-driven systems where entities exist in various states.</p><p><strong>Benefits and Usage:</strong></p><p>A Finite State Machine (FSM) is a powerful tool that models and manages the state transitions of objects in a system and has versatile applications across fields like:</p><ul><li>Software Engineering: User Interface Design, Compiler Construction</li><li>Embedded Systems: Process Control, Communication Protocols</li><li>Networking: Protocol Design (e.g., TCP)</li><li>Hardware Design: Digital Circuit Design</li><li>Game Development: Character AI</li><li>Automated Systems: Manufacturing Processes, Traffic Control</li><li>Business Process Modeling: Workflow Management</li><li>Natural Language Processing: Language Parsing</li><li>Telecommunications: Call Processing</li></ul><p>In this tutorial, we’ll explore how to integrate and implement a robust Finite State Machine in your Symfony project using the everlution/fsm library. This library is lightweight, easy to use, and perfect for Symfony applications dealing with stateful entities.</p><p>You can use this library in your Symfony, Laravel, or any PHP project.</p><h3>Implementing Finite State Machine in Symfony</h3><h4><strong>Prerequisites</strong></h4><p>Before we begin, ensure you have a Symfony project set up and Composer installed.</p><h4><strong>Step 1: Install everlution/fsm</strong></h4><p>Use Composer to install everlution/fsm. Open your terminal and run:</p><pre>composer require everlution/fsm</pre><h4>Step 2: Create a Symfony Entity</h4><p>Let’s assume you want to manage the state of an Task entity in your Symfony application. Create a Task entity with the necessary properties and annotations:</p><pre>// src/Entity/Task.php<br><br>namespace App\Entity;<br><br>use Doctrine\ORM\Mapping as ORM;<br>use Everlution\Fsm\FsmTrait;<br><br>/**<br> * @ORM\Entity<br> */<br>class Task <br>{<br>    use FsmTrait;<br><br>    // ... (Existing properties)<br><br>    public function __construct()<br>    {<br>        // Initialize the state machine with the initial state<br>        $this-&gt;initializeFsm(&#39;pending&#39;);<br>    }<br>}</pre><h4><strong>Step 3: Finite State Machine Configuration</strong></h4><p>Now, integrate FSMs into the Task entity with a simple configuration. For the sake of this example, we&#39;ll define two states (pending and completed) and a transition (complete_task) that transitions a task from the pending state to the completed state.</p><pre>// src/Fsm/TaskFsm.php<br><br>use Everlution\Fsm\Model\Event;<br>use Everlution\Fsm\Model\Guard;<br><br>class TaskFsm extends AbstractFsm<br>{<br>    // ... (Existing code)<br><br>    private function initTransitions()<br>    {<br>        $this-&gt;addTransition(<br>            new Transition(<br>                self::TRANSITION_COMPLETE_TASK,<br>                self::STATE_PENDING,<br>                self::STATE_COMPLETED,<br>                [<br>                    new Guard(<br>                        &#39;can_complete_task&#39;,<br>                        function (Task $task) {<br>                            // Add custom logic to determine if the task can be completed<br>                            return !$task-&gt;isArchived();<br>                        }<br>                    ),<br>                ],<br>                [<br>                    new Event(<br>                        &#39;task_completed&#39;,<br>                        function (Task $task) {<br>                            // Add custom logic to execute when the task is completed<br>                            $task-&gt;setCompletedAt(new \DateTime());<br>                        }<br>                    ),<br>                ]<br>            )<br>        );<br>    }<br><br>    // ... (Existing code)<br>}</pre><p>In this configuration, we’ve added a guard (can_complete_task) to the TRANSITION_COMPLETE_TASK transition, ensuring that the task can only be completed if it is not archived. Additionally, an event (task_completed) is triggered when the task is completed, allowing you to execute custom logic.</p><h4>Step 4: Symfony Controller Integration</h4><p>Enhance the Symfony controller by adding more sophisticated error handling, authorization checks, and a dedicated view for displaying completed tasks.</p><pre><br><br>// src/Controller/TaskController.php<br><br>class TaskController extends AbstractController<br>{<br>    /**<br>     * @Route(&quot;/task/{id}/complete&quot;, name=&quot;task_complete&quot;, methods={&quot;POST&quot;})<br>     */<br>    public function completeTask(Task $task): Response<br>    {<br>        // Ensure the user has permission to complete tasks<br>        $this-&gt;denyAccessUnlessGranted(&#39;COMPLETE_TASK&#39;, $task);<br><br>        try {<br>            // Use the FSM to transition the task to the completed state<br>            $fsm = $this-&gt;get(TaskFsm::class);<br>            $fsm-&gt;apply($task, TaskFsm::TRANSITION_COMPLETE_TASK);<br><br>            // Persist the changes to the database<br>            $entityManager = $this-&gt;getDoctrine()-&gt;getManager();<br>            $entityManager-&gt;flush();<br><br>            // Redirect to the task details page<br>            return $this-&gt;redirectToRoute(&#39;task_show&#39;, [&#39;id&#39; =&gt; $task-&gt;getId()]);<br>        } catch (\Exception $e) {<br>            // Handle transition failure, log the error, and display a user-friendly message<br>            $this-&gt;addFlash(&#39;error&#39;, &#39;Failed to complete the task.&#39;);<br>            $this-&gt;get(&#39;logger&#39;)-&gt;error(&#39;Failed to complete task: &#39; . $e-&gt;getMessage());<br><br>            // Redirect to the task details page<br>            return $this-&gt;redirectToRoute(&#39;task_show&#39;, [&#39;id&#39; =&gt; $task-&gt;getId()]);<br>        }<br>    }<br><br>    /**<br>     * @Route(&quot;/task/{id}&quot;, name=&quot;task_show&quot;, methods={&quot;GET&quot;})<br>     */<br>    public function show(Task $task): Response<br>    {<br>        // Render the task details page with appropriate handling for completed tasks<br>        return $this-&gt;render(&#39;task/show.html.twig&#39;, [<br>            &#39;task&#39; =&gt; $task,<br>            &#39;isCompleted&#39; =&gt; $task-&gt;getState() === TaskFsm::STATE_COMPLETED,<br>        ]);<br>    }<br>}</pre><h3><strong>Key Takeaways</strong></h3><ol><li><strong>Enhanced Robustness and Maintainability: Fsm’s </strong>Advanced features like guards, events, and improved error handling elevate the overall robustness and maintainability of Finite State Machines in Symfony applications.</li><li><strong>Improved User Experience and Workflow Visibility</strong>: The strategic enhancement not only ensures a responsive user experience but also increases visibility within dynamic workflows, contributing to a more streamlined and effective application.</li><li><strong>Synergizing everlution/fsm and Symfony Features:</strong> The combination of the everlution/fsm library with Symfony’s powerful features forms a potent toolkit for managing complex state transitions in PHP projects.</li><li><strong>Empowerment for Developers</strong>: This approach empowers developers to create flexible and efficient event-driven systems that can adapt to diverse application requirements, providing a solid foundation for future development.</li><li><strong>Confidence in State Transitions</strong>: A comprehensive approach, guided by everlution/fsm library and Symfony’s documentation on FSM integration, instills confidence in developers for navigating and controlling state transitions in Symfony-based PHP applications.</li></ol><h3><strong>Conclusion</strong></h3><p>By integrating everlution/fsm with Symfony, you lay a solid foundation for managing state transitions in PHP projects. You can customize this example to fit your project’s specific needs. For detailed guidance, refer to <a href="https://github.com/everlution/fsm">everlution/fsm</a> and <a href="https://symfony.com/doc/current/workflow/workflow-and-state-machine.html">Symfony’s documentation</a> on FSM integration.</p><blockquote><strong>Follow </strong><a href="https://medium.com/simform-engineering"><strong>Simform Engineering</strong></a><strong> to keep yourself updated with the latest trends in the technology horizon. Follow us: </strong><a href="https://twitter.com/simform"><strong>Twitter</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/simform/"><strong>LinkedIn</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9841b233f9a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/simform-engineering/unleashing-the-power-of-finite-state-machines-with-everlution-fsm-symfony-e9841b233f9a">Unleashing the Power of Finite State Machines with Everlution/fsm - Symfony</a> was originally published in <a href="https://medium.com/simform-engineering">Simform Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>