<?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: Valeriu Guțu</title>
    <description>The latest articles on Forem by Valeriu Guțu (@neowit).</description>
    <link>https://forem.com/neowit</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%2F895148%2Fafa3aa89-b734-4654-beda-b0b4d5a34146.jpg</url>
      <title>Forem: Valeriu Guțu</title>
      <link>https://forem.com/neowit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/neowit"/>
    <language>en</language>
    <item>
      <title>Make a simple Command Bus in PHP</title>
      <dc:creator>Valeriu Guțu</dc:creator>
      <pubDate>Wed, 01 Feb 2023 00:03:01 +0000</pubDate>
      <link>https://forem.com/neowit/make-a-simple-command-bus-in-php-197c</link>
      <guid>https://forem.com/neowit/make-a-simple-command-bus-in-php-197c</guid>
      <description>&lt;h3&gt;
  
  
  What is a Command Bus?
&lt;/h3&gt;

&lt;p&gt;A command bus is a design pattern used to manage and execute commands in a PHP application. It acts as a central dispatch mechanism for sending and handling commands, making it easier to manage the flow of requests in a system.&lt;/p&gt;

&lt;p&gt;In a command bus architecture, commands are objects that represent a specific request or action that needs to be performed. The command bus is responsible for receiving these commands and forwarding them to the appropriate handler for execution. The handler is a class that implements the logic for a specific type of command, and it returns the result of the command execution to the bus.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's a simple example of a command bus in PHP:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
interface CommandBus {
    public function handle(Command $command);
}

class SimpleCommandBus implements CommandBus {
    private $handlers = [];

    public function registerHandler(string $commandName, callable $handler) {
        $this-&amp;gt;handlers[$commandName] = $handler;
    }

    public function handle(Command $command) {
        $handler = $this-&amp;gt;handlers[get_class($command)];
        $handler($command);
    }
}

class DepositCommand {
    private $amount;

    public function __construct(float $amount) {
        $this-&amp;gt;amount = $amount;
    }

    public function getAmount(): float {
        return $this-&amp;gt;amount;
    }
}

class DepositCommandHandler {
    public function handle(DepositCommand $command) {
        // Deposit the funds
    }
}

$commandBus = new SimpleCommandBus();
$commandBus-&amp;gt;registerHandler(DepositCommand::class, [new DepositCommandHandler(), 'handle']);

$commandBus-&amp;gt;handle(new DepositCommand(100.0));
php?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;SimpleCommandBus&lt;/code&gt; implements the &lt;code&gt;CommandBus&lt;/code&gt; interface and acts as the central dispatch mechanism for commands. The &lt;code&gt;DepositCommand&lt;/code&gt; represents a request to deposit funds, and the &lt;code&gt;DepositCommandHandler&lt;/code&gt; implements the logic for handling the deposit request. The external code sends the command to the command bus, which forwards it to the appropriate handler for execution. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The command bus provides a clear and organized way to manage the flow of requests in the system, making it easier to maintain and extend.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>ai</category>
      <category>machinelearning</category>
      <category>discuss</category>
    </item>
    <item>
      <title>3 essential new features of PHP 8</title>
      <dc:creator>Valeriu Guțu</dc:creator>
      <pubDate>Fri, 27 Jan 2023 22:22:57 +0000</pubDate>
      <link>https://forem.com/neowit/3-essential-new-features-of-php-8-1hcm</link>
      <guid>https://forem.com/neowit/3-essential-new-features-of-php-8-1hcm</guid>
      <description>&lt;h2&gt;
  
  
  Constructor Property Promotion
&lt;/h2&gt;

&lt;p&gt;This new feature allows developers to define class properties directly within the constructor method, without having to declare them separately at the top of the class. This can make the code more concise and readable, as well as reduce the amount of duplicated code.&lt;/p&gt;

&lt;p&gt;In previous version of PHP, you would have to declare the properties at the top of the class, and then set the value of the properties in the constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Example {
    private string $name;
    private int $age;
    private bool $isActive;
    public function __construct(string $name, int $age, bool $isActive = true) {
        $this-&amp;gt;name = $name;
        $this-&amp;gt;age = $age;
        $this-&amp;gt;isActive = $isActive;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the code from above can change to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Example {
    public function __construct(
        private string $name,
        private int $age,
        private bool $isActive = true
    ) {}

    // ... other class methods ...
}

$example = new Example('John', 25);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Union Types
&lt;/h2&gt;

&lt;p&gt;In PHP 8, union types allow for specifying multiple types for a function or method parameter or return type. This allows for more specific type checking, and can help prevent errors caused by passing the wrong type of argument to a function. For example, a function that accepts either a string or an integer for a parameter could be declared like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function example(int|string $param) {
    // function body
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that the example function accepts either a string or an integer as the value of $param.&lt;/p&gt;

&lt;p&gt;Note that Union types are not supported for class and interface types, and also if you use the mixed type, the union types will be ignored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match Expressions
&lt;/h2&gt;

&lt;p&gt;Match expressions in PHP 8 provide a more concise and expressive way to perform pattern matching, as it allows checking the type of variable and perform different actions based on the type. For example, instead of writing an if-else or switch statement, you can use a match expression like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$message = match ($statusCode) {
    200, 300 =&amp;gt; null,
    400 =&amp;gt; 'not found',
    500 =&amp;gt; 'server error',
    default =&amp;gt; 'unknown status code',
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;There are many new features and improvements in PHP 8. You can go over all of them &lt;a href="https://kinsta.com/blog/php-8/#php-8-improvements-and-new-features"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>programming</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Single Action Controller - Pros and Cons for REST APIs</title>
      <dc:creator>Valeriu Guțu</dc:creator>
      <pubDate>Mon, 02 Jan 2023 14:19:53 +0000</pubDate>
      <link>https://forem.com/neowit/single-action-controller-pros-and-cons-for-rest-apis-2phc</link>
      <guid>https://forem.com/neowit/single-action-controller-pros-and-cons-for-rest-apis-2phc</guid>
      <description>&lt;h2&gt;
  
  
  What is a Single Action Controller?
&lt;/h2&gt;

&lt;p&gt;It is a Controller dedicated to a single action. &lt;/p&gt;

&lt;p&gt;For example: &lt;br&gt;
Instead of creating a single &lt;code&gt;UserController&lt;/code&gt; for all CRUD actions as methods and some additional, like filters and showing, you are creating a Controller for each action, which becomes &lt;code&gt;CreateUserController&lt;/code&gt;, &lt;code&gt;GetUserController&lt;/code&gt;, &lt;code&gt;FilterUserController&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://neutrondev.com/single-action-controller-in-laravel/" rel="noopener noreferrer"&gt;Single Action Controller in Laravel&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pros
&lt;/h2&gt;

&lt;p&gt;✅ has one job only&lt;br&gt;
✅ has a descriptive name, and it’s easy to understand what it does&lt;br&gt;
✅ is easy to change&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the cons for Single Action Controllers?
&lt;/h2&gt;

&lt;p&gt;I've read some articles and the only cons I've found is that you'll end up with “tons of controllers over time”. The author says that it's not a good idea if you’re aiming for scalability in terms of development. &lt;/p&gt;

&lt;h2&gt;
  
  
  Cons solution
&lt;/h2&gt;

&lt;p&gt;In fact, you can wrap controllers in folders by modules. By putting all user controllers under &lt;code&gt;User&lt;/code&gt; folder, you end up having a clean hierarchy of controllers sorted by modules and a bunch of Pros in addition that are making the architecture easy to maintain.&lt;/p&gt;

&lt;p&gt;What do you think about it?&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Breaking the ice on AWS Fundamentals</title>
      <dc:creator>Valeriu Guțu</dc:creator>
      <pubDate>Fri, 12 Aug 2022 14:36:00 +0000</pubDate>
      <link>https://forem.com/neowit/breaking-the-ice-on-aws-fundamentals-19b4</link>
      <guid>https://forem.com/neowit/breaking-the-ice-on-aws-fundamentals-19b4</guid>
      <description>&lt;p&gt;I start learning a course on Coursera. I'll share here my experience and notes.&lt;/p&gt;

&lt;p&gt;What advices do you have for an AWS junior?&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
