diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..23a84b44d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +/vendor/ +.env +/helpers/ \ No newline at end of file diff --git a/README.md b/README.md index e16b2a49b..0eb8a186d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json new file mode 100644 index 000000000..0d72d3ad3 --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "dmitry/hw16", + "type": "project", + "autoload": { + "psr-4": { + "Dmitry\\Hw16\\": "src/" + } + }, + "authors": [ + { + "name": "Dmitry Aleynik" + } + ], + "require": {} +} diff --git a/index.php b/index.php new file mode 100644 index 000000000..3623b3741 --- /dev/null +++ b/index.php @@ -0,0 +1,15 @@ +run(); +} catch (\Exception $e) { + throw new \Exception($e->getMessage()); +} diff --git a/src/App.php b/src/App.php new file mode 100644 index 000000000..24b71cacd --- /dev/null +++ b/src/App.php @@ -0,0 +1,35 @@ +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); + } +} diff --git a/src/Application/Adapter/PizzaAdapter.php b/src/Application/Adapter/PizzaAdapter.php new file mode 100644 index 000000000..f9a5f22a0 --- /dev/null +++ b/src/Application/Adapter/PizzaAdapter.php @@ -0,0 +1,31 @@ +createPizza(); + } + + private function createPizza(): void + { + $this->pizza = new Pizza(); + } + + public function getName(): string + { + return $this->pizza->getName(); + } + + public function makeCooked(): void + { + $this->pizza->makeCooked(); + } +} diff --git a/src/Application/Builder/ProductBuilder.php b/src/Application/Builder/ProductBuilder.php new file mode 100644 index 000000000..e416ca40e --- /dev/null +++ b/src/Application/Builder/ProductBuilder.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/src/Application/Decorator/OnionDecorator.php b/src/Application/Decorator/OnionDecorator.php new file mode 100644 index 000000000..3bb857a9f --- /dev/null +++ b/src/Application/Decorator/OnionDecorator.php @@ -0,0 +1,25 @@ +product = $product; + } + + public function makeCooked(): void + { + $this->product->makeCooked(); + } + + public function getName(): string + { + return $this->product->getName() . ' с луком'; + } +} diff --git a/src/Application/Decorator/PepperDecorator.php b/src/Application/Decorator/PepperDecorator.php new file mode 100644 index 000000000..b0d8dfb33 --- /dev/null +++ b/src/Application/Decorator/PepperDecorator.php @@ -0,0 +1,25 @@ +product = $product; + } + + public function makeCooked(): void + { + $this->product->makeCooked(); + } + + public function getName(): string + { + return $this->product->getName() . ' с перцем'; + } +} diff --git a/src/Application/Decorator/SaladDecorator.php b/src/Application/Decorator/SaladDecorator.php new file mode 100644 index 000000000..71ded8d10 --- /dev/null +++ b/src/Application/Decorator/SaladDecorator.php @@ -0,0 +1,25 @@ +product = $product; + } + + public function makeCooked(): void + { + $this->product->makeCooked(); + } + + public function getName(): string + { + return $this->product->getName() . ' с салатом'; + } +} diff --git a/src/Application/Factory/ProductFactory.php b/src/Application/Factory/ProductFactory.php new file mode 100644 index 000000000..04b54ff42 --- /dev/null +++ b/src/Application/Factory/ProductFactory.php @@ -0,0 +1,31 @@ +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); + } + } +} diff --git a/src/Application/Publisher/PublisherInterface.php b/src/Application/Publisher/PublisherInterface.php new file mode 100644 index 000000000..954cb752f --- /dev/null +++ b/src/Application/Publisher/PublisherInterface.php @@ -0,0 +1,15 @@ +publisher = $publisher; + } + + public function cook(ProductInterface $product): ProductInterface + { + $this->publisher->notify($product, 'Отправлен готовиться'); + $product->makeCooked(); + $this->publisher->notify($product, 'Приготовлен'); + return $product; + } +} diff --git a/src/Application/UseCase/CookingUseCase.php b/src/Application/UseCase/CookingUseCase.php new file mode 100644 index 000000000..448229543 --- /dev/null +++ b/src/Application/UseCase/CookingUseCase.php @@ -0,0 +1,18 @@ +cook($item); + } + } +} diff --git a/src/Domain/Entity/Burger.php b/src/Domain/Entity/Burger.php new file mode 100644 index 000000000..9ad29a61b --- /dev/null +++ b/src/Domain/Entity/Burger.php @@ -0,0 +1,13 @@ +name = 'Бургер'; + } +} diff --git a/src/Domain/Entity/Hotdog.php b/src/Domain/Entity/Hotdog.php new file mode 100644 index 000000000..a2acc6c9e --- /dev/null +++ b/src/Domain/Entity/Hotdog.php @@ -0,0 +1,13 @@ +name = 'Хотдог'; + } +} diff --git a/src/Domain/Entity/Pizza.php b/src/Domain/Entity/Pizza.php new file mode 100644 index 000000000..51a927fa0 --- /dev/null +++ b/src/Domain/Entity/Pizza.php @@ -0,0 +1,24 @@ +name = 'Пицца'; + } + + public function getName(): string + { + return $this->name; + } + + public function makeCooked(): void + { + $this->is_cooked = true; + } +} diff --git a/src/Domain/Entity/Product.php b/src/Domain/Entity/Product.php new file mode 100644 index 000000000..36c7f033b --- /dev/null +++ b/src/Domain/Entity/Product.php @@ -0,0 +1,21 @@ +name; + } + + public function makeCooked(): void + { + $this->is_cooked = true; + } +} diff --git a/src/Domain/Entity/ProductInterface.php b/src/Domain/Entity/ProductInterface.php new file mode 100644 index 000000000..71d1aff19 --- /dev/null +++ b/src/Domain/Entity/ProductInterface.php @@ -0,0 +1,10 @@ +name = 'Сэндвич'; + } +}