Skip to content

Commit

Permalink
Github integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneisenschmidt committed Nov 27, 2017
1 parent 1e14cb7 commit 24e507d
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ demo-import: .application-demo-import

demo-remove: .application-demo-remove

github-import: .application-github-import

github-remove: .application-github-remove

default-publish:
@$(call .publish,dist/docker-run.default.sh,sveneisenschmidt/yay,$(DOCKER_ENV),$(DOCKER_BRANCH))

Expand Down
19 changes: 19 additions & 0 deletions dist/examples/demo/example-01.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

# Enable demo integration
make demo-import

# Start application
make start

# Create new player
curl -X "POST" http://localhost:50080/api/players/ \
-d "{\"name\": \"Jane Doe\",\"username\":\"jane.doe\",\"email\": \"jane.doe@example.org\",\"image_url\":\"https://api.adorable.io/avatars/128/354\"}"

# Perform demo action 5x
curl -X "POST" http://localhost:50080/api/progress/ \
-d "{\"username\":\"jane.doe\",\"actions\":[\"demo-action\",\"demo-action\",\"demo-action\",\"demo-action\",\"demo-action\"]}"

# Get player activity
curl -X "GET" http://localhost:50080/api/players/jane.doe/personal-activities/
8 changes: 8 additions & 0 deletions dist/examples/github/example-01.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

# Enable demo integration
make github-import

# Start application
make start
1 change: 1 addition & 0 deletions integration/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ integration:
actions: ~
achievements: ~
validators: ~
webhooks: ~
6 changes: 6 additions & 0 deletions integration/github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
integration:
levels:
actions: ~
achievements: ~
validators: ~
webhooks: ~
25 changes: 24 additions & 1 deletion makedefs/application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,34 @@

.application-demo-remove-fixtures:
@$(call .docker-run,cli,'\
php bin/console yay:integration:disable default && \
php bin/console yay:integration:disable default && \
php bin/console yay:integration:disable demo && \
php bin/console cache:clear --no-warmup && \
php bin/console cache:warmup')

.application-github-import: \
.application-clean-database \
.application-github-import-fixtures

.application-github-remove: \
.application-github-remove-fixtures \
.application-clean-database

.application-github-import-fixtures:
@$(call .docker-run,cli,'\
php bin/console yay:integration:enable default integration/default && \
php bin/console yay:integration:enable github integration/github && \
php bin/console yay:recalculate && \
php bin/console cache:clear --no-warmup && \
php bin/console cache:warmup')

.application-github-remove-fixtures:
@$(call .docker-run,cli,'\
php bin/console yay:integration:disable default && \
php bin/console yay:integration:disable github && \
php bin/console cache:clear --no-warmup && \
php bin/console cache:warmup')

.application-test:
@$(call .docker-run,cli,'\
php bin/console doctrine:schema:drop --env=test --force --em=default && \
Expand Down
13 changes: 10 additions & 3 deletions src/App/Api/Controller/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Api\Validator\EntityValidator;
use Component\Engine\Engine;
use Component\Entity\PlayerInterface;
use Component\Entity\Activity;

/**
* @Route("/players")
Expand Down Expand Up @@ -382,11 +383,17 @@ public function indexPersonalActivitiesAction(
throw $this->createNotFoundException();
}

/** @var PlayerInterface $player */
$player = $players->first();
$activities = $players
->first()
->getActivities()
->toArray();

\usort($activities, function (Activity $a, Activity $b) {
return $a->getCreatedAt() > $b->getCreatedAt() ? -1 : 1;
});

return $serializer->createResponse(
$player->getActivities(),
$activities,
['player.personal_activities.show']
);
}
Expand Down
32 changes: 32 additions & 0 deletions src/App/Api/EventListener/KernelExceptionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace App\Api\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use App\Api\Response\ResponseSerializer;

class KernelExceptionListener
{
public function __construct(ResponseSerializer $serializer)
{
$this->serializer = $serializer;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$response = $this->serializer->createResponse([
'exception' => get_class($exception),
'message' => $exception->getMessage()
]);

if ($exception instanceof HttpExceptionInterface) {

This comment has been minimized.

Copy link
@sveneisenschmidt

sveneisenschmidt Nov 27, 2017

Author Owner

Set application json header

$response->setStatusCode($exception->getStatusCode());
} else {
$response->setStatusCode(500);
}

$event->setResponse($response);
}
}
16 changes: 10 additions & 6 deletions src/App/Api/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ services:
App\Api\Response\ResponseSerializer: ~
App\Api\Validator\EntityValidator: ~

App\Api\EventListener\KernelExceptionListener:
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

App\Api\Serializer\EventListener\LinkListener:
public: true
tags:
- { name: jms_serializer.event_listener, event: serializer.post_serialize, method: onPostSerialize, direction: serialization }

Component\HttpFoundation\Request\ParamConverter\QueryStringConverter:
tags:
- { name: request.param_converter, priority: -2, converter: QueryString }
Expand All @@ -23,10 +32,5 @@ services:
- { name: request.param_converter, priority: -2, converter: JsonField }
Component\HttpFoundation\Request\ParamConverter\DeserializeFieldConverter:
tags:
- { name: request.param_converter, priority: -2, converter: DeserializeField }

App\Api\Serializer\EventListener\LinkListener:
public: true
tags:
- { name: jms_serializer.event_listener, event: serializer.post_serialize, method: onPostSerialize, direction: serialization }
- { name: request.param_converter, priority: -2, converter: DeserializeField }

2 changes: 1 addition & 1 deletion src/App/Engine/Controller/EngineControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function advance(
foreach ($actions as $action) {
$actionDefinitions = $engine->findActionDefinitionBy(['name' => $action]);
if ($actionDefinitions->isEmpty()) {
continue;
throw $this->createNotFoundException(sprintf('Action "%s" not found', $action));
}

$personalActionCollection->add(
Expand Down

0 comments on commit 24e507d

Please # to comment.