Skip to content

Commit

Permalink
Merge pull request #7 from k-kaufmann/getRecipeById
Browse files Browse the repository at this point in the history
getRecipeById
  • Loading branch information
k-kaufmann authored Mar 20, 2021
2 parents 0c98098 + 4274682 commit 71b7327
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use chefkoch\clients\RecipeClient;
use chefkoch\clients\UserClient;
use chefkoch\model\Category;
use chefkoch\model\Recipe;
use chefkoch\model\User;

class ApiClient
Expand Down Expand Up @@ -45,6 +46,11 @@ public function getRecipesByCategories(array $categories, string $offset = "0"):
return $this->recipeClient->getRecipesByCategories($categories, $offset);
}

public function getRecipeById(string $id): Recipe
{
return $this->recipeClient->getRecipeById($id);
}

public function getCateogries(): array
{
return $this->categoryClient->getCategories();
Expand Down
2 changes: 2 additions & 0 deletions src/ChefkochFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use chefkoch\clients\UserClient;
use chefkoch\mapper\CategoryMapper;
use chefkoch\mapper\RecipeMapper;
use chefkoch\mapper\RecipeSimpleMapper;
use chefkoch\mapper\UserMapper;
use GuzzleHttp\Client;

Expand Down Expand Up @@ -37,6 +38,7 @@ public function createApiClient(): ApiClient
),
new RecipeClient(
new Client($this->basicRecipeClientConfiguration),
new RecipeSimpleMapper(),
new RecipeMapper()
),
new CategoryClient(
Expand Down
35 changes: 29 additions & 6 deletions src/clients/RecipeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@

namespace chefkoch\clients;

use chefkoch\exceptions\RecipeMappingException;
use chefkoch\exceptions\RecipeSimpleMappingException;
use chefkoch\mapper\RecipeMapper;
use chefkoch\mapper\RecipeSimpleMapper;
use chefkoch\model\Category;
use chefkoch\model\Recipe;
use chefkoch\model\RecipeSimple;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class RecipeClient
{
private Client $client;
private RecipeSimpleMapper $recipeSimpleMapper;
private RecipeMapper $recipeMapper;

public function __construct(Client $client, RecipeMapper $recipeMapper)
public function __construct(Client $client, RecipeSimpleMapper $recipeSimpleMapper, RecipeMapper $recipeMapper)
{
$this->client = $client;
$this->recipeSimpleMapper = $recipeSimpleMapper;
$this->recipeMapper = $recipeMapper;
}

Expand All @@ -39,14 +45,20 @@ public function getRecipes(string $offset = "0"): array
$response = json_decode($response->getBody()->getContents(), true);
foreach ($response['results'] as $result) {
try {
$recipesSimple[] = $this->recipeMapper->mapArray($result["recipe"]);
} catch (RecipeMappingException $recipeMappingException) {
$recipesSimple[] = $this->recipeSimpleMapper->mapArray($result["recipe"]);
} catch (RecipeSimpleMappingException $recipeMappingException) {
// TODO logging for recipeMappingExceptions
}
}
return $recipesSimple;
}

/**
* @param array $categories
* @param string $offset
* @return Category[]
* @throws GuzzleException]
*/
public function getRecipesByCategories(array $categories, string $offset): array
{
$recipesSimple = [];
Expand All @@ -65,14 +77,25 @@ public function getRecipesByCategories(array $categories, string $offset): array
$response = json_decode($response->getBody()->getContents(), true);
foreach ($response['results'] as $result) {
try {
$recipesSimple[] = $this->recipeMapper->mapArray($result["recipe"]);
} catch (RecipeMappingException $recipeMappingException) {
$recipesSimple[] = $this->recipeSimpleMapper->mapArray($result["recipe"]);
} catch (RecipeSimpleMappingException $recipeMappingException) {
// TODO logging for recipeMappingExceptions
}
}
return $recipesSimple;
}

public function getRecipeById(string $id): Recipe
{
$response = $this->client->get($id);
return $this->recipeMapper->mapArray(
json_decode(
$response->getBody()->getContents(),
true
)
);
}

private function buildQueryString(array $queryParams): string
{
$query = "";
Expand Down
11 changes: 11 additions & 0 deletions src/exceptions/RecipeSimpleMappingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace chefkoch\exceptions;

use Exception;

class RecipeSimpleMappingException extends Exception
{

}
61 changes: 38 additions & 23 deletions src/mapper/RecipeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,51 @@
namespace chefkoch\mapper;

use chefkoch\exceptions\RecipeMappingException;
use chefkoch\model\RecipeSimple;
use chefkoch\model\Recipe;
use DateTime;
use Exception;

class RecipeMapper
{
public function mapArray(array $recipeSimple): RecipeSimple
public function mapArray(array $recipe): Recipe
{
try {
return new RecipeSimple(
$recipeSimple["id"],
(int)$recipeSimple["type"],
$recipeSimple["title"],
$recipeSimple["subtitle"],
$recipeSimple["owner"],
$recipeSimple["rating"],
(int)$recipeSimple["difficulty"],
(bool)$recipeSimple["hasImage"],
(bool)$recipeSimple["hasVideo"],
$recipeSimple["previewImageId"],
(int)$recipeSimple["preparationTime"],
(bool)$recipeSimple["isSubmitted"],
(bool)$recipeSimple["isRejected"],
new DateTime($recipeSimple["createdAt"]),
(int)$recipeSimple["imageCount"],
$recipeSimple["editor"],
($recipeSimple["submissionDate"] === null) ? null : new DateTime($recipeSimple["submissionDate"]),
(bool)$recipeSimple["isPremium"],
(int)$recipeSimple["status"],
$recipeSimple["id"],
return new Recipe(
$recipe["id"],
$recipe["type"],
$recipe["title"],
$recipe["subtitle"],
$recipe["owner"],
$recipe["rating"],
$recipe["difficulty"],
$recipe["hasImage"],
$recipe["hasVideo"],
$recipe["previewImageId"],
$recipe["preparationTime"],
$recipe["isSubmitted"],
$recipe["isRejected"],
new DateTime($recipe["createdAt"]),
$recipe["imageCount"],
$recipe["editor"],
($recipe["submissionDate"] == null) ? null : new DateTime($recipe["submissionDate"]),
$recipe["isPremium"],
$recipe["status"],
$recipe["servings"],
$recipe["kCalories"],
$recipe["instructions"],
$recipe["miscellaneousText"],
$recipe["ingredientsText"],
$recipe["tags"],
$recipe["fullTags"],
$recipe["viewCount"],
$recipe["cookingTime"],
$recipe["restingTime"],
$recipe["totalTime"],
$recipe["ingredientGroups"],
$recipe["categoryIds"],
$recipe["recipeVideoId"],
$recipe["isIndexable"],
$recipe["siteUrl"]
);
} catch (Exception $exception) {
throw new RecipeMappingException($exception->getMessage());
Expand Down
42 changes: 42 additions & 0 deletions src/mapper/RecipeSimpleMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace chefkoch\mapper;

use chefkoch\exceptions\RecipeSimpleMappingException;
use chefkoch\model\RecipeSimple;
use DateTime;
use Exception;

class RecipeSimpleMapper
{
public function mapArray(array $recipeSimple): RecipeSimple
{
try {
return new RecipeSimple(
$recipeSimple["id"],
(int)$recipeSimple["type"],
$recipeSimple["title"],
$recipeSimple["subtitle"],
$recipeSimple["owner"],
$recipeSimple["rating"],
(int)$recipeSimple["difficulty"],
(bool)$recipeSimple["hasImage"],
(bool)$recipeSimple["hasVideo"],
$recipeSimple["previewImageId"],
(int)$recipeSimple["preparationTime"],
(bool)$recipeSimple["isSubmitted"],
(bool)$recipeSimple["isRejected"],
new DateTime($recipeSimple["createdAt"]),
(int)$recipeSimple["imageCount"],
$recipeSimple["editor"],
($recipeSimple["submissionDate"] === null) ? null : new DateTime($recipeSimple["submissionDate"]),
(bool)$recipeSimple["isPremium"],
(int)$recipeSimple["status"],
$recipeSimple["id"],
);
} catch (Exception $exception) {
throw new RecipeSimpleMappingException($exception->getMessage());
}
}
}
Loading

0 comments on commit 71b7327

Please sign in to comment.