Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 18. Design patterns #1202

Open
wants to merge 8 commits into
base: DAleynik/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
/vendor/
.env
/helpers/
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# PHP_2023
# Homework 16. Design patterns

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus

Разрабатываем часть интернет-ресторана. Продаёт он фаст-фуд.

1. Абстрактная фабрика будет отвечать за генерацию базового продукта-прототипа: бургер, сэндвич или хот-дог
2. При готовке каждого типа продукта Декоратор будет добавлять составляющие к базовому продукту либо по рецепту, либо по пожеланию клиента (салат, лук, перец и т.д.)
3. Наблюдатель подписывается на статус приготовления и отправляет оповещения о том, что изменился статус приготовления продукта.
4. Адаптер используем для сущности пицца.
5. Строитель будет отвечать за то, что нужно приготовить.
6. Все сущности должны по максимуму генерироваться через DI.
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "dmitry/hw16",
"type": "project",
"autoload": {
"psr-4": {
"Dmitry\\Hw16\\": "src/"
}
},
"authors": [
{
"name": "Dmitry Aleynik"
}
],
"require": {}
}
15 changes: 15 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Dmitry\Hw16\App;


try {
$app = new App();
$app->run();
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
35 changes: 35 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Dmitry\Hw16;

use Dmitry\Hw16\Application\Adapter\PizzaAdapter;
use Dmitry\Hw16\Application\Decorator\OnionDecorator;
use Dmitry\Hw16\Application\Decorator\PepperDecorator;
use Dmitry\Hw16\Application\Decorator\SaladDecorator;
use Dmitry\Hw16\Application\Factory\BurgerFactory;
use Dmitry\Hw16\Application\Factory\ProductFactory;
use Dmitry\Hw16\Application\Publisher\Publisher;
use Dmitry\Hw16\Application\Services\CookingInterface;
use Dmitry\Hw16\Application\Services\CookingService;
use Dmitry\Hw16\Application\UseCase\CookingUseCase;

class App
{
private CookingInterface $cookingService;

public function __construct()
{
$this->cookingService = new CookingService(new Publisher());
}

public function run(): void
{
$burger = ProductFactory::makeFood('burger');
$sandwich = new PepperDecorator(new OnionDecorator(new SaladDecorator(ProductFactory::makeFood('sandwich'))));
$pizza = new PizzaAdapter();
$usecase = new CookingUseCase();
$usecase($this->cookingService, $burger, $sandwich, $pizza);
}
}
31 changes: 31 additions & 0 deletions src/Application/Adapter/PizzaAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Dmitry\Hw16\Application\Adapter;

use Dmitry\Hw16\Domain\Entity\Pizza;
use Dmitry\Hw16\Domain\Entity\ProductInterface;

class PizzaAdapter implements ProductInterface
{
private ProductInterface $pizza;

public function __construct()
{
$this->createPizza();
}

private function createPizza(): void
{
$this->pizza = new Pizza();
}

public function getName(): string
{
return $this->pizza->getName();
}

public function makeCooked(): void
{
$this->pizza->makeCooked();
}
}
44 changes: 44 additions & 0 deletions src/Application/Builder/ProductBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Dmitry\Hw16\Application\Builder;

use Dmitry\Hw16\Application\Decorator\OnionDecorator;
use Dmitry\Hw16\Application\Decorator\PepperDecorator;
use Dmitry\Hw16\Application\Decorator\SaladDecorator;
use Dmitry\Hw16\Domain\Entity\ProductInterface;

class ProductBuilder
{
private ProductInterface $product;

public function __construct(ProductInterface $product)
{
$this->product = $this->product;
}


public function addOnion(): self
{
$this->product = new OnionDecorator($this->product);
return $this;
}

public function addPepper(): self
{
$this->product = new PepperDecorator($this->product);
return $this;
}


public function addSalad(): self
{
$this->product = new SaladDecorator($this->product);
return $this;
}


public function build(): ProductInterface
{
return $this->product;
}
}
25 changes: 25 additions & 0 deletions src/Application/Decorator/OnionDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Dmitry\Hw16\Application\Decorator;

use Dmitry\Hw16\Domain\Entity\ProductInterface;

class OnionDecorator implements ProductInterface
{
private $product;

public function __construct($product)
{
$this->product = $product;
}

public function makeCooked(): void
{
$this->product->makeCooked();
}

public function getName(): string
{
return $this->product->getName() . ' с луком';
}
}
25 changes: 25 additions & 0 deletions src/Application/Decorator/PepperDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Dmitry\Hw16\Application\Decorator;

use Dmitry\Hw16\Domain\Entity\ProductInterface;

class PepperDecorator implements ProductInterface
{
private $product;

public function __construct($product)
{
$this->product = $product;
}

public function makeCooked(): void
{
$this->product->makeCooked();
}

public function getName(): string
{
return $this->product->getName() . ' с перцем';
}
}
25 changes: 25 additions & 0 deletions src/Application/Decorator/SaladDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Dmitry\Hw16\Application\Decorator;

use Dmitry\Hw16\Domain\Entity\ProductInterface;

class SaladDecorator implements ProductInterface
{
private $product;

public function __construct($product)
{
$this->product = $product;
}

public function makeCooked(): void
{
$this->product->makeCooked();
}

public function getName(): string
{
return $this->product->getName() . ' с салатом';
}
}
31 changes: 31 additions & 0 deletions src/Application/Factory/ProductFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Dmitry\Hw16\Application\Factory;

use Dmitry\Hw16\Domain\Entity\Burger;
use Dmitry\Hw16\Domain\Entity\Product;
use Dmitry\Hw16\Domain\Entity\Sandwich;
use Dmitry\Hw16\Domain\Entity\Hotdog;

class ProductFactory
{
private function __construct()
{
//Method disabled
}

public static function makeFood(string $type): Product
{
switch ($type) {
case 'burger':
return new Burger();
case 'sandwich':
return new Sandwich();
case 'hotdog':
return new Hotdog();
default:
throw new \Exception('Неизвестный продукт - ' . $type);
break;
}
}
}
26 changes: 26 additions & 0 deletions src/Application/Publisher/Publisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Dmitry\Hw16\Application\Publisher;

class Publisher implements PublisherInterface
{
private $subscribers = [];

public function subscribe(SubscriberInterface $subscriber): void
{
$this->subscribers[spl_object_id($subscriber)] = $subscriber;
}

public function unsubscribe(SubscriberInterface $subscriber): void
{
unset($this->subscribers[spl_object_id($subscriber)]);
}

public function notify($product, $status)
{
var_dump($product->getName() . ' - ' . $status);
foreach ($this->subscribers as $subscriber) {
$subscriber->notify($product, $status);
}
}
}
15 changes: 15 additions & 0 deletions src/Application/Publisher/PublisherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Dmitry\Hw16\Application\Publisher;

use Dmitry\Hw16\Domain\Entity\Product;
use MongoDB\Driver\Monitoring\Subscriber;

interface PublisherInterface
{
public function subscribe(SubscriberInterface $subscriber): void;

public function unsubscribe(SubscriberInterface $subscriber): void;

public function notify(Product $product, string $status);
}
10 changes: 10 additions & 0 deletions src/Application/Publisher/SubscriberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Dmitry\Hw16\Application\Publisher;

use Dmitry\Hw16\Domain\Entity\Product;

interface SubscriberInterface
{
public function notify(Product $product, string $status);
}
13 changes: 13 additions & 0 deletions src/Application/Services/CookingInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Dmitry\Hw16\Application\Services;

use Dmitry\Hw16\Application\Publisher\PublisherInterface;
use Dmitry\Hw16\Domain\Entity\ProductInterface;

interface CookingInterface
{
public function __construct(PublisherInterface $publisher);

public function cook(ProductInterface $product): ProductInterface;
}
24 changes: 24 additions & 0 deletions src/Application/Services/CookingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Dmitry\Hw16\Application\Services;

use Dmitry\Hw16\Application\Publisher\PublisherInterface;
use Dmitry\Hw16\Domain\Entity\ProductInterface;

class CookingService implements CookingInterface
{
private PublisherInterface $publisher;

public function __construct(PublisherInterface $publisher)
{
$this->publisher = $publisher;
}

public function cook(ProductInterface $product): ProductInterface
{
$this->publisher->notify($product, 'Отправлен готовиться');
$product->makeCooked();
$this->publisher->notify($product, 'Приготовлен');
return $product;
}
}
18 changes: 18 additions & 0 deletions src/Application/UseCase/CookingUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Dmitry\Hw16\Application\UseCase;

use Dmitry\Hw16\Application\Services\CookingInterface;
use Dmitry\Hw16\Domain\Entity\ProductInterface;

class CookingUseCase
{
public function __invoke(
CookingInterface $cookingService,
ProductInterface ...$product
) {
foreach ($product as $item) {
$cookingService->cook($item);
}
}
}
Loading
Loading