Skip to content

Commit

Permalink
Merge pull request #357 from cultuurnet/PPF-230-endpoint-for-ppf
Browse files Browse the repository at this point in the history
PPF-230 endpoint for ppf
  • Loading branch information
LucWollants authored Nov 24, 2023
2 parents 878318c + 7212327 commit fb1a81c
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 29 deletions.
8 changes: 8 additions & 0 deletions app/Project/ProjectControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CultuurNet\ProjectAanvraag\Project;

use CultuurNet\ProjectAanvraag\Project\Controller\ImportProjectController;
use CultuurNet\ProjectAanvraag\Project\Controller\ProjectController;
use Silex\Api\ControllerProviderInterface;
use Silex\Application;
Expand All @@ -26,11 +27,18 @@ public function connect(Application $app)
);
};

$app['import_project_controller'] = function (Application $app) {
return new ImportProjectController(
$app['command_bus']
);
};

/* @var ControllerCollection $controllers */
$controllers = $app['controllers_factory'];
$controllers->get('/', 'project_controller:getProjects');
$controllers->get('/{id}', 'project_controller:getProject');
$controllers->post('/', 'project_controller:createProject');
$controllers->post('/{uuid}', 'import_project_controller:importProject');
$controllers->delete('/{id}', 'project_controller:deleteProject');
$controllers->post('/{id}/request-activation', 'project_controller:requestActivation');
$controllers->get('/{id}/activate', 'project_controller:activateProject');
Expand Down
1 change: 1 addition & 0 deletions app/WebApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ protected function registerProviders()
new Path('^/widgets/autocomplete/regions', 'GET'),
new Path('^/event/', 'GET'),
new Path('^/upload', 'POST'),
new Path('^/project/.*$', 'POST'),
new Path('^.*$', 'OPTIONS'),
]
),
Expand Down
7 changes: 7 additions & 0 deletions app/config/messagebus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ handlers:
- integration_types.storage
- insightly_logger

import_project_handler:
command: CultuurNet\ProjectAanvraag\Project\Command\ImportProject
class: \CultuurNet\ProjectAanvraag\Project\CommandHandler\ImportProjectCommandHandler
arguments:
- orm.em
- insightly_logger

activate_project_handler:
command: CultuurNet\ProjectAanvraag\Project\Command\ActivateProject
class: \CultuurNet\ProjectAanvraag\Project\CommandHandler\ActivateProjectCommandHandler
Expand Down
94 changes: 94 additions & 0 deletions src/Project/Command/ImportProject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace CultuurNet\ProjectAanvraag\Project\Command;

class ImportProject
{
/**
* @var string
*/
private $userId;

/**
* @var string
*/
private $name;

/**
* @var string
*/
private $description;

/**
* @var int
*/
private $groupId;

/**
* @var string
*/
protected $platformUuid;

/**
* @var string
*/
private $testApiKeySapi3;

/**
* @var string
*/
private $liveApiKeySapi3;

public function __construct(
string $platformUuid,
string $userId,
string $name,
string $description,
int $groupId,
string $testApiKeySapi3,
string $liveApiKeySapi3
) {
$this->platformUuid = $platformUuid;
$this->userId = $userId;
$this->name = $name;
$this->description = $description;
$this->groupId = $groupId;
$this->testApiKeySapi3 = $testApiKeySapi3;
$this->liveApiKeySapi3 = $liveApiKeySapi3;
}

public function getUserId(): string
{
return $this->userId;
}

public function getName(): string
{
return $this->name;
}

public function getDescription(): string
{
return $this->description;
}

public function getGroupId(): int
{
return $this->groupId;
}

public function getPlatformUuid(): string
{
return $this->platformUuid;
}

public function getTestApiKeySapi3(): string
{
return $this->testApiKeySapi3;
}

public function getLiveApiKeySapi3(): string
{
return $this->liveApiKeySapi3;
}
}
52 changes: 52 additions & 0 deletions src/Project/CommandHandler/ImportProjectCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace CultuurNet\ProjectAanvraag\Project\CommandHandler;

use CultuurNet\ProjectAanvraag\Entity\Project;
use CultuurNet\ProjectAanvraag\Entity\User;
use CultuurNet\ProjectAanvraag\Project\Command\ImportProject;
use CultuurNet\ProjectAanvraag\User\UserInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;

class ImportProjectCommandHandler
{
/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @var LoggerInterface
*/
private $logger;

public function __construct(
EntityManagerInterface $entityManager,
LoggerInterface $logger
) {
$this->entityManager = $entityManager;
$this->logger = $logger;
}

public function handle(ImportProject $importProject): void
{
$this->logger->debug('Start handling ImportProject for ' . $importProject->getName());

$project = new Project();
$project->setName($importProject->getName());
$project->setDescription($importProject->getDescription());
$project->setGroupId($importProject->getGroupId());
$project->setUserId($importProject->getUserId());
$project->setPlatformUuid($importProject->getPlatformUuid());
$project->setTestApiKeySapi3($importProject->getTestApiKeySapi3());
$project->setLiveApiKeySapi3($importProject->getLiveApiKeySapi3());
$project->setStatus(Project::PROJECT_STATUS_APPLICATION_SENT);

$this->entityManager->persist($project);

$this->entityManager->flush();

$this->logger->debug('Finished handling ImportProject for ' . $importProject->getName());
}
}
47 changes: 47 additions & 0 deletions src/Project/Controller/ImportProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace CultuurNet\ProjectAanvraag\Project\Controller;

use CultuurNet\ProjectAanvraag\Project\Command\ImportProject;
use SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class ImportProjectController
{
use ValidateRequiredFieldsTrait;

/**
* @var MessageBusSupportingMiddleware
*/
private $commandBus;

public function __construct(MessageBusSupportingMiddleware $commandBus)
{
$this->commandBus = $commandBus;
}

public function importProject(string $uuid, Request $request): JsonResponse
{
$postedProject = json_decode($request->getContent());

$this->validate(
['userId', 'name', 'summary', 'groupId', 'testApiKeySapi3', 'liveApiKeySapi3'],
$postedProject
);

$this->commandBus->handle(
new ImportProject(
$uuid,
$postedProject->userId,
$postedProject->name,
$postedProject->summary,
$postedProject->groupId,
$postedProject->testApiKeySapi3,
$postedProject->liveApiKeySapi3
)
);

return new JsonResponse();
}
}
33 changes: 7 additions & 26 deletions src/Project/Controller/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use CultuurNet\ProjectAanvraag\Coupon\CouponValidatorInterface;
use CultuurNet\ProjectAanvraag\Entity\Project;
use CultuurNet\ProjectAanvraag\Insightly\InsightlyClientInterface;
use CultuurNet\ProjectAanvraag\Insightly\Item\ContactInfo;
use CultuurNet\ProjectAanvraag\Insightly\Item\EntityList;
use CultuurNet\ProjectAanvraag\Insightly\Item\Address as AddressEntity;
use CultuurNet\ProjectAanvraag\Insightly\Item\Link;
use CultuurNet\ProjectAanvraag\Insightly\Item\Organisation;
use CultuurNet\ProjectAanvraag\Insightly\Parser\OrganisationParser;
Expand All @@ -22,6 +19,7 @@
use CultuurNet\ProjectAanvraag\Project\Command\BlockProject;
use CultuurNet\ProjectAanvraag\Project\Command\CreateProject;
use CultuurNet\ProjectAanvraag\Project\Command\DeleteProject;
use CultuurNet\ProjectAanvraag\Project\Command\ImportProject;
use CultuurNet\ProjectAanvraag\Project\Command\RequestActivation;
use CultuurNet\ProjectAanvraag\Project\ProjectServiceInterface;
use SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware;
Expand All @@ -33,6 +31,8 @@

final class ProjectController
{
use ValidateRequiredFieldsTrait;

/**
* @var MessageBusSupportingMiddleware
*/
Expand Down Expand Up @@ -95,7 +95,7 @@ public function createProject(Request $request)
{
$postedProject = json_decode($request->getContent());

$this->validateRequiredFields(
$this->validate(
['name', 'summary', 'integrationType', 'termsAndConditions'],
$postedProject
);
Expand Down Expand Up @@ -184,7 +184,7 @@ public function requestActivation($id, Request $request)
$this->couponValidator->validateCoupon($postedData->coupon);
$this->commandBus->handle(new ActivateProject($project, $postedData->coupon));
} else {
$this->validateRequiredFields(
$this->validate(
['name', 'street', 'postal', 'city'],
$postedData
);
Expand Down Expand Up @@ -224,7 +224,7 @@ public function updateContentFilter(Request $request, $id)
$project = $this->getProjectWithAccessCheck($id, 'edit');
$data = json_decode($request->getContent());

$this->validateRequiredFields(['contentFilter'], $data);
$this->validate(['contentFilter'], $data);

$this->projectService->updateContentFilter($project, $data->contentFilter);

Expand Down Expand Up @@ -260,7 +260,7 @@ public function updateOrganisation($id, Request $request)
$project = $this->getProjectWithAccessCheck($id, 'edit');

$postedData = json_decode($request->getContent());
$this->validateRequiredFields(
$this->validate(
['name', 'addresses'],
$postedData
);
Expand Down Expand Up @@ -334,25 +334,6 @@ private function getProjectWithAccessCheck($id, $operation)
return $project;
}

/**
* Validate if all required fields are in the data.
* @param \stdClass $data
* @throws MissingRequiredFieldsException
*/
private function validateRequiredFields($requiredFields, \stdClass $data = null)
{
$emptyFields = [];
foreach ($requiredFields as $field) {
if (empty($data->$field)) {
$emptyFields[] = $field;
}
}

if (!empty($emptyFields)) {
throw new MissingRequiredFieldsException('Some required fields are missing: ' . implode(', ', $emptyFields));
}
}

/**
* Gets the organisation linked to a project
* @param Project $project
Expand Down
27 changes: 27 additions & 0 deletions src/Project/Controller/ValidateRequiredFieldsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace CultuurNet\ProjectAanvraag\Project\Controller;

use CultuurNet\ProjectAanvraag\Core\Exception\MissingRequiredFieldsException;

trait ValidateRequiredFieldsTrait
{
/**
* Validate if all required fields are in the data.
* @param \stdClass $data
* @throws MissingRequiredFieldsException
*/
private function validate($requiredFields, \stdClass $data = null)
{
$emptyFields = [];
foreach ($requiredFields as $field) {
if (empty($data->$field)) {
$emptyFields[] = $field;
}
}

if (!empty($emptyFields)) {
throw new MissingRequiredFieldsException('Some required fields are missing: ' . implode(', ', $emptyFields));
}
}
}
4 changes: 2 additions & 2 deletions test/Entity/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function testGetAndSet()
$project->setTotalWidgets(3);
$this->assertEquals(3, $project->getTotalWidgets());

$project->setCoupon('my-coupon');
$this->assertEquals('my-coupon', $project->getCoupon());
$project->setPlatformUuid('a050bfea-d45a-48b7-9645-1c07ccb42037');
$this->assertEquals('a050bfea-d45a-48b7-9645-1c07ccb42037', $project->getPlatformUuid());
}

/**
Expand Down
Loading

0 comments on commit fb1a81c

Please sign in to comment.