Skip to content

Commit

Permalink
Adiciona versão mais recente mapacultural ce
Browse files Browse the repository at this point in the history
  • Loading branch information
lpirola committed Jun 18, 2024
1 parent c2a2a54 commit 6594c96
Show file tree
Hide file tree
Showing 72 changed files with 2,076 additions and 623 deletions.
1 change: 1 addition & 0 deletions api/mapas/app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ENV=
3 changes: 2 additions & 1 deletion api/mapas/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
vendor/
.env
34 changes: 33 additions & 1 deletion api/mapas/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Para criar um no cenário de teste funcional, basta adicionar sua nova classe no
```php
<?php

namespace App\Tests;
namespace App\Tests\Functional;

class MeuTest extends AbstractTestCase
{
Expand Down Expand Up @@ -417,6 +417,13 @@ Para executar os testes veja a seção <a href="#console-commands">Console Comma

---

## DI (Injeção de dependência)
Com a injeção de dependência diminuímos o acoplamento das nossas classes. E a razão para isso ser tão importante está no princípio da inversão de dependência em que o código deve depender de abstrações e não de implementações concretas.

Documentação PHP-DI: https://php-di.org

Todo o código se encontra no diretório `/app/config`, no arquivo `di.php`.

## Console Commands


Expand Down Expand Up @@ -451,11 +458,36 @@ php app/bin/console app:code-style

:memo: Fixtures são dados falsos com a finalidade de testes.

#### Configuração do ambiente

Antes de executar os fixtures, é necessário criar um arquivo `.env` dentro da pasta app (/app/.env). Este arquivo deve conter a configuração de ambiente necessária. Um arquivo de exemplo chamado `.env.example` foi fornecido para facilitar esse processo.

1. Copie o arquivo `.env.example` para `.env`:

```sh
cp .env.example .env
```

2. Abra o arquivo `.env` e configure as variáveis de ambiente. Para fins de desenvolvimento, você pode definir a variável `APP_ENV` como `local`:

```sh
APP_ENV=local
```


#### Executando os Fixtures


Para executar o conjunto de fixtures basta entrar no container da aplicação e executar
```
php app/bin/console app:fixtures
```
> **Observação:**
> Se o arquivo `.env` não for encontrado, você verá a seguinte mensagem de erro:
>
> ```sh
> Please create a .env file in the root directory (/app/.env)
> ```
</details>

<details>
Expand Down
11 changes: 10 additions & 1 deletion api/mapas/app/bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
require dirname(__DIR__, 2).'/vendor/autoload.php';
require dirname(__DIR__, 2).'/public/bootstrap.php';

use App\Application\Environment;
use App\Command\CodeStyleCommand;
use App\Command\DebugRouterCommand;
use App\Command\FixturesCommand;
use App\Command\TestsCommand;
use App\Command\WelcomeCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Dotenv\Dotenv;

$env = Environment::getEnvData();
$dotenv = new Dotenv();
$dotenv->load($env);

$application = new Application();
$entityManager = $app->em ?? null;
Expand All @@ -18,8 +24,11 @@ $application->addCommands([
new WelcomeCommand(),
new TestsCommand(),
new CodeStyleCommand(),
new FixturesCommand($entityManager),
new DebugRouterCommand(),
]);

if (true === Environment::isLocal()) {
$application->addCommands([new FixturesCommand($entityManager)]);
}

$application->run();
70 changes: 70 additions & 0 deletions api/mapas/app/config/di.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

use App\Repository\AgentRepository;
use App\Repository\EventRepository;
use App\Repository\Interface\AgentRepositoryInterface;
use App\Repository\Interface\EventRepositoryInterface;
use App\Repository\Interface\OpportunityRepositoryInterface;
use App\Repository\Interface\ProjectRepositoryInterface;
use App\Repository\Interface\SealRepositoryInterface;
use App\Repository\Interface\SpaceRepositoryInterface;
use App\Repository\Interface\TermRepositoryInterface;
use App\Repository\OpportunityRepository;
use App\Repository\ProjectRepository;
use App\Repository\SealRepository;
use App\Repository\SpaceRepository;
use App\Repository\TermRepository;
use App\Service\AgentService;
use App\Service\EventService;
use App\Service\Interface\AgentServiceInterface;
use App\Service\Interface\EventServiceInterface;
use App\Service\Interface\OpportunityServiceInterface;
use App\Service\Interface\ProjectServiceInterface;
use App\Service\Interface\SealServiceInterface;
use App\Service\Interface\SpaceServiceInterface;
use App\Service\Interface\TermServiceInterface;
use App\Service\OpportunityService;
use App\Service\ProjectService;
use App\Service\SealService;
use App\Service\SpaceService;
use App\Service\TermService;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;

return [
SerializerInterface::class => fn () => new Serializer([new ObjectNormalizer()]),
ValidatorInterface::class => fn () => Validation::createValidatorBuilder()->enableAttributeMapping()->getValidator(),
...repositories(),
...services(),
];

function repositories(): array
{
return [
AgentRepositoryInterface::class => fn () => new AgentRepository(),
EventRepositoryInterface::class => fn () => new EventRepository(),
OpportunityRepositoryInterface::class => fn () => new OpportunityRepository(),
ProjectRepositoryInterface::class => fn () => new ProjectRepository(),
SealRepositoryInterface::class => fn () => new SealRepository(),
SpaceRepositoryInterface::class => fn () => new SpaceRepository(),
TermRepositoryInterface::class => fn () => new TermRepository(),
];
}

function services(): array
{
return [
AgentServiceInterface::class => fn () => new AgentService(new AgentRepository(), new Serializer([new ObjectNormalizer()])),
EventServiceInterface::class => fn () => new EventService(new EventRepository(), new Serializer([new ObjectNormalizer()])),
OpportunityServiceInterface::class => fn () => new OpportunityService(new OpportunityRepository(), new Serializer([new ObjectNormalizer()])),
ProjectServiceInterface::class => fn () => new ProjectService(new ProjectRepository(), new Serializer([new ObjectNormalizer()])),
SealServiceInterface::class => fn () => new SealService(new AgentRepository(), new SealRepository(), new Serializer([new ObjectNormalizer()])),
SpaceServiceInterface::class => fn () => new SpaceService(new Serializer([new ObjectNormalizer()]), new SpaceRepository()),
TermServiceInterface::class => fn () => new TermService(new Serializer([new ObjectNormalizer()]), new TermRepository()),
];
}
2 changes: 1 addition & 1 deletion api/mapas/app/routes/api/agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'/api/v2/agents/{id}' => [
Request::METHOD_GET => [AgentApiController::class, 'getOne'],
Request::METHOD_PATCH => [AgentApiController::class, 'patch'],
Request::METHOD_DELETE => [AgentApiController::class, 'delete'],
Request::METHOD_DELETE => [AgentApiController::class, 'remove'],
],
'/api/v2/agents/{id}/opportunities' => [
Request::METHOD_GET => [OpportunityApiController::class, 'getOpportunitiesByAgent'],
Expand Down
2 changes: 1 addition & 1 deletion api/mapas/app/routes/api/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
'/api/v2/events/{id}' => [
Request::METHOD_GET => [EventApiController::class, 'getOne'],
Request::METHOD_PATCH => [EventApiController::class, 'patch'],
Request::METHOD_DELETE => [EventApiController::class, 'delete'],
Request::METHOD_DELETE => [EventApiController::class, 'remove'],
],
];
2 changes: 1 addition & 1 deletion api/mapas/app/routes/api/opportunity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'/api/v2/opportunities/{id}' => [
Request::METHOD_GET => [OpportunityApiController::class, 'getOne'],
Request::METHOD_PATCH => [OpportunityApiController::class, 'patch'],
Request::METHOD_DELETE => [OpportunityApiController::class, 'delete'],
Request::METHOD_DELETE => [OpportunityApiController::class, 'remove'],
],
];
2 changes: 1 addition & 1 deletion api/mapas/app/routes/api/project.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'/api/v2/projects/{id}' => [
Request::METHOD_GET => [ProjectApiController::class, 'getOne'],
Request::METHOD_PATCH => [ProjectApiController::class, 'patch'],
Request::METHOD_DELETE => [ProjectApiController::class, 'delete'],
Request::METHOD_DELETE => [ProjectApiController::class, 'remove'],
],
];
2 changes: 1 addition & 1 deletion api/mapas/app/routes/api/seal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'/api/v2/seals/{id}' => [
Request::METHOD_GET => [SealApiController::class, 'getOne'],
Request::METHOD_PATCH => [SealApiController::class, 'patch'],
Request::METHOD_DELETE => [SealApiController::class, 'delete'],
Request::METHOD_DELETE => [SealApiController::class, 'remove'],
],
];
2 changes: 1 addition & 1 deletion api/mapas/app/routes/api/space.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
'/api/v2/spaces/{id}' => [
Request::METHOD_GET => [SpaceApiController::class, 'getOne'],
Request::METHOD_DELETE => [SpaceApiController::class, 'delete'],
Request::METHOD_DELETE => [SpaceApiController::class, 'remove'],
Request::METHOD_PATCH => [SpaceApiController::class, 'patch'],
],
'/api/v2/spaces/{id}/events' => [
Expand Down
3 changes: 3 additions & 0 deletions api/mapas/app/routes/api/term.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
return [
'/api/v2/terms' => [
Request::METHOD_GET => [TermApiController::class, 'getList'],
Request::METHOD_POST => [TermApiController::class, 'post'],
],
'/api/v2/terms/{id}' => [
Request::METHOD_GET => [TermApiController::class, 'getOne'],
Request::METHOD_PATCH => [TermApiController::class, 'patch'],
Request::METHOD_DELETE => [TermApiController::class, 'remove'],
],
];
36 changes: 36 additions & 0 deletions api/mapas/app/src/Application/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Application;

use Symfony\Component\Dotenv\Dotenv;

class Environment
{
private const LOCAL = 'local';

public static function getEnvinronment(): string
{
$env = self::getEnvData();

if (false === file_exists($env) || false === isset($_ENV['APP_ENV'])) {
exit('Please create correctly a .env file in the root directory (/app/.env)'.PHP_EOL);
}

return $_ENV['APP_ENV'];
}

public static function getEnvData(): mixed
{
return dirname(__DIR__, 2).'/.env';
}

public static function isLocal(): bool
{
$dotenv = new Dotenv();
$dotenv->load(self::getEnvData());

return self::LOCAL === self::getEnvinronment();
}
}
12 changes: 8 additions & 4 deletions api/mapas/app/src/Command/FixturesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeLn([
PHP_EOL,
'==============================',
'== RUN DATA FIXTURES ==',
'==============================',
PHP_EOL,
'=================================',
'=== RUNNING DATA FIXTURES ',
]);

$loader = new Loader();
Expand All @@ -37,6 +35,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$executor = new ORMExecutor($this->entityManager, new ORMPurger());
$executor->execute($loader->getFixtures(), true);

$output->writeln([
'=== '.count($loader->getFixtures()).' fixtures executed',
'=================================',
PHP_EOL,
]);

return Command::SUCCESS;
}

Expand Down
3 changes: 2 additions & 1 deletion api/mapas/app/src/Command/TestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class TestsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('-------------------------------------------------------');
passthru("php vendor/bin/phpunit {$input->getArgument('path')}");
passthru('php app/bin/console app:fixtures');
passthru("php vendor/bin/phpunit {$input->getArgument('path')} --testdox --colors=always");
$output->writeln('-------------------------------------------------------');

return Command::SUCCESS;
Expand Down
19 changes: 19 additions & 0 deletions api/mapas/app/src/Controller/Api/AbstractApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Controller\Api;

use App\Exception\RequiredIdParamException;

abstract class AbstractApiController
{
public function extractIdParam(array $params): int
{
if (false === isset($params['id'])) {
throw new RequiredIdParamException();
}

return (int) $params['id'];
}
}
Loading

0 comments on commit 6594c96

Please sign in to comment.