Skip to content
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
dbdata
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# PHP2021
# PHP2021

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus
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());
}
46 changes: 46 additions & 0 deletions code/app/Adapters/KitchenAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Adapters;

use App\Meal\MealInterface;
use Exception;

class KitchenAdapter
{
private KitchenService $kitchenService;

/**
* @param KitchenService $kitchenService
*/
public function __construct(KitchenService $kitchenService)
{
$this->kitchenService = $kitchenService;
}

/**
* @param MealInterface $meal
* @return void
* @throws Exception
*/
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');
}
}

/**
* @param MealInterface $meal
* @return void
*/
public function utilizeMeal(MealInterface $meal): void
{
$this->kitchenService->utilize($meal);
}
}
55 changes: 55 additions & 0 deletions code/app/Adapters/KitchenService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Adapters;

use App\Meal\MealInterface;

class KitchenService
{
private static array $needFried = [
'bread',
'cutlet',
'sausage',
'sausage slices',
'onion',
'beef',
];

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

return $cookedIngredients;
}

/**
* @param MealInterface $meal
* @return bool
*/
public function checkMealQuality(MealInterface $meal): bool
{
foreach ($meal->getIngredients() as $ingredient => $value)
if (in_array($ingredient, self::$needFried)) return false;
return true;
}

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

namespace App;

use App\Observer\Customer;
use App\Strategy\BurgerStrategy;
use App\Strategy\HotDogStrategy;
use App\Strategy\OrderContext;
use App\Strategy\SandwichStrategy;
use Exception;

class Application
{
private $request;

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

/**
* @return void
* @throws Exception
*/
public function run(): void
{
$customer = new Customer();
$order = new OrderContext($customer);

switch ($_POST['meal']) {
case 'Burger':
$order->setCookingStrategy(new BurgerStrategy());
break;
case 'HotDog':
$order->setCookingStrategy(new HotDogStrategy());
break;
case 'Sandwich':
$order->setCookingStrategy(new SandwichStrategy());
break;
default:
throw new Exception('No meal choosed');
}

$meal = $order->getOrderedMeal($_POST['client_ingredients'] ?? []);
print_r($meal->getIngredients());
}
}
36 changes: 36 additions & 0 deletions code/app/Decorators/BurgerDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Decorators;


class BurgerDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;
private static array $baseRecipe = [
'pickles' => 3,
'onion' => 2,
'lettuce' => 1,
'cotlete' => 1,
];

/**
* @param MealDecorator $mealBaseClass
* @return void
*/
public function __constructor(MealDecorator $mealBaseClass): void
{
$this->mealDecorator = $mealBaseClass;
}

/**
* @return void
*/
public function addBaseIngredients(): void
{
$this->mealDecorator->ingredients = array_merge(
$this->mealDecorator->ingredients,
$this->mealDecorator->getAdapter()->addIngredients(self::$baseRecipe)
);
$this->mealDecorator->setStatus('Added base burger recipe ingredients');
}
}
34 changes: 34 additions & 0 deletions code/app/Decorators/CustomerIngredientsDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Decorators;

use App\Meal\MealBaseClass;

class CustomerIngredientsDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;

/**
* @param MealBaseClass $mealBaseClass
* @return void
*/
public function __constructor(MealBaseClass $mealBaseClass): void
{
$this->mealDecorator = $mealBaseClass;
}

/**
* @param array $customerIngredients
* @return void
*/
public function addCustomerIngredients(array $customerIngredients): void
{
$this->mealDecorator->ingredients = array_merge(
$this->mealDecorator->ingredients,
$this->mealDecorator->getAdapter()->addIngredients($customerIngredients)
);
$this->mealDecorator->setStatus('Added client Ingredients');
}


}
36 changes: 36 additions & 0 deletions code/app/Decorators/HotDogDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Decorators;

use App\Meal\MealBaseClass;

class HotDogDecorator extends MealDecorator
{
private MealDecorator $mealDecorator;
private static array $baseRecipe = [
'ketchup' => 1,
'mustard' => 1,
'sausage' => 1,
];

/**
* @param MealBaseClass $mealBaseClass
* @return void
*/
public function __constructor(MealBaseClass $mealBaseClass): void
{
$this->mealDecorator = $mealBaseClass;
}

/**
* @return void
*/
public function addBaseIngredients(): void
{
$this->mealDecorator->ingredients = array_merge(
$this->mealDecorator->ingredients,
$this->mealDecorator->getAdapter()->addIngredients(self::$baseRecipe)
);
$this->mealDecorator->setStatus('Added base hot-dog recipe ingredients');
}
}
76 changes: 76 additions & 0 deletions code/app/Decorators/MealDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Decorators;

use App\Meal\IngredientAdapter;
use App\Meal\MealBaseClass;
use SplObjectStorage;

class MealDecorator extends MealBaseClass
{
private MealBaseClass $mealBaseClass;
private SplObjectStorage $splObjectStorage;
private IngredientAdapter $adapter;

/**
* @param MealBaseClass $mealBaseClass
* @return void
*/
public function __constructor(MealBaseClass $mealBaseClass): void
{
parent::__constructor();
$this->mealBaseClass = $mealBaseClass;
$this->adapter = new IngredientAdapter();
$this->resetIngredients();
}

/**
* @return void
*/
public function resetIngredients(): void
{
$this->ingredients = $this->mealBaseClass->getIngredients();
}

/**
* @param string $status
* @return void
*/
public function setStatus(string $status): void
{
$this->mealBaseClass->setStatus($status);
}

/**
* @return string
*/
public function getBaseType(): string
{
return get_class($this->mealBaseClass);
}

/**
* @return array
*/
public function getIngredients(): array
{
return $this->ingredients;
}

/**
* @param array $ingredients
* @return void
*/
public function addIngredients(array $ingredients = []): void
{
$this->ingredients = array_merge($this->ingredients, $ingredients);
}

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