Skip to content

WIP - Data collector #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions DataCollector/ApiCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace JGI\BillogramBundle\DataCollector;

class ApiCall
{
protected $url;

protected $statusCode;

public function __construct(string $url, int $statusCode)
{
$this->url = $url;
$this->statusCode = $statusCode;
}

public function getUrl(): string
{
return $this->url;
}

public function getStatusCode(): int
{
return $this->statusCode;
}

public function getPath(): string
{
$result = parse_url($this->getUrl());

return $result['path'];
}
}
60 changes: 60 additions & 0 deletions DataCollector/ApiCallCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace JGI\BillogramBundle\DataCollector;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class ApiCallCollector extends DataCollector
{
protected $logger;

protected $sandbox;

public function __construct(Logger $logger, bool $sandbox)
{
$this->logger = $logger;
$this->sandbox = $sandbox;
}

public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data['calls'] = $this->logger->getApiCalls();
$this->data['sandbox'] = $this->sandbox;
}

public function getApiCallsCount(): int
{
return count($this->data['calls']);
}

public function getApiCalls(): array
{
return $this->data['calls'];
}

public function hasErrors(): bool
{
/** @var \JGI\BillogramBundle\DataCollector\ApiCall $apiCall */
foreach ($this->data['calls'] as $apiCall) {
if ($apiCall->getStatusCode() != 200) {
return true;
}
}

return false;
}

public function isSandbox(): bool
{
return $this->data['sandbox'];
}

public function getName(): string
{
return 'jgi.billogram.collector';
}
}
20 changes: 20 additions & 0 deletions DataCollector/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace JGI\BillogramBundle\DataCollector;

class Logger
{
protected $apiCalls = [];

public function addApiCall(ApiCall $apiCall)
{
$this->apiCalls[] = $apiCall;
}

public function getApiCalls(): array
{
return $this->apiCalls;
}
}
2 changes: 2 additions & 0 deletions DependencyInjection/JGIBillogramExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('billogram.id', $config['id']);
$container->setParameter('billogram.password', $config['password']);
$container->setParameter('billogram.url', Api::API_URL_BASE);
$container->setParameter('billogram.sandbox', false);

if ($config['sandbox']) {
$container->setParameter('billogram.id', $config['sandbox_id']);
$container->setParameter('billogram.password', $config['sandbox_password']);
$container->setParameter('billogram.url', 'https://sandbox.billogram.com/api/v2');
$container->setParameter('billogram.sandbox', true);
}
}
}
25 changes: 25 additions & 0 deletions Listener/DataCollectorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace JGI\BillogramBundle\Listener;

use Billogram\Api\Event\RequestEvent;
use JGI\BillogramBundle\DataCollector\ApiCall;
use JGI\BillogramBundle\DataCollector\Logger;

class DataCollectorListener
{
protected $logger;

public function __construct(Logger $logger)
{
$this->logger = $logger;
}

/**
* @param RequestEvent $event
*/
public function whenRequestIsProcessed(RequestEvent $event)
{
$this->logger->addApiCall(new ApiCall($event->getUrl(), $event->getStatusCode()));
}
}
36 changes: 36 additions & 0 deletions Listener/LoggerListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace JGI\BillogramBundle\Listener;

use Billogram\Api\Event\RequestEvent;
use Psr\Log\LoggerInterface;

class LoggerListener
{
/**
* @var LoggerInterface
*/
protected $logger;

/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* @param RequestEvent $event
*/
public function whenRequestIsProcessed(RequestEvent $event)
{
$this->logger->info(
sprintf(
'Billogram request to "%s" with status code %s',
$event->getUrl(),
$event->getStatusCode()
)
);
}
}
31 changes: 31 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,34 @@ services:
- %billogram.password%
- null
- %billogram.url%
- ~
- "@event_dispatcher"

jgi.billogram.collector:
class: JGI\BillogramBundle\DataCollector\ApiCallCollector
arguments:
- "@jgi.billogram.collector.logger"
- %billogram.sandbox%
public: false
tags:
-
name: data_collector
template: 'JGIBillogramBundle:Collector:template.html.twig'
id: 'jgi.billogram.collector'

jgi.billogram.listener.logger:
class: JGI\BillogramBundle\Listener\LoggerListener
arguments:
- "@logger"
tags:
- { name: kernel.event_listener, event: billogram.request, method: whenRequestIsProcessed }

jgi.billogram.listener.data_collector:
class: JGI\BillogramBundle\Listener\DataCollectorListener
arguments:
- "@jgi.billogram.collector.logger"
tags:
- { name: kernel.event_listener, event: billogram.request, method: whenRequestIsProcessed }

jgi.billogram.collector.logger:
class: JGI\BillogramBundle\DataCollector\Logger
Loading