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

APinyansky/hw14 #403

Open
wants to merge 2 commits into
base: 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
11 changes: 11 additions & 0 deletions code/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once('vendor/autoload.php');

try {
$app = new App\Application();
$app->run();
}
catch(Exception $e) {
App\Response::generateBadRequestResponse($e->getMessage());
}
34 changes: 34 additions & 0 deletions code/app_src/Adapter/KitchenAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Adapter;

use App\Meal\MealInterface;

class KitchenAdapter
{
private KitchenService $kitchenService;


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

public function cookMeal(MealInterface $meal): void
{
$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): void
{
$this->kitchenService->utilize($meal);
}
}
45 changes: 45 additions & 0 deletions code/app_src/Adapter/KitchenService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Adapter;

use App\Meal\MealInterface;

class KitchenService
{
private static array $needToBeFried = [
'sausage',
'cotlete',
'onion',
'beef',
];

public function fry(array $ingredients): array
{
$cookedIngredients = [];
foreach ($ingredients as $ingredient => $value) {
if (in_array($ingredient, self::$needToBeFried)) {
$cookedIngredients["fried_$ingredient"] = $value;
} else {
$cookedIngredients[$ingredient] = $value;
}
}

return $cookedIngredients;
}

public function checkMealQuality(MealInterface $meal): bool
{
foreach ($meal->getIngredients() as $ingredient => $value) {
if (in_array($ingredient, self::$needToBeFried)) {
return false;
}
}

return true;
}

public function utilize(MealInterface $meal): void
{
$meal->setStatus('UTILIZED');
}
}
43 changes: 43 additions & 0 deletions code/app_src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App;

class Application
{
private $request;

public function __construct()
{
try {
$this->request = RequestValidator::validate($_POST);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}

public function run(): void
{
$customer = new Observer\Customer();
$order = new Strategy\OrderContext($customer);

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->getIngredients());
}
}
31 changes: 31 additions & 0 deletions code/app_src/Decorator/BurgerDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Decorator;

use App\Meal\MealInterface;

class BurgerDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;

private static array $baseRecipe = [
'pickles' => 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,
$this->mealDecorator->getAdapter()->createIngredientsArray(self::$baseRecipe)
);
$this->mealDecorator->setStatus('ADDED_BASE_BURGER_RECIPE_INGREDIENTS');
}
}
24 changes: 24 additions & 0 deletions code/app_src/Decorator/CustomerIngredientsDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Decorator;

use App\Meal\MealInterface;

class CustomerIngredientsDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;

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

public function addCustomerIngredients(array $customerIngredients): void
{
$this->mealDecorator->ingredients = array_merge(
$this->mealDecorator->ingredients,
$this->mealDecorator->getAdapter()->createIngredientsArray($customerIngredients)
);
$this->mealDecorator->setStatus('ADDED_CLIENT_INGREDIENTS');
}
}
30 changes: 30 additions & 0 deletions code/app_src/Decorator/HotdogDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Decorator;

use App\Meal\MealInterface;

class HotdogDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;

private static array $baseRecipe = [
'ketchup' => 1,
'mustard' => 1,
'sausage' => 1,
];

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

function addBaseIngredients(): void
{
$this->mealDecorator->ingredients = array_merge(
$this->mealDecorator->ingredients,
$this->mealDecorator->getAdapter()->createIngredientsArray(self::$baseRecipe)
);
$this->mealDecorator->setStatus('ADDED_BASE_HOTDOG_RECIPE_INGREDIENTS');
}
}
52 changes: 52 additions & 0 deletions code/app_src/Decorator/MealDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

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();
}

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);
}

public function getAdapter(): IngredientAdapter
{
return $this->adapter;
}

}
29 changes: 29 additions & 0 deletions code/app_src/Decorator/SandwichDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Decorator;

use App\Meal\MealInterface;

class SandwichDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;

private static array $baseRecipe = [
'ham' => 1,
'cheese' => 2,
];

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

function addBaseIngredients(): void
{
$this->mealDecorator->ingredients = array_merge(
$this->mealDecorator->ingredients,
$this->mealDecorator->getAdapter()->createIngredientsArray(self::$baseRecipe)
);
$this->mealDecorator->setStatus('ADDED_BASE_SANDWICH_RECIPE_INGREDIENTS');
}
}
14 changes: 14 additions & 0 deletions code/app_src/Factory/BurgerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Factory;

use App\Meal\Burger;
use App\Meal\MealInterface;

class BurgerFactory extends MealFactory
{
public function createMealBase(): MealInterface
{
return new Burger();
}
}
14 changes: 14 additions & 0 deletions code/app_src/Factory/HotdogFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Factory;

use App\Meal\Hotdog;
use App\Meal\MealInterface;

class HotdogFactory extends MealFactory
{
public function createMealBase(): MealInterface
{
return new Hotdog();
}
}
12 changes: 12 additions & 0 deletions code/app_src/Factory/MealFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Factory;

use App\Meal\MealInterface;

abstract class MealFactory
{

abstract public function createMealBase(): MealInterface;

}
14 changes: 14 additions & 0 deletions code/app_src/Factory/SandwichFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Factory;

use App\Meal\Sandwich;
use App\Meal\MealInterface;

class SandwichFactory extends MealFactory
{
public function createMealBase(): MealInterface
{
return new Sandwich();
}
}
14 changes: 14 additions & 0 deletions code/app_src/Meal/Burger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Meal;

class Burger extends MealBaseClass
{
public function __construct()
{
parent::__construct();
$this->setIngredients([
new Ingredient('bun', 2),
]);
}
}
Loading