From 951e5ad40ff6617bb5bd80586265e2f7c2778978 Mon Sep 17 00:00:00 2001 From: MadFisherman Date: Mon, 11 Apr 2022 00:52:36 +0300 Subject: [PATCH 1/2] APinyansky/hw14 --- code/app.php | 11 +++ code/app_src/Adapter/KitchenAdapter.php | 34 +++++++++ code/app_src/Adapter/KitchenService.php | 45 ++++++++++++ code/app_src/Application.php | 48 +++++++++++++ code/app_src/Decorator/BurgerDecorator.php | 28 ++++++++ .../CustomerIngredientsDecorator.php | 21 ++++++ code/app_src/Decorator/HotdogDecorator.php | 27 ++++++++ code/app_src/Decorator/MealDecorator.php | 44 ++++++++++++ code/app_src/Decorator/SandwichDecorator.php | 26 +++++++ code/app_src/Factory/BurgerFactory.php | 14 ++++ code/app_src/Factory/HotdogFactory.php | 14 ++++ code/app_src/Factory/MealFactory.php | 12 ++++ code/app_src/Factory/SandwichFactory.php | 14 ++++ code/app_src/Meal/Burger.php | 14 ++++ code/app_src/Meal/Hotdog.php | 14 ++++ code/app_src/Meal/MealBaseClass.php | 63 +++++++++++++++++ code/app_src/Meal/MealInterface.php | 8 +++ code/app_src/Meal/Sandwich.php | 14 ++++ code/app_src/Observer/Customer.php | 14 ++++ code/app_src/RequestValidator.php | 28 ++++++++ code/app_src/Response.php | 18 +++++ code/app_src/Strategy/BurgerStrategy.php | 36 ++++++++++ .../Strategy/CookingStrategyInterface.php | 12 ++++ code/app_src/Strategy/HotdogStrategy.php | 36 ++++++++++ code/app_src/Strategy/OrderContext.php | 40 +++++++++++ code/app_src/Strategy/SandwichStrategy.php | 36 ++++++++++ code/composer.json | 12 ++++ docker-compose.yml | 69 +++++++++++++++++++ nginx-balancer/Dockerfile | 11 +++ .../hosts/nginx-balancer.local.conf | 19 +++++ nginx-webserver/Dockerfile | 11 +++ nginx-webserver/hosts/otus.local.conf | 32 +++++++++ php-fpm/Dockerfile | 30 ++++++++ php-fpm/php.ini | 3 + 34 files changed, 858 insertions(+) create mode 100644 code/app.php create mode 100644 code/app_src/Adapter/KitchenAdapter.php create mode 100644 code/app_src/Adapter/KitchenService.php create mode 100644 code/app_src/Application.php create mode 100644 code/app_src/Decorator/BurgerDecorator.php create mode 100644 code/app_src/Decorator/CustomerIngredientsDecorator.php create mode 100644 code/app_src/Decorator/HotdogDecorator.php create mode 100644 code/app_src/Decorator/MealDecorator.php create mode 100644 code/app_src/Decorator/SandwichDecorator.php create mode 100644 code/app_src/Factory/BurgerFactory.php create mode 100644 code/app_src/Factory/HotdogFactory.php create mode 100644 code/app_src/Factory/MealFactory.php create mode 100644 code/app_src/Factory/SandwichFactory.php create mode 100644 code/app_src/Meal/Burger.php create mode 100644 code/app_src/Meal/Hotdog.php create mode 100644 code/app_src/Meal/MealBaseClass.php create mode 100644 code/app_src/Meal/MealInterface.php create mode 100644 code/app_src/Meal/Sandwich.php create mode 100644 code/app_src/Observer/Customer.php create mode 100644 code/app_src/RequestValidator.php create mode 100644 code/app_src/Response.php create mode 100644 code/app_src/Strategy/BurgerStrategy.php create mode 100644 code/app_src/Strategy/CookingStrategyInterface.php create mode 100644 code/app_src/Strategy/HotdogStrategy.php create mode 100644 code/app_src/Strategy/OrderContext.php create mode 100644 code/app_src/Strategy/SandwichStrategy.php create mode 100644 code/composer.json create mode 100644 docker-compose.yml create mode 100644 nginx-balancer/Dockerfile create mode 100644 nginx-balancer/hosts/nginx-balancer.local.conf create mode 100644 nginx-webserver/Dockerfile create mode 100644 nginx-webserver/hosts/otus.local.conf create mode 100644 php-fpm/Dockerfile create mode 100644 php-fpm/php.ini diff --git a/code/app.php b/code/app.php new file mode 100644 index 00000000..d6b114dc --- /dev/null +++ b/code/app.php @@ -0,0 +1,11 @@ +run(); +} +catch(Exception $e) { + App\Response::generateBadRequestResponse($e->getMessage()); +} diff --git a/code/app_src/Adapter/KitchenAdapter.php b/code/app_src/Adapter/KitchenAdapter.php new file mode 100644 index 00000000..8e8b3dd9 --- /dev/null +++ b/code/app_src/Adapter/KitchenAdapter.php @@ -0,0 +1,34 @@ +kitchenService = $kitchenService; + } + + public function cookMeal(MealInterface $meal) + { + $currentIngredients = $meal->getIngredients(); + $meal->resetIngredients(); + $meal->addIngredients($this->kitchenService->fry($currentIngredients)); + + if ($this->kitchenService->checkMealQuality($meal)) { + $meal->setStatus('COOKED_AND_CHECKED'); + } else { + throw new \Exception('Cooking failed'); + } + } + + public function utilizeMeal(MealInterface $meal) + { + $this->kitchenService->utilize($meal); + } +} \ No newline at end of file diff --git a/code/app_src/Adapter/KitchenService.php b/code/app_src/Adapter/KitchenService.php new file mode 100644 index 00000000..801ca142 --- /dev/null +++ b/code/app_src/Adapter/KitchenService.php @@ -0,0 +1,45 @@ + $value) { + if (in_array($ingredient, self::$needToBeFried)) { + $cookedIngredients["fried_$ingredient"] = $value; + } else { + $cookedIngredients[$ingredient] = $value; + } + } + + return $cookedIngredients; + } + + public function checkMealQuality(MealInterface $meal) + { + foreach ($meal->getIngredients() as $ingredient => $value) { + if (in_array($ingredient, self::$needToBeFried)) { + return false; + } + } + + return true; + } + + public function utilize(MealInterface $meal) + { + $meal->setStatus('UTILIZED'); + } +} \ No newline at end of file diff --git a/code/app_src/Application.php b/code/app_src/Application.php new file mode 100644 index 00000000..df88aa7a --- /dev/null +++ b/code/app_src/Application.php @@ -0,0 +1,48 @@ +request = RequestValidator::validate($_POST); + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } + } + + public function run() + { + $customer = new Observer\Customer(); + $order = new Strategy\OrderContext($customer); + //$_POST['client_ingredients'] = ['lettuce' => 1, 'tomato' => 2, 'additional_cheese' => 2]; + $m = new Meal\Burger(); + switch ($_POST['meal']) { + case 'Burger': + $order->setCookingStrategy(new Strategy\BurgerStrategy()); + break; + + case 'Hotdog': + $order->setCookingStrategy(new Strategy\HotdogStrategy()); + break; + + case 'Sandwich': + $order->setCookingStrategy(new Strategy\SandwichStrategy()); + break; + + default: + throw new Exception('No meal choosed'); + } + + $meal = $order->getOrderedMeal(isset($_POST['client_ingredients']) ? $_POST['client_ingredients'] : []); + print_r($meal); + } +} diff --git a/code/app_src/Decorator/BurgerDecorator.php b/code/app_src/Decorator/BurgerDecorator.php new file mode 100644 index 00000000..3b9c9210 --- /dev/null +++ b/code/app_src/Decorator/BurgerDecorator.php @@ -0,0 +1,28 @@ + 3, + 'onion' => 2, + 'lettuce' => 1, + 'cotlete' => 1, + ]; + + public function __construct(MealDecorator $mealDecorator) + { + $this->mealDecorator = $mealDecorator; + } + + function addBaseIngredients(): void + { + $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, self::$baseRecipe); + $this->mealDecorator->setStatus('ADDED_BASE_BURGER_RECIPE_INGREDIENTS'); + } +} \ No newline at end of file diff --git a/code/app_src/Decorator/CustomerIngredientsDecorator.php b/code/app_src/Decorator/CustomerIngredientsDecorator.php new file mode 100644 index 00000000..72cf6d45 --- /dev/null +++ b/code/app_src/Decorator/CustomerIngredientsDecorator.php @@ -0,0 +1,21 @@ +mealDecorator = $mealDecorator; + } + + public function addCustomerIngredients(array $customerIngredients) + { + $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, $customerIngredients); + $this->mealDecorator->setStatus('ADDED_CLIENT_INGREDIENTS'); + } +} \ No newline at end of file diff --git a/code/app_src/Decorator/HotdogDecorator.php b/code/app_src/Decorator/HotdogDecorator.php new file mode 100644 index 00000000..182a8893 --- /dev/null +++ b/code/app_src/Decorator/HotdogDecorator.php @@ -0,0 +1,27 @@ + 1, + 'mustard' => 1, + 'sausage' => 1, + ]; + + public function __construct(MealDecorator $mealDecorator) + { + $this->mealDecorator = $mealDecorator; + } + + function addBaseIngredients(): void + { + $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, self::$baseRecipe); + $this->mealDecorator->setStatus('ADDED_BASE_HOTDOG_RECIPE_INGREDIENTS'); + } +} \ No newline at end of file diff --git a/code/app_src/Decorator/MealDecorator.php b/code/app_src/Decorator/MealDecorator.php new file mode 100644 index 00000000..c71a6c48 --- /dev/null +++ b/code/app_src/Decorator/MealDecorator.php @@ -0,0 +1,44 @@ +meal = $baseMeal; + $this->resetIngredients(); + } + + public function resetIngredients(): void + { + $this->ingredients = $this->meal->getIngredients(); + } + + public function setStatus(string $status): void + { + $this->meal->setStatus($status); + } + + public function getBaseMealType(): string + { + return get_class($this->meal); + } + + public function getIngredients(): array + { + return $this->ingredients; + } + + public function addIngredients(array $ingredients = []): void + { + $this->ingredients = array_merge($this->ingredients, $ingredients); + } + +} \ No newline at end of file diff --git a/code/app_src/Decorator/SandwichDecorator.php b/code/app_src/Decorator/SandwichDecorator.php new file mode 100644 index 00000000..d6168e56 --- /dev/null +++ b/code/app_src/Decorator/SandwichDecorator.php @@ -0,0 +1,26 @@ + 1, + 'cheese' => 2, + ]; + + public function __construct(MealDecorator $mealDecorator) + { + $this->mealDecorator = $mealDecorator; + } + + function addBaseIngredients(): void + { + $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, self::$baseRecipe); + $this->mealDecorator->setStatus('ADDED_BASE_SANDWICH_RECIPE_INGREDIENTS'); + } +} \ No newline at end of file diff --git a/code/app_src/Factory/BurgerFactory.php b/code/app_src/Factory/BurgerFactory.php new file mode 100644 index 00000000..bdb8a79c --- /dev/null +++ b/code/app_src/Factory/BurgerFactory.php @@ -0,0 +1,14 @@ +setIngredients([ + 'bun' => 2, + ]); + } +} diff --git a/code/app_src/Meal/Hotdog.php b/code/app_src/Meal/Hotdog.php new file mode 100644 index 00000000..75ece680 --- /dev/null +++ b/code/app_src/Meal/Hotdog.php @@ -0,0 +1,14 @@ +setIngredients([ + 'bun' => 1, + ]); + } +} diff --git a/code/app_src/Meal/MealBaseClass.php b/code/app_src/Meal/MealBaseClass.php new file mode 100644 index 00000000..05081540 --- /dev/null +++ b/code/app_src/Meal/MealBaseClass.php @@ -0,0 +1,63 @@ +observers = new SplObjectStorage(); + } + + public function attach(SplObserver $observer): void + { + $this->observers->attach($observer); + } + + public function detach(SplObserver $observer): void + { + $this->observers->detach($observer); + } + + public function setStatus(string $status): void + { + $this->status = $status; + $this->notify(); + } + + public function getStatus(): string + { + return $this->status; + } + + public function notify(): void + { + foreach ($this->observers as $observer) { + $observer->update($this); + } + } + + public function getIngredients(): array + { + return $this->ingredients; + } + + public function setIngredients(array $ingredients = []): void + { + $this->ingredients = $ingredients; + } + + public function addIngredients(array $ingredients = []): void + { + $this->ingredients = array_merge($this->ingredients, $ingredients); + } +} diff --git a/code/app_src/Meal/MealInterface.php b/code/app_src/Meal/MealInterface.php new file mode 100644 index 00000000..12d57c84 --- /dev/null +++ b/code/app_src/Meal/MealInterface.php @@ -0,0 +1,8 @@ +setIngredients([ + 'toast' => 2, + ]); + } +} diff --git a/code/app_src/Observer/Customer.php b/code/app_src/Observer/Customer.php new file mode 100644 index 00000000..3802d12b --- /dev/null +++ b/code/app_src/Observer/Customer.php @@ -0,0 +1,14 @@ +getStatus() . PHP_EOL); + } +} \ No newline at end of file diff --git a/code/app_src/RequestValidator.php b/code/app_src/RequestValidator.php new file mode 100644 index 00000000..902f12d5 --- /dev/null +++ b/code/app_src/RequestValidator.php @@ -0,0 +1,28 @@ +generateBaseBurger(); + $baseBurger->attach($customer); + $baseBurger->setStatus('STARTED'); + return $this->addIngredients($baseBurger, $customerIngredients); + } + + private function generateBaseBurger(): MealInterface + { + $factory = new BurgerFactory(); + return $factory->createMealBase(); + } + + private function addIngredients(MealInterface $baseBurger, array $customerIngredients): MealInterface + { + $decorator = new Decorator\MealDecorator($baseBurger); + $burgerDecorator = new Decorator\BurgerDecorator($decorator); + $burgerDecorator->addBaseIngredients(); + $customerDecorator = new Decorator\CustomerIngredientsDecorator($decorator); + $customerDecorator->addCustomerIngredients($customerIngredients); + + return $decorator; + } +} diff --git a/code/app_src/Strategy/CookingStrategyInterface.php b/code/app_src/Strategy/CookingStrategyInterface.php new file mode 100644 index 00000000..c0117ea8 --- /dev/null +++ b/code/app_src/Strategy/CookingStrategyInterface.php @@ -0,0 +1,12 @@ +generateBaseHotdog(); + $baseHotdog->attach($customer); + $baseHotdog->setStatus('STARTED'); + return $this->addIngredients($baseHotdog, $customerIngredients); + } + + private function generateBaseHotdog(): MealInterface + { + $factory = new HotdogFactory(); + return $factory->createMealBase(); + } + + private function addIngredients(MealInterface $baseHotdog, array $customerIngredients): MealInterface + { + $decorator = new Decorator\MealDecorator($baseHotdog); + $HotdogDecorator = new Decorator\HotdogDecorator($decorator); + $HotdogDecorator->addBaseIngredients(); + $customerDecorator = new Decorator\CustomerIngredientsDecorator($decorator); + $customerDecorator->addCustomerIngredients($customerIngredients); + + return $decorator; + } +} diff --git a/code/app_src/Strategy/OrderContext.php b/code/app_src/Strategy/OrderContext.php new file mode 100644 index 00000000..8961d60e --- /dev/null +++ b/code/app_src/Strategy/OrderContext.php @@ -0,0 +1,40 @@ +customerObserver = $customer; + } + + public function setCookingStrategy(CookingStrategyInterface $strategy) + { + $this->strategy = $strategy; + } + + public function getOrderedMeal(array $customerIngredients = []) + { + $orderedMeal = $this->strategy->prepareIngredients($customerIngredients, $this->customerObserver); + $orderedMeal->setStatus('INGREDIENTS_PREPARED'); + + try { + $kitchen = new KitchenAdapter(new KitchenService()); + $kitchen->cookMeal($orderedMeal); + } catch (\Exception $e) { + if ($kitchen instanceof KitchenAdapter) { + $kitchen->utilizeMeal($orderedMeal); + } + } + + return $orderedMeal; + } +} diff --git a/code/app_src/Strategy/SandwichStrategy.php b/code/app_src/Strategy/SandwichStrategy.php new file mode 100644 index 00000000..05c51085 --- /dev/null +++ b/code/app_src/Strategy/SandwichStrategy.php @@ -0,0 +1,36 @@ +generateBaseSandwich(); + $baseSandwich->attach($customer); + $baseSandwich->setStatus('STARTED'); + return $this->addIngredients($baseSandwich, $customerIngredients); + } + + private function generateBaseSandwich(): MealInterface + { + $factory = new SandwichFactory(); + return $factory->createMealBase(); + } + + private function addIngredients(MealInterface $baseSandwich, array $customerIngredients): MealInterface + { + $decorator = new Decorator\MealDecorator($baseSandwich); + $SandwichDecorator = new Decorator\SandwichDecorator($decorator); + $SandwichDecorator->addBaseIngredients(); + $customerDecorator = new Decorator\CustomerIngredientsDecorator($decorator); + $customerDecorator->addCustomerIngredients($customerIngredients); + + return $decorator; + } +} diff --git a/code/composer.json b/code/composer.json new file mode 100644 index 00000000..ebb5c094 --- /dev/null +++ b/code/composer.json @@ -0,0 +1,12 @@ +{ + "require": { + "php": ">=7.4", + "ext-json": "*", + "ext-sockets": "*", + }, + "autoload": { + "psr-4": { + "App\\": "app_src" + } + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..4a55c36f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,69 @@ +version: '3' + +services: + nginx-balancer: + build: + context: ./nginx-balancer + dockerfile: Dockerfile + container_name: nginx-balancer + image: localapp/nginx-balancer + ports: + - "80:80" + - "443:443" + depends_on: + - nginx-webserver1 + - nginx-webserver2 + networks: + - app-network + + nginx-webserver1: + build: + context: ./nginx-webserver + dockerfile: Dockerfile + container_name: nginx-webserver1 + image: localapp/nginx-webserver + ports: + - "8081:80" + volumes: + - ./code:/var/www/otus.local + networks: + - app-network + + nginx-webserver2: + build: + context: ./nginx-webserver + dockerfile: Dockerfile + container_name: nginx-webserver2 + image: localapp/nginx-webserver + ports: + - "8082:80" + volumes: + - ./code:/var/www/otus.local + networks: + - app-network + + php1: + build: + context: ./php-fpm + dockerfile: Dockerfile + image: localapp/php + container_name: php1 + volumes: + - ./code:/var/www/otus.local + networks: + - app-network + + php2: + build: + context: ./php-fpm + dockerfile: Dockerfile + image: localapp/php + container_name: php2 + volumes: + - ./code:/var/www/otus.local + networks: + - app-network + +networks: + app-network: + driver: bridge diff --git a/nginx-balancer/Dockerfile b/nginx-balancer/Dockerfile new file mode 100644 index 00000000..082a656f --- /dev/null +++ b/nginx-balancer/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu:latest + +RUN apt-get update && apt-get install -y nginx + +COPY ./hosts/nginx-balancer.local.conf /etc/nginx/sites-enabled/otus.local.conf + +WORKDIR /var/www/otus.local +VOLUME /var/www/otus.local +EXPOSE 80 + +CMD [ "nginx", "-g", "daemon off;"] diff --git a/nginx-balancer/hosts/nginx-balancer.local.conf b/nginx-balancer/hosts/nginx-balancer.local.conf new file mode 100644 index 00000000..2c733d23 --- /dev/null +++ b/nginx-balancer/hosts/nginx-balancer.local.conf @@ -0,0 +1,19 @@ +upstream nginx-webservers { + server nginx-webserver1; + server nginx-webserver2; +} + +server { + listen 80; + + server_name otus.local; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + + location / { + proxy_pass http://nginx-webservers; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + } +} diff --git a/nginx-webserver/Dockerfile b/nginx-webserver/Dockerfile new file mode 100644 index 00000000..87b379b2 --- /dev/null +++ b/nginx-webserver/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu:latest + +RUN apt-get update && apt-get install -y nginx + +COPY ./hosts/otus.local.conf /etc/nginx/sites-enabled/otus.local.conf + +WORKDIR /var/www/otus.local +VOLUME /var/www/otus.local +EXPOSE 80 + +CMD [ "nginx", "-g", "daemon off;"] diff --git a/nginx-webserver/hosts/otus.local.conf b/nginx-webserver/hosts/otus.local.conf new file mode 100644 index 00000000..4446e512 --- /dev/null +++ b/nginx-webserver/hosts/otus.local.conf @@ -0,0 +1,32 @@ +upstream php-fpm { + server php1:9000; + server php2:9000; +} + +server { + listen 80; + index index.php index.html index.htm; + server_name otus.local; + root /var/www/otus.local; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + + location ~* .(jpg|jpeg|gif|css|png|js|html)$ { + access_log off; + expires max; + } + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~* .php$ { + try_files $uri = 404; + fastcgi_split_path_info (.+?\.php)(/.*)$; + fastcgi_pass php-fpm; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile new file mode 100644 index 00000000..61cb608a --- /dev/null +++ b/php-fpm/Dockerfile @@ -0,0 +1,30 @@ +FROM php:8.0-fpm + +RUN apt-get update && apt-get install -y \ + curl \ + wget \ + git \ + libfreetype6-dev \ + libjpeg62-turbo-dev \ + libpng-dev \ + libonig-dev \ + libzip-dev \ + libmcrypt-dev \ + libmemcached-dev\ + libmemcached-tools\ + && pecl install memcached \ + && docker-php-ext-enable memcached \ + && pecl install mcrypt-1.0.4 \ + && docker-php-ext-enable mcrypt \ + && docker-php-ext-install -j$(nproc) iconv mbstring mysqli pdo_mysql zip \ + && docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install -j$(nproc) gd \ + && curl -sS https://getcomposer.org/installer \ + | php -- --install-dir=/usr/local/bin --filename=composer + +ADD php.ini /usr/local/etc/php/conf.d/40-custom.ini + +WORKDIR /var/www +VOLUME /var/www + +CMD ["php-fpm"] diff --git a/php-fpm/php.ini b/php-fpm/php.ini new file mode 100644 index 00000000..a58749e0 --- /dev/null +++ b/php-fpm/php.ini @@ -0,0 +1,3 @@ +extension=memcached.so +session.save_handler = memcache +session.save_path = "tcp://memcache:11211" From 0a58eda5069797d19bfb5246a79af1dfa61d03fd Mon Sep 17 00:00:00 2001 From: MadFisherman Date: Mon, 11 Apr 2022 23:46:56 +0300 Subject: [PATCH 2/2] APinyansky/hw14-refactoring --- code/app_src/Adapter/KitchenAdapter.php | 6 ++--- code/app_src/Adapter/KitchenService.php | 4 +-- code/app_src/Application.php | 11 +++----- code/app_src/Decorator/BurgerDecorator.php | 5 +++- .../CustomerIngredientsDecorator.php | 7 +++-- code/app_src/Decorator/HotdogDecorator.php | 5 +++- code/app_src/Decorator/MealDecorator.php | 8 ++++++ code/app_src/Decorator/SandwichDecorator.php | 5 +++- code/app_src/Meal/Burger.php | 2 +- code/app_src/Meal/Hotdog.php | 2 +- code/app_src/Meal/Ingredient.php | 26 +++++++++++++++++++ code/app_src/Meal/IngredientAdapter.php | 16 ++++++++++++ code/app_src/Meal/Sandwich.php | 2 +- code/app_src/RequestValidator.php | 22 ++++++++-------- code/app_src/Strategy/OrderContext.php | 5 ++-- 15 files changed, 92 insertions(+), 34 deletions(-) create mode 100644 code/app_src/Meal/Ingredient.php create mode 100644 code/app_src/Meal/IngredientAdapter.php diff --git a/code/app_src/Adapter/KitchenAdapter.php b/code/app_src/Adapter/KitchenAdapter.php index 8e8b3dd9..e82d7a86 100644 --- a/code/app_src/Adapter/KitchenAdapter.php +++ b/code/app_src/Adapter/KitchenAdapter.php @@ -9,12 +9,12 @@ class KitchenAdapter private KitchenService $kitchenService; - public function __construct($kitchenService) + public function __construct(KitchenService $kitchenService) { $this->kitchenService = $kitchenService; } - public function cookMeal(MealInterface $meal) + public function cookMeal(MealInterface $meal): void { $currentIngredients = $meal->getIngredients(); $meal->resetIngredients(); @@ -27,7 +27,7 @@ public function cookMeal(MealInterface $meal) } } - public function utilizeMeal(MealInterface $meal) + public function utilizeMeal(MealInterface $meal): void { $this->kitchenService->utilize($meal); } diff --git a/code/app_src/Adapter/KitchenService.php b/code/app_src/Adapter/KitchenService.php index 801ca142..00d09719 100644 --- a/code/app_src/Adapter/KitchenService.php +++ b/code/app_src/Adapter/KitchenService.php @@ -27,7 +27,7 @@ public function fry(array $ingredients): array return $cookedIngredients; } - public function checkMealQuality(MealInterface $meal) + public function checkMealQuality(MealInterface $meal): bool { foreach ($meal->getIngredients() as $ingredient => $value) { if (in_array($ingredient, self::$needToBeFried)) { @@ -38,7 +38,7 @@ public function checkMealQuality(MealInterface $meal) return true; } - public function utilize(MealInterface $meal) + public function utilize(MealInterface $meal): void { $meal->setStatus('UTILIZED'); } diff --git a/code/app_src/Application.php b/code/app_src/Application.php index df88aa7a..7eabd7d5 100644 --- a/code/app_src/Application.php +++ b/code/app_src/Application.php @@ -2,13 +2,9 @@ namespace App; -require 'vendor/autoload.php'; - class Application { private $request; - private $storageInterface; - private $appHelper; public function __construct() { @@ -19,12 +15,11 @@ public function __construct() } } - public function run() + public function run(): void { $customer = new Observer\Customer(); $order = new Strategy\OrderContext($customer); - //$_POST['client_ingredients'] = ['lettuce' => 1, 'tomato' => 2, 'additional_cheese' => 2]; - $m = new Meal\Burger(); + switch ($_POST['meal']) { case 'Burger': $order->setCookingStrategy(new Strategy\BurgerStrategy()); @@ -43,6 +38,6 @@ public function run() } $meal = $order->getOrderedMeal(isset($_POST['client_ingredients']) ? $_POST['client_ingredients'] : []); - print_r($meal); + print_r($meal->getIngredients()); } } diff --git a/code/app_src/Decorator/BurgerDecorator.php b/code/app_src/Decorator/BurgerDecorator.php index 3b9c9210..eaf8ce15 100644 --- a/code/app_src/Decorator/BurgerDecorator.php +++ b/code/app_src/Decorator/BurgerDecorator.php @@ -22,7 +22,10 @@ public function __construct(MealDecorator $mealDecorator) function addBaseIngredients(): void { - $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, self::$baseRecipe); + $this->mealDecorator->ingredients = array_merge( + $this->mealDecorator->ingredients, + $this->mealDecorator->getAdapter()->createIngredientsArray(self::$baseRecipe) + ); $this->mealDecorator->setStatus('ADDED_BASE_BURGER_RECIPE_INGREDIENTS'); } } \ No newline at end of file diff --git a/code/app_src/Decorator/CustomerIngredientsDecorator.php b/code/app_src/Decorator/CustomerIngredientsDecorator.php index 72cf6d45..c1192ac6 100644 --- a/code/app_src/Decorator/CustomerIngredientsDecorator.php +++ b/code/app_src/Decorator/CustomerIngredientsDecorator.php @@ -13,9 +13,12 @@ public function __construct(MealDecorator $mealDecorator) $this->mealDecorator = $mealDecorator; } - public function addCustomerIngredients(array $customerIngredients) + public function addCustomerIngredients(array $customerIngredients): void { - $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, $customerIngredients); + $this->mealDecorator->ingredients = array_merge( + $this->mealDecorator->ingredients, + $this->mealDecorator->getAdapter()->createIngredientsArray($customerIngredients) + ); $this->mealDecorator->setStatus('ADDED_CLIENT_INGREDIENTS'); } } \ No newline at end of file diff --git a/code/app_src/Decorator/HotdogDecorator.php b/code/app_src/Decorator/HotdogDecorator.php index 182a8893..b6c75216 100644 --- a/code/app_src/Decorator/HotdogDecorator.php +++ b/code/app_src/Decorator/HotdogDecorator.php @@ -21,7 +21,10 @@ public function __construct(MealDecorator $mealDecorator) function addBaseIngredients(): void { - $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, self::$baseRecipe); + $this->mealDecorator->ingredients = array_merge( + $this->mealDecorator->ingredients, + $this->mealDecorator->getAdapter()->createIngredientsArray(self::$baseRecipe) + ); $this->mealDecorator->setStatus('ADDED_BASE_HOTDOG_RECIPE_INGREDIENTS'); } } \ No newline at end of file diff --git a/code/app_src/Decorator/MealDecorator.php b/code/app_src/Decorator/MealDecorator.php index c71a6c48..b474893c 100644 --- a/code/app_src/Decorator/MealDecorator.php +++ b/code/app_src/Decorator/MealDecorator.php @@ -3,16 +3,19 @@ namespace App\Decorator; use App\Meal\MealBaseClass; +use App\Meal\IngredientAdapter; class MealDecorator extends MealBaseClass { private MealBaseClass $meal; private SplObjectStorage $observers; + private IngredientAdapter $adapter; public function __construct(MealBaseClass $baseMeal) { $this->meal = $baseMeal; + $this->adapter = new IngredientAdapter(); $this->resetIngredients(); } @@ -40,5 +43,10 @@ public function addIngredients(array $ingredients = []): void { $this->ingredients = array_merge($this->ingredients, $ingredients); } + + public function getAdapter(): IngredientAdapter + { + return $this->adapter; + } } \ No newline at end of file diff --git a/code/app_src/Decorator/SandwichDecorator.php b/code/app_src/Decorator/SandwichDecorator.php index d6168e56..1f8f516a 100644 --- a/code/app_src/Decorator/SandwichDecorator.php +++ b/code/app_src/Decorator/SandwichDecorator.php @@ -20,7 +20,10 @@ public function __construct(MealDecorator $mealDecorator) function addBaseIngredients(): void { - $this->mealDecorator->ingredients = array_merge($this->mealDecorator->ingredients, self::$baseRecipe); + $this->mealDecorator->ingredients = array_merge( + $this->mealDecorator->ingredients, + $this->mealDecorator->getAdapter()->createIngredientsArray(self::$baseRecipe) + ); $this->mealDecorator->setStatus('ADDED_BASE_SANDWICH_RECIPE_INGREDIENTS'); } } \ No newline at end of file diff --git a/code/app_src/Meal/Burger.php b/code/app_src/Meal/Burger.php index 8fb4b881..79d53e44 100644 --- a/code/app_src/Meal/Burger.php +++ b/code/app_src/Meal/Burger.php @@ -8,7 +8,7 @@ public function __construct() { parent::__construct(); $this->setIngredients([ - 'bun' => 2, + new Ingredient('bun', 2), ]); } } diff --git a/code/app_src/Meal/Hotdog.php b/code/app_src/Meal/Hotdog.php index 75ece680..c3f8e213 100644 --- a/code/app_src/Meal/Hotdog.php +++ b/code/app_src/Meal/Hotdog.php @@ -8,7 +8,7 @@ public function __construct() { parent::__construct(); $this->setIngredients([ - 'bun' => 1, + new Ingredient('bun', 1), ]); } } diff --git a/code/app_src/Meal/Ingredient.php b/code/app_src/Meal/Ingredient.php new file mode 100644 index 00000000..a9f338d3 --- /dev/null +++ b/code/app_src/Meal/Ingredient.php @@ -0,0 +1,26 @@ +name = $name; + $this->amount = $amount; + } + + public function getName(): string + { + return $this->name; + } + + public function getAmount(): int + { + return $this->$amount; + } +} \ No newline at end of file diff --git a/code/app_src/Meal/IngredientAdapter.php b/code/app_src/Meal/IngredientAdapter.php new file mode 100644 index 00000000..717a03e8 --- /dev/null +++ b/code/app_src/Meal/IngredientAdapter.php @@ -0,0 +1,16 @@ + $amount) { + $ingredients[] = new Ingredient($ingredient, (int)$amount); + } + + return $ingredients; + } +} \ No newline at end of file diff --git a/code/app_src/Meal/Sandwich.php b/code/app_src/Meal/Sandwich.php index 8e60529d..d3e86fe5 100644 --- a/code/app_src/Meal/Sandwich.php +++ b/code/app_src/Meal/Sandwich.php @@ -8,7 +8,7 @@ public function __construct() { parent::__construct(); $this->setIngredients([ - 'toast' => 2, + new Ingredient('toast', 2), ]); } } diff --git a/code/app_src/RequestValidator.php b/code/app_src/RequestValidator.php index 902f12d5..beb3a854 100644 --- a/code/app_src/RequestValidator.php +++ b/code/app_src/RequestValidator.php @@ -4,24 +4,24 @@ class RequestValidator { - public static function validate($request) + public static function validate(array $request): array { - if (!self::checkRequestType('POST')) { - throw new \Exception('Wrong request method'); - } - if (self::checkRequestIsEmpty($request)) { - throw new \Exception('Empty request'); - } - - return $request; + if (!self::checkRequestType('POST')) { + throw new \Exception('Wrong request method'); + } + if (self::checkRequestIsEmpty($request)) { + throw new \Exception('Empty request'); + } + + return $request; } - public static function checkRequestType($typeNeeded) + public static function checkRequestType(string $typeNeeded): bool { return $_SERVER['REQUEST_METHOD'] == $typeNeeded ? true : false; } - public static function checkRequestIsEmpty($request) + public static function checkRequestIsEmpty(array $request): bool { return empty($request) ? true : false; } diff --git a/code/app_src/Strategy/OrderContext.php b/code/app_src/Strategy/OrderContext.php index 8961d60e..e2a6ff88 100644 --- a/code/app_src/Strategy/OrderContext.php +++ b/code/app_src/Strategy/OrderContext.php @@ -5,6 +5,7 @@ use App\Observer\Customer; use App\Adapter\KitchenAdapter; use App\Adapter\KitchenService; +use App\Meal\MealInterface; class OrderContext { @@ -16,12 +17,12 @@ public function __construct(Customer $customer) $this->customerObserver = $customer; } - public function setCookingStrategy(CookingStrategyInterface $strategy) + public function setCookingStrategy(CookingStrategyInterface $strategy): void { $this->strategy = $strategy; } - public function getOrderedMeal(array $customerIngredients = []) + public function getOrderedMeal(array $customerIngredients = []): MealInterface { $orderedMeal = $this->strategy->prepareIngredients($customerIngredients, $this->customerObserver); $orderedMeal->setStatus('INGREDIENTS_PREPARED');