Skip to content

Commit

Permalink
Check gitter enabled attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagolizardo committed Jun 8, 2024
1 parent 47f7747 commit d86e981
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions config-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"integrations": {
"gitter": {
"enabled": false,
"token": "yourpersonaltoken",
"roomId": "yourroomid"
},
Expand Down
16 changes: 14 additions & 2 deletions src/Controllers/Clients/CreateClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Reconmap\Controllers\Controller;
use Reconmap\Models\AuditActions\ClientAuditActions;
use Reconmap\Models\Client;
use Reconmap\Repositories\ClientRepository;
use Reconmap\Services\ActivityPublisherService;

class CreateClientController extends Controller
{
public function __construct(private readonly ClientRepository $repository)
public function __construct(private readonly ClientRepository $repository, private readonly ActivityPublisherService $activityPublisherService)
{
}

public function __invoke(ServerRequestInterface $request): ResponseInterface
{
$loggedInUserId = $request->getAttribute('userId');

/** @var Client $client */
$client = $this->getJsonBodyDecodedAsClass($request, new Client());
$client->creator_uid = $request->getAttribute('userId');
$client->creator_uid = $loggedInUserId;

$client->id = $this->repository->insert($client);

$this->auditAction($loggedInUserId, $client);

return $this->createStatusCreatedResponse($client);
}

private function auditAction(int $loggedInUserId, Client $client): void
{
$this->activityPublisherService->publish($loggedInUserId, ClientAuditActions::CREATED, ['type' => 'client', 'id' => $client->id, 'name' => $client->name]);
}
}
4 changes: 2 additions & 2 deletions src/Controllers/Clients/DeleteClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class DeleteClientController extends Controller
{
public function __construct(
private ClientRepository $repository,
private ActivityPublisherService $activityPublisherService)
private readonly ClientRepository $repository,
private readonly ActivityPublisherService $activityPublisherService)
{
}

Expand Down
9 changes: 7 additions & 2 deletions src/Integrations/GitterIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Reconmap\Integrations;

use GuzzleHttp\Client;
use Reconmap\Services\ApplicationConfig;

class GitterIntegration implements Integration, ActivityPublisher
{
public function __construct(private ApplicationConfig $config)
public function __construct(private readonly ApplicationConfig $config)
{
}

Expand All @@ -33,7 +34,11 @@ public function publishActivity(string $activity): void

$configuration = (object)$this->getConfiguration();

$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.gitter.im/v1']);
if (!$configuration->enabled) {
return;
}

$client = new Client(['base_uri' => 'https://api.gitter.im/v1']);
$client->request('POST', '/v1/rooms/' . $configuration->roomId . '/chatMessages', [
'headers' => ['Authorization' => 'Bearer ' . $configuration->token],
'json' => [
Expand Down
6 changes: 3 additions & 3 deletions src/Models/AuditActions/AuditLogAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class AuditLogAction implements
VulnerabilityLogActions,
VulnerabilityCategoryLogActions
{
public const DATA_IMPORTED = 'Imported data';
public const DATA_EXPORTED = 'Exported data';
public const string DATA_IMPORTED = 'Imported data';
public const string DATA_EXPORTED = 'Exported data';

public const ORGANISATION_UPDATED = 'Updated organisation';
public const string ORGANISATION_UPDATED = 'Updated organisation';
}
6 changes: 3 additions & 3 deletions src/Models/AuditActions/ClientAuditActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface ClientAuditActions
{
public const CREATED = 'Created client';
public const UPDATED = 'Updated client';
public const DELETED = 'Deleted client';
public const string CREATED = 'Created client';
public const string UPDATED = 'Updated client';
public const string DELETED = 'Deleted client';
}
9 changes: 8 additions & 1 deletion tests/Controllers/Clients/CreateClientControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Fig\Http\Message\StatusCodeInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Reconmap\Models\AuditActions\ClientAuditActions;
use Reconmap\Models\Client;
use Reconmap\Repositories\ClientRepository;
use Reconmap\Services\ActivityPublisherService;

class CreateClientControllerTest extends TestCase
{
Expand All @@ -33,7 +35,12 @@ public function testHappyPath()
->method('getBody')
->willReturn('{"name":"exciting new client","address":"evergreen","url":"1.1.1.1"}');

$controller = new CreateClientController($mockProjectRepository);
$mockActivityPublisherService = $this->createMock(ActivityPublisherService::class);
$mockActivityPublisherService->expects($this->once())
->method('publish')
->with(9, ClientAuditActions::CREATED, ['type' => 'client', 'id' => 1, 'name' => $expectedClient->name]);

$controller = new CreateClientController($mockProjectRepository, $mockActivityPublisherService);
$response = $controller($mockRequest);

$this->assertEquals(StatusCodeInterface::STATUS_CREATED, $response->getStatusCode());
Expand Down

0 comments on commit d86e981

Please sign in to comment.