Skip to content

Commit

Permalink
chore: code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
benborla committed Nov 17, 2023
1 parent 2e10035 commit 2b99dec
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"symfony/dotenv": "6.3.*",
"symfony/flex": "^2",
"symfony/framework-bundle": "6.3.*",
"symfony/http-client": "6.3.*",
"symfony/runtime": "6.3.*",
"symfony/yaml": "6.3.*"
},
Expand Down
172 changes: 171 additions & 1 deletion api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions api/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
app.api_endpoint: '%env(API_ENDPOINT)%'

services:
# default configuration for services in *this* file
Expand All @@ -22,3 +23,17 @@ services:

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

# @INFO: Register FruitsApi
App\Service\FruitsApiClient:
arguments:
$apiEndpoint: '%app.api_endpoint%'

# @INFO: Register FruitsApiEntity
App\Entity\Api\FruitApiEntity:

# @INFO: Register FruitAggregator
App\Service\FruitAggregator:

# @INFO: Register FruitDecomulator
App\Service\FruitDecomulator:
17 changes: 17 additions & 0 deletions api/src/Controller/IndexController.php
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());
}
}
83 changes: 83 additions & 0 deletions api/src/Entity/Api/FruitApiEntity.php
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']);
}
}
35 changes: 35 additions & 0 deletions api/src/Service/FruitAggregator.php
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
}
}
30 changes: 30 additions & 0 deletions api/src/Service/FruitDecomulator.php
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));
}
}
Loading

0 comments on commit 2b99dec

Please sign in to comment.