Skip to content

Instantly share code, notes, and snippets.

View mmouih's full-sized avatar

Mounir Mouih mmouih

View GitHub Profile
@mmouih
mmouih / HandlerInterface
Created January 5, 2024 00:23
Handlers Interface
<?php
namespace App\Handler;
use App\Payload\PayloadInterface;
interface HandlerInterface
{
public function handle(PayloadInterface $payload);
}
@mmouih
mmouih / ControllersTest.php
Last active January 5, 2024 01:01
Controllers Tests with pest
<?php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
arch('controllers must has "Controller" as suffix')
->expect('App\Controller')
->toHaveSuffix('Controller');
arch(sprintf('constroller must extends %s', AbstractController::class))
->expect('App\Controller')
@mmouih
mmouih / DumpsTest.php
Created January 5, 2024 00:29
Forgotten dumps tests
<?php
arch('Do not forget dumps in your production code')
->expect(['dd', 'dump', 'exit', 'die', 'print_r', 'var_dump', 'echo', 'print'])
->not
->toBeUsed();
@mmouih
mmouih / HandlersTest.php
Created January 5, 2024 00:32
Testing handlers
<?php
use App\Handler\CreateOrUpdateUserHandler;
use App\Handler\HandlerInterface;
arch(sprintf('CreateOrUpdateUserHandler must implement %s', HandlerInterface::class))
->expect(CreateOrUpdateUserHandler::class)
->toImplement(HandlerInterface::class);
arch('handlers must be final')
@mmouih
mmouih / PayloadTest.php
Last active January 5, 2024 01:18
Payloads Test
<?php
use App\Payload\PayloadInterface;
arch('Payloads should be readonly')
->expect('App\Payload')
->classes()
->toBeReadonly();
arch('Payloads must implement ' . PayloadInterface::class)
@mmouih
mmouih / User.php
Created January 5, 2024 01:21
User Payload implements an payload interface
<?php
namespace App\Payload;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Type;
readonly class User implements PayloadInterface
{

Consider the following business requirements:

  1. Loyalty Discount:

    • Condition: The system should provide a loyalty discount based on the client's loyalty tier.
    • Requirement: Clients with a "Gold" loyalty tier should receive a 10% discount, and clients with a "Silver" loyalty tier should receive a 5% discount.
  2. Purchase History Discount:

    • Condition: The system should offer an additional discount based on the client's purchase history.
    • Requirement: Clients with a purchase history greater than 5 should receive an extra 8% discount.
@mmouih
mmouih / OriginalComplexPaymentService.php
Last active January 14, 2024 19:07
Complex none-refactored, none-maintainable payment service
<?php
namespace App\Service;
use App\Entity\Client;
use Service\TransactionService;
class PaymentService
{
public function __construct(private TransactionService $transactionService)
<?php
namespace App\Service;
use App\Entity\Client;
use App\Entity\LoyaltyTier;
use App\Entity\Subscription;
use Service\TransactionService;
class PaymentService
@mmouih
mmouih / PaymentProcessor.php
Created January 14, 2024 19:30
PaymentProcessor Interface
<?php
namespace App\Domain\Payment;
use App\Entity\Client;
interface PaymentProcessor
{
public function handle(float $amount, Client $client): float;
}