Skip to content

Commit fe69339

Browse files
committed
add Chain of Responsabily Pattern
2 parents 6d52f2d + 3b2f5d2 commit fe69339

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\ChainOfResponsability;
6+
7+
abstract class ApproveHandler implements SaleHandler
8+
{
9+
private ?ApproveHandler $nextHandler = null;
10+
11+
public final function setNext(ApproveHandler $nextHandler): ApproveHandler
12+
{
13+
$this->nextHandler = $nextHandler;
14+
return $nextHandler;
15+
}
16+
17+
public function processSale(Sale $sale): void
18+
{
19+
if ($this->nextHandler) {
20+
$this->nextHandler->processSale($sale);
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\ChainOfResponsability;
6+
7+
// Diretor
8+
final class DirectorHandler extends ApproveHandler
9+
{
10+
public function processSale(Sale $sale): void
11+
{
12+
if ($sale->price >= 30_000) {
13+
printf("Sale approved by director with price %.2f\n", $sale->price);
14+
} else {
15+
parent::processSale($sale);
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\ChainOfResponsability;
6+
7+
// gerente
8+
final class ManagerHandler extends ApproveHandler
9+
{
10+
public function processSale(Sale $sale): void
11+
{
12+
if ($sale->price >= 3_000 && $sale->price < 30_000) {
13+
printf("Sale approved by manager with price %.2f\n", $sale->price);
14+
} else {
15+
parent::processSale($sale);
16+
}
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\ChainOfResponsability;
6+
7+
final class Sale
8+
{
9+
public readonly float $price;
10+
11+
public function __construct(float $price)
12+
{
13+
$this->price = $price;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DesignPattern\Behavioral\ChainOfResponsability;
5+
6+
namespace Growthdev\DesignPatterns\Behavioral\ChainOfResponsability;
7+
8+
interface SaleHandler
9+
{
10+
public function processSale(Sale $sale): void;
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Behavioral\ChainOfResponsability;
6+
7+
// vendedor
8+
final class SellerHandler extends ApproveHandler
9+
{
10+
public function processSale(Sale $sale): void
11+
{
12+
if ($sale->price < 3_000) {
13+
printf("Sale approved by seller with price %.2f\n", $sale->price);
14+
} else {
15+
parent::processSale($sale);
16+
}
17+
}
18+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Tests\Behavioral\ChainOfResponsability;
6+
7+
use Growthdev\DesignPatterns\Behavioral\ChainOfResponsability\DirectorHandler;
8+
use Growthdev\DesignPatterns\Behavioral\ChainOfResponsability\ManagerHandler;
9+
use Growthdev\DesignPatterns\Behavioral\ChainOfResponsability\Sale;
10+
use Growthdev\DesignPatterns\Behavioral\ChainOfResponsability\SellerHandler;
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class ChainOfResponsabilityTest extends TestCase
14+
{
15+
public function testExpectOfApproveBySeller()
16+
{
17+
$seller = new SellerHandler;
18+
$seller->setNext(new ManagerHandler)
19+
->setNext(new DirectorHandler);
20+
21+
// Request
22+
$sale = new Sale(2_999.99);
23+
$seller->processSale($sale);
24+
25+
$this->expectOutputString("Sale approved by seller with price 2999.99\n");
26+
}
27+
28+
public function testExpectOfApproveByManager()
29+
{
30+
$seller = new SellerHandler;
31+
$seller->setNext(new ManagerHandler)
32+
->setNext(new DirectorHandler);
33+
34+
// Request
35+
$sale = new Sale(3_999.99);
36+
$seller->processSale($sale);
37+
38+
$this->expectOutputString("Sale approved by manager with price 3999.99\n");
39+
}
40+
41+
public function testExpectOfApproveByDirector()
42+
{
43+
$seller = new SellerHandler;
44+
$seller->setNext(new ManagerHandler)
45+
->setNext(new DirectorHandler);
46+
47+
// Request
48+
$sale = new Sale(30_000.01);
49+
$seller->processSale($sale);
50+
51+
$this->expectOutputString("Sale approved by director with price 30000.01\n");
52+
}
53+
}

0 commit comments

Comments
 (0)