-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
408 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use App\Service\FruitAggregator; | ||
use App\Service\FruitDecomulator; | ||
|
||
final class IndexController extends AbstractController | ||
{ | ||
#[Route('/all', name: 'fruits_get_all', methods: ['GET'])] | ||
public function all(FruitDecomulator $fruit) | ||
{ | ||
dd($fruit->__invoke()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace App\Entity\Api; | ||
|
||
/** | ||
* A class used to create an Object Model from | ||
* a serialized response data | ||
*/ | ||
class FruitApiEntity | ||
{ | ||
protected array $items = []; | ||
protected int $total = 0; | ||
|
||
/** | ||
* @var array $items | ||
* | ||
* @return self | ||
*/ | ||
public function setItems(array $items): self | ||
{ | ||
$this->items = $items; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
/** | ||
* @var int $total | ||
* | ||
* @return self | ||
*/ | ||
public function setTotal(int $total): self | ||
{ | ||
$this->total = $total; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTotal(): int | ||
{ | ||
return $this->total; | ||
} | ||
|
||
/** | ||
* A helper function to return the entire properties as an array | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
return get_object_vars($this); | ||
} | ||
|
||
/** | ||
* Returns the first item in the array | ||
* | ||
* @return array | ||
*/ | ||
public function first(): array | ||
{ | ||
return current($this->toArray()['items']); | ||
} | ||
|
||
/** | ||
* Returns the last item in the array | ||
* | ||
* @return array | ||
*/ | ||
public function last(): array | ||
{ | ||
return end($this->toArray()['items']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use App\Service\FruitsApiClient; | ||
use App\Entity\Api\FruitApiEntity; | ||
use App\Repository\FruitRepository; | ||
use App\Entity\Fruit; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
class FruitAggregator | ||
{ | ||
public function __construct( | ||
private FruitsApiClient $api, | ||
private EntityManagerInterface $em | ||
) { | ||
} | ||
|
||
public function save() | ||
{ | ||
$fruitsFromApi = $this->api->get(); | ||
|
||
// @INFO: Throw an exception if the data is invalid | ||
if (! $fruitsFromApi instanceof FruitApiEntity) { | ||
throw new \Exception('Invalid response from the API endpoint'); | ||
} | ||
|
||
// @INFO: Do nothing if nothing is returned from the API call | ||
if (0 === $fruitsFromApi->getItems()) { | ||
return; | ||
} | ||
|
||
// @INFO: Check if the fetched data from API already exist in the database table | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use App\Entity\Fruit; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
/** | ||
* This service class will truncate the `fruits` table | ||
*/ | ||
class FruitDecomulator | ||
{ | ||
public function __construct( | ||
private EntityManagerInterface $em | ||
) { | ||
} | ||
|
||
/** | ||
* Truncate the `fruits` table | ||
* | ||
* @return void | ||
*/ | ||
public function __invoke(): void | ||
{ | ||
$connection = $this->em->getConnection(); | ||
$platform = $connection->getDatabasePlatform(); | ||
$table = $this->em->getClassMetadata(Fruit::class)->getTableName(); | ||
$connection->executeStatement($platform->getTruncateTableSQL($table, true)); | ||
} | ||
} |
Oops, something went wrong.