<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Peter Kostandy</title>
    <description>The latest articles on Forem by Peter Kostandy (@peterkostandy).</description>
    <link>https://forem.com/peterkostandy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1083068%2F6e36bb56-4af8-47d8-ad2a-71b970061953.jpg</url>
      <title>Forem: Peter Kostandy</title>
      <link>https://forem.com/peterkostandy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/peterkostandy"/>
    <language>en</language>
    <item>
      <title>🚨 Avoiding the Single Point of Failure (SPOF) in Software Design 🚨</title>
      <dc:creator>Peter Kostandy</dc:creator>
      <pubDate>Tue, 08 Jul 2025 09:55:50 +0000</pubDate>
      <link>https://forem.com/peterkostandy/avoiding-the-single-point-of-failure-spof-in-software-design-8gm</link>
      <guid>https://forem.com/peterkostandy/avoiding-the-single-point-of-failure-spof-in-software-design-8gm</guid>
      <description>&lt;p&gt;&lt;strong&gt;🚨 Avoiding the #Single_Point_of_Failure (#SPOF) in Software Design 🚨&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In software architecture, a Single Point of Failure is any one component that, if it fails, brings down the entire system. Whether it’s a service, server, or piece of code, depending solely on one thing is risky.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🎯 Let’s make it real with a PHP example.&lt;/p&gt;

&lt;p&gt;Imagine you're using a class to handle logging across your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Logger {

  public function log($message) {

    file_put_contents('log.txt', $message . PHP_EOL, FILE_APPEND);

  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And across your code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$logger = new Logger();

$logger-&amp;gt;log("Something happened");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If log.txt becomes unavailable (e.g., disk full, file permission denied), your whole logging system fails, and maybe even "crashes your app".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔧 Solution: Apply the #Strategy #Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of relying on one logging method (file logging), we can design our system to be more resilient:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface LoggerInterface {

  public function log($message);

}



class FileLogger implements LoggerInterface {

  public function log($message) {

    file_put_contents('log.txt', $message . PHP_EOL, FILE_APPEND);

  }

}



class DatabaseLogger implements LoggerInterface {

  public function log($message) {

    // Assume $pdo is a valid PDO instance

    global $pdo;

    $stmt = $pdo-&amp;gt;prepare("INSERT INTO logs (message) VALUES (:msg)");

    $stmt-&amp;gt;execute(['msg' =&amp;gt; $message]);

  }

}



class LoggerManager {

  private array $loggers;



  public function __construct(array $loggers) {

    $this-&amp;gt;loggers = $loggers;

  }



  public function log($message) {

    foreach ($this-&amp;gt;loggers as $logger) {

      try {

        $logger-&amp;gt;log($message);

      } catch (Exception $e) {

        // Handle individual logger failure silently

      }

    }

  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$manager = new LoggerManager([new FileLogger(), new DatabaseLogger()]);

$manager-&amp;gt;log("App started");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ If the file logger fails, the DB logger still works, so there is no single point of failure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;💡 Key takeaway: Always design for redundancy. Don’t put all your reliability eggs in one basket, "especially in #production".&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Have you encountered a SPOF in your projects? How did you overcome it?&lt;/p&gt;

&lt;h1&gt;
  
  
  SoftwareArchitecture #PHP #DesignPatterns #CleanCode #TechLeadership #SPOF #SinglePointOfFailure #PHPDevelopers #CleanCode #DesignPatterns #ResilientDesign #BackendDevelopment #SoftwareArchitecture #CodeSmarter #DevLife #TechTips
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Mastering Cart Management with Laravel's Observer Pattern</title>
      <dc:creator>Peter Kostandy</dc:creator>
      <pubDate>Thu, 24 Aug 2023 08:13:37 +0000</pubDate>
      <link>https://forem.com/peterkostandy/mastering-cart-management-with-laravels-observer-pattern-1dod</link>
      <guid>https://forem.com/peterkostandy/mastering-cart-management-with-laravels-observer-pattern-1dod</guid>
      <description>&lt;h2&gt;
  
  
  Implementing the Observer Pattern for "&lt;em&gt;Add to Cart&lt;/em&gt;" in Laravel:
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;Observer pattern&lt;/em&gt; is a powerful design pattern that allows you to create &lt;em&gt;loosely coupled&lt;/em&gt; components that can respond to changes in the state of an object.&lt;br&gt;
Applying this pattern to a common scenario like "Add to Cart" functionality in Laravel can lead to a more modular and maintainable codebase.&lt;/p&gt;

&lt;p&gt;In this guide, we'll walk you through implementing the Observer pattern to handle the "&lt;strong&gt;Add to Cart&lt;/strong&gt;" feature in a Laravel application. We'll explain every step and give you code samples to help you understand the idea and use it effectively...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Define the Subject and Observers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, we'll define the "Subject", which is the object that maintains a list of its dependents (observers) and notifies them of any state changes. In our case, &lt;em&gt;the cart will act as the subject&lt;/em&gt;, and items added to the cart will trigger notifications to the observers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class Cart extends Model
{
    protected $items;

    public function __construct()
    {
        $this-&amp;gt;items = new Collection();
    }

    public function addItem($item)
    {
        $this-&amp;gt;items-&amp;gt;push($item);
        $this-&amp;gt;notifyObservers();
    }

    public function notifyObservers()
    {
        foreach ($this-&amp;gt;observers as $observer) {
            $observer-&amp;gt;update($this);
        }
    }
}

interface CartObserver
{
    public function update(Cart $cart);
}

class PriceObserver implements CartObserver
{
    public function update(Cart $cart)
    {
        // Update total price logic here
    }
}

class InventoryObserver implements CartObserver
{
    public function update(Cart $cart)
    {
        // Adjust inventory logic here
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Create the Observers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll create the observers that will respond to changes in the cart's state. These observers can include &lt;em&gt;actions&lt;/em&gt; like &lt;em&gt;updating the total price&lt;/em&gt;, &lt;em&gt;displaying a notification&lt;/em&gt;, or &lt;em&gt;adjusting inventory&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$priceObserver = new PriceObserver();
$inventoryObserver = new InventoryObserver();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Implement the Observer Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll integrate the observers with the cart using Laravel's event system. We'll demonstrate how to define events, listeners, and dispatch them when items are added to the cart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$cart = new Cart();
$cart-&amp;gt;attach($priceObserver);
$cart-&amp;gt;attach($inventoryObserver);

// Adding an item to the cart triggers observers
$cart-&amp;gt;addItem($item);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Register Observers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll show you how to register the observers with the cart. This step is crucial to establish the connection between the subject (&lt;strong&gt;cart&lt;/strong&gt;) and the &lt;strong&gt;observers&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Cart extends Model
{
    protected $items;
    protected $observers;

    public function __construct()
    {
        $this-&amp;gt;items = new Collection();
        $this-&amp;gt;observers = new Collection();
    }

    public function attach(CartObserver $observer)
    {
        $this-&amp;gt;observers-&amp;gt;push($observer);
    }

    // Other methods as before...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Test the Implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Testing is vital to ensure that our Observer pattern implementation works as expected. We'll guide you through testing scenarios where adding items triggers the desired observer responses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Cart extends Model
{
    protected $items;
    protected $observers;

    public function __construct()
    {
        $this-&amp;gt;items = new Collection();
        $this-&amp;gt;observers = new Collection();
    }

    public function attach(CartObserver $observer)
    {
        $this-&amp;gt;observers-&amp;gt;push($observer);
    }

    // Other methods as before...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benefits of Using the Observer Pattern:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupling:&lt;/strong&gt; The Observer pattern promotes loose coupling between components, making your codebase more flexible and easier to maintain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Adding new functionalities or behaviors becomes more manageable without altering existing code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Observers can be reused across different parts of your application, enhancing code efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modularity:&lt;/strong&gt; Each observer handles a specific task, promoting code organization and clarity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of this guide, you'll have a clear understanding of how to implement the Observer pattern for "Add to Cart" functionality in Laravel. You'll be equipped with the knowledge to enhance your Laravel applications by applying this pattern to other scenarios as well.&lt;/p&gt;

&lt;p&gt;Unlock the power of the Observer pattern and create a more dynamic and responsive application. Let's dive in and transform your development approach today!&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;👉 Don't miss out on exclusive Laravel tips and tricks.😊&lt;br&gt;
👉 &lt;a href="https://www.linkedin.com/in/peterkostandy/" rel="noopener noreferrer"&gt;Peter B. Youssef&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Decoding DI vs. DI ✨</title>
      <dc:creator>Peter Kostandy</dc:creator>
      <pubDate>Fri, 18 Aug 2023 16:25:52 +0000</pubDate>
      <link>https://forem.com/peterkostandy/di-vs-di-39d9</link>
      <guid>https://forem.com/peterkostandy/di-vs-di-39d9</guid>
      <description>&lt;h2&gt;
  
  
  Dependency Injection (DI) vs. Dependency Inversion (DI)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Dependency Injection (DI):&lt;/strong&gt;&lt;br&gt;
Dependency Injection is a technique where external dependencies are passed to a class rather than the class creating them itself. It's about injecting dependencies from the outside. In other words, instead of a class creating its own dependencies, those dependencies are provided from the outside.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Dependency Injection:&lt;/strong&gt;&lt;br&gt;
Consider a Laravel controller that requires a service to handle business logic. Without DI, the controller would create the service instance internally. With DI, the service instance is passed to the controller's constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Without Dependency Injection&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$userService&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAllUsers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'users.index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// With Dependency Injection&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$userService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;UserService&lt;/span&gt; &lt;span class="nv"&gt;$userService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$userService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAllUsers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'users.index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Dependency Inversion (DI):&lt;/strong&gt;&lt;br&gt;
Dependency Inversion is a principle from the SOLID design principles. It suggests that high-level modules should not depend on low-level modules. Both should depend on abstractions. Additionally, abstractions should not depend on details; details should depend on abstractions. This means that instead of &lt;em&gt;tightly coupling&lt;/em&gt; components, &lt;em&gt;they should depend on abstractions (interfaces or abstract classes)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Dependency Inversion:&lt;/strong&gt;&lt;br&gt;
Suppose you have a Laravel controller that interacts with a specific repository implementation. Without applying DI, the controller would directly depend on that implementation. With DI and DI principle, you would use an interface as an abstraction for the repository, and then inject the concrete implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Without Dependency Inversion&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$userRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;userRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'users.index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// With Dependency Inversion&lt;/span&gt;
&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepositoryInterface&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;UserRepositoryInterface&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Fetch users from database&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$userRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;UserRepositoryInterface&lt;/span&gt; &lt;span class="nv"&gt;$userRepository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;userRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$userRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'users.index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'users'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;Dependency Injection&lt;/em&gt; example, you see how the UserService is injected into the controller's constructor.&lt;br&gt;
In the &lt;em&gt;Dependency Inversion&lt;/em&gt; example, the UserRepositoryInterface is used to define the contract, and the UserController depends on this abstraction instead of a concrete implementation.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Enhancing Laravel Applications with Action-Based Architecture 💡</title>
      <dc:creator>Peter Kostandy</dc:creator>
      <pubDate>Tue, 08 Aug 2023 17:13:00 +0000</pubDate>
      <link>https://forem.com/peterkostandy/enhancing-laravel-applications-with-action-based-architecture-b2e</link>
      <guid>https://forem.com/peterkostandy/enhancing-laravel-applications-with-action-based-architecture-b2e</guid>
      <description>&lt;p&gt;Laravel is a popular PHP framework that provides a structured and elegant way to build web applications. As your application grows in complexity, maintaining clean and organized code becomes crucial.&lt;br&gt;
One approach to enhance code organization is by using the concept of "&lt;strong&gt;Actions&lt;/strong&gt;".&lt;br&gt;
In this article, we will delve into what Laravel Actions are, why they are beneficial, and provide practical examples of how to implement them in your Laravel applications.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Are Laravel Actions?
&lt;/h2&gt;

&lt;p&gt;Laravel Actions are a design pattern that helps organize your application's logic into reusable and encapsulated classes.&lt;br&gt;
They promote the Single Responsibility Principle (SRP) by separating different functionalities into distinct classes, resulting in cleaner and more maintainable code. Actions allow you to encapsulate the steps required to perform a specific action, making your codebase more modular and easier to test.&lt;/p&gt;
&lt;h2&gt;
  
  
  Benefits of Using Actions:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modularity&lt;/strong&gt;: Actions encourage breaking down your application's functionalities into small, focused classes. This promotes code reuse and ensures that changes to one part of the application have minimal impact on other parts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability&lt;/strong&gt;: With Actions, your code becomes self-documenting. Each Action class represents a specific action or use case, making it easier for developers to understand the purpose of the code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Testability&lt;/strong&gt;: Actions are highly testable because they encapsulate a specific functionality. You can write unit tests for each Action to ensure that it behaves as expected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separation of Concerns&lt;/strong&gt;: Actions separate different concerns, such as validation, business logic, and database interactions. This leads to cleaner code where each class focuses on a single task.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Implementing Actions:
&lt;/h2&gt;

&lt;p&gt;Let's illustrate the concept of Laravel Actions with a practical example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1: Creating a User&lt;/strong&gt;&lt;br&gt;
Suppose you want to create a new user in your application. You can encapsulate this logic into an Action class.&lt;/p&gt;

&lt;p&gt;a) Create the Action class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:action CreateUserAction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) Open &lt;code&gt;CreateUserAction.php&lt;/code&gt; and define the logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace App\Actions;

use App\Models\User;

class CreateUserAction
{
    public function execute(array $data)
    {
        return User::create($data);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) Use the Action in a controller:&lt;/p&gt;

&lt;p&gt;namespace App\Http\Controllers;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Actions\CreateUserAction;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request, CreateUserAction $createUserAction)
    {
        $data = $request-&amp;gt;all();
        $user = $createUserAction-&amp;gt;execute($data);

        return response()-&amp;gt;json($user, 201);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2: Sending Email Notifications&lt;/strong&gt;&lt;br&gt;
Let's say you want to send email notifications to users. Create an Action to handle this functionality.&lt;/p&gt;

&lt;p&gt;a) Create the Action class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:action SendEmailNotificationAction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) Open &lt;code&gt;SendEmailNotificationAction.php&lt;/code&gt; and define the logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace App\Actions;

use Illuminate\Support\Facades\Mail;
use App\Mail\NotificationEmail;

class SendEmailNotificationAction
{
    public function execute($user, $message)
    {
        Mail::to($user-&amp;gt;email)-&amp;gt;send(new NotificationEmail($message));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) Use the Action in a controller or service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace App\Http\Controllers;

use App\Actions\SendEmailNotificationAction;
use App\Models\User;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function sendNotification(Request $request, SendEmailNotificationAction $sendEmailNotificationAction)
    {
        $user = User::find($request-&amp;gt;user_id);
        $message = $request-&amp;gt;message;

        $sendEmailNotificationAction-&amp;gt;execute($user, $message);

        return response()-&amp;gt;json(['message' =&amp;gt; 'Notification sent']);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3: Creating a Blog Post&lt;/strong&gt;&lt;br&gt;
Assume you want to create a blog post in your application using an Action.&lt;/p&gt;

&lt;p&gt;a) Create the Action class:&lt;/p&gt;

&lt;p&gt;Create an Action class named &lt;code&gt;CreateBlogPostAction&lt;/code&gt; using the artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:action CreateBlogPostAction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b) Open &lt;code&gt;CreateBlogPostAction.php&lt;/code&gt; and define the  Action Logic:&lt;/p&gt;

&lt;p&gt;Open the CreateBlogPostAction.php file and define the logic for creating a new blog post. This may involve validating input, interacting with the database, and any other necessary steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Models\BlogPost;

class CreateBlogPostAction
{
    public function execute(array $data)
    {
        // Validate input data
        $validatedData = validator($data, [
            'title' =&amp;gt; 'required|string',
            'content' =&amp;gt; 'required|string',
        ])-&amp;gt;validate();

        // Create a new blog post
        return BlogPost::create($validatedData);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c) Use the Action in a controller:&lt;/p&gt;

&lt;p&gt;In your controller, you can use the Action to handle the creation of a new blog post:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Actions\CreateBlogPostAction;

class BlogPostController extends Controller
{
    public function store(Request $request, CreateBlogPostAction $createBlogPostAction)
    {
        $data = $request-&amp;gt;only(['title', 'content']);
        $blogPost = $createBlogPostAction-&amp;gt;execute($data);

        // Redirect or return response
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These examples demonstrate how to use Actions in various scenarios within a Laravel application.&lt;br&gt;
By encapsulating specific logic into Action classes, you can achieve better code organization, modularity, and testability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Considerations and Best Practices:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Injection:&lt;/strong&gt; In the example above, we used dependency injection to inject the &lt;code&gt;CreateBlogPostAction&lt;/code&gt; into the controller. This approach ensures that your controller remains focused on handling HTTP-related concerns while delegating the actual business logic to the Action class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Handling:&lt;/strong&gt; Consider adding error handling to your Actions. You can throw custom exceptions or return error messages based on the outcome of the Action's execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authorization and Validation:&lt;/strong&gt; Actions can encapsulate not only business logic but also authorization and validation rules. This keeps your controller methods concise and easy to understand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusable Actions:&lt;/strong&gt; As your application grows, you can reuse Actions across different parts of your application. For example, the same &lt;code&gt;CreateBlogPostAction&lt;/code&gt; could be used in an API endpoint and a web form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unit Testing:&lt;/strong&gt; Write comprehensive unit tests for your Action classes to ensure they function as expected. Laravel's testing tools make it straightforward to test individual components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Documentation:&lt;/strong&gt; Document your Action classes with clear descriptions of their purpose, input data, and expected outcomes. This helps other developers understand how to use them effectively.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Laravel Actions provide a structured and elegant approach to organizing your application's logic. By encapsulating specific actions or use cases into separate classes, you can achieve greater modularity, readability, and testability in your codebase. Actions encourage adhering to best practices, such as the Single Responsibility Principle and the separation of concerns. As you continue developing your Laravel applications, consider adopting the Action design pattern to create cleaner, more maintainable, and more efficient code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep innovating and coding with a smile!&lt;/strong&gt; 😉👌&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>designpatterns</category>
      <category>designprinciples</category>
    </item>
    <item>
      <title>Laravel Pro Tips - Laravel Queue ✨</title>
      <dc:creator>Peter Kostandy</dc:creator>
      <pubDate>Sat, 27 May 2023 11:21:14 +0000</pubDate>
      <link>https://forem.com/peterkostandy/laravel-pro-tips-laravel-queue-2bcp</link>
      <guid>https://forem.com/peterkostandy/laravel-pro-tips-laravel-queue-2bcp</guid>
      <description>&lt;p&gt;✨ &lt;strong&gt;Unlocking Efficiency and Scalability: Harnessing the Power of Laravel Queue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;📂 &lt;em&gt;From "&lt;a href="https://www.linkedin.com/feed/hashtag/?keywords=laravel_pro_tips" rel="noopener noreferrer"&gt;Laravel Pro Tips&lt;/a&gt;" series,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;• Laravel queue is a powerful tool that allows developers to perform time-consuming tasks in the background, freeing up server resources and improving application performance. Here are some examples of how Laravel queue can be used in real-life scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Sending emails: When sending emails to a large number of recipients, it can take a lot of time and resources to process each email individually. By using the Laravel queue, developers can send emails in the background, allowing users to continue using the application without any delays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Processing payments: When processing payments, it's important to ensure that transactions are completed quickly and securely. By using the Laravel queue, developers can process payments in the background, reducing the risk of errors or delays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generating reports: Generating reports can be a time-consuming task that requires a lot of server resources. By using the Laravel queue, developers can generate reports in the background, allowing users to continue using the application without any delays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Image processing: When uploading and processing images on a website or application, it can take a lot of time and resources to resize or edit each image individually. By using the Laravel queue, developers can process images in the background, allowing users to continue using the application without any delays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data analysis: When performing data analysis on large datasets, it can take a lot of time and resources to process each data point individually. By using the Laravel queue, developers can perform data analysis in the background, allowing users to continue using the application without any delays.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;• Overall, #Laravel_Queue is an essential tool for improving application performance and user experience by offloading time-consuming tasks to the background.&lt;/p&gt;

&lt;p&gt;👉 Don't miss out on exclusive Laravel tips and tricks. Follow my LinkedIn account today! 😊&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://www.linkedin.com/in/peterkostandy/" rel="noopener noreferrer"&gt;Peter B. Youssef&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>queue</category>
      <category>laravelprotips</category>
      <category>webdev</category>
    </item>
    <item>
      <title>5 tips for writing efficient PHP code 💡💹</title>
      <dc:creator>Peter Kostandy</dc:creator>
      <pubDate>Mon, 15 May 2023 14:12:54 +0000</pubDate>
      <link>https://forem.com/peterkostandy/5-tips-for-writing-efficient-php-code-1edi</link>
      <guid>https://forem.com/peterkostandy/5-tips-for-writing-efficient-php-code-1edi</guid>
      <description>&lt;p&gt;&lt;strong&gt;5 tips for writing efficient PHP code 💡💹&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;• Use Proper Variable Scope&lt;br&gt;
• Minimize Database Queries&lt;br&gt;
• Optimize Loops&lt;br&gt;
• Use Built-in Functions&lt;br&gt;
• Use Opcode Caching&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For more information :&lt;/em&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/feed/update/urn:li:activity:7062033419655888896/" rel="noopener noreferrer"&gt;https://www.linkedin.com/feed/update/urn:li:activity:7062033419655888896/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By following these tips, you can improve the efficiency and performance of your PHP code, resulting in faster and more responsive applications.&lt;/p&gt;

</description>
      <category>php</category>
      <category>cleancode</category>
      <category>oop</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
