Skip to content

Commit

Permalink
feat: implementa funçoes de _logs_
Browse files Browse the repository at this point in the history
  • Loading branch information
valdeir2000 committed Sep 22, 2020
1 parent bdd02e1 commit ea74da5
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 10 deletions.
110 changes: 108 additions & 2 deletions upload/system/library/PagSeguro/src/Domains/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Monolog\Logger as Monolog;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\HtmlFormatter;

class Logger
{
Expand All @@ -20,9 +19,13 @@ public static function getInstance(): Monolog
if (self::$instance === null) {
self::init();

if (!is_dir(PAGSEGURO_LOG)) {
mkdir(PAGSEGURO_LOG, 0777, true);
}

$dateFormat = "Y-m-d\TH:i:s";
$output = "%datetime% :: %level_name% :: %message% %context% %extra%\n";
$formatter = new HtmlFormatter($dateFormat);
$formatter = new PsrFormatter($dateFormat);

$stream = new StreamHandler(PAGSEGURO_LOG . '/' . date('Y-m-d') . '.log');
$stream->setFormatter($formatter);
Expand All @@ -34,6 +37,109 @@ public static function getInstance(): Monolog
return self::$instance;
}

/**
* Adds a log record at an arbitrary level.
*
* @param mixed $level The log level
* @param string $message The log message
* @param array $context The log context
*/
public function log($level, $message, array $context = []): void
{
(self::getInstance())->log($level, $message, $context);
}

/**
* Adds a log record at the DEBUG level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function debug($message, array $context = []): void
{
self::log(Monolog::DEBUG, $message, $context);
}

/**
* Adds a log record at the INFO level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function info($message, array $context = []): void
{
self::log(Monolog::INFO, $message, $context);
}

/**
* Adds a log record at the NOTICE level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function notice($message, array $context = []): void
{
self::log(Monolog::NOTICE, $message, $context);
}

/**
* Adds a log record at the WARNING level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function warning($message, array $context = []): void
{
self::log(Monolog::WARNING, $message, $context);
}

/**
* Adds a log record at the ERROR level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function error($message, array $context = []): void
{
self::log(Monolog::ERROR, $message, $context);
}

/**
* Adds a log record at the CRITICAL level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function critical($message, array $context = []): void
{
self::log(Monolog::CRITICAL, $message, $context);
}

/**
* Adds a log record at the ALERT level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function alert($message, array $context = []): void
{
self::log(Monolog::ALERT, $message, $context);
}

/**
* Adds a log record at the EMERGENCY level.
*
* @param string $message The log message
* @param array $context The log context
*/
public function emergency($message, array $context = []): void
{
self::log(Monolog::EMERGENCY, $message, $context);
}

/**
* Cria as constantes necessárias
*/
private static function init()
{
if (!defined('DS')) {
Expand Down
15 changes: 14 additions & 1 deletion upload/system/library/PagSeguro/src/Exception/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

namespace ValdeirPsr\PagSeguro\Exception;

use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Domains\Logger\Logger;

class Auth extends \RuntimeException
{

public function __construct(
Environment $env,
?string $message = null,
int $code = null,
\Throwable $previous = null
) {
Logger::emergency('Credenciais inválidas', [
'e-mail' => $env->getEmail(),
'token' => $env->getToken(),
]);
}
}
10 changes: 10 additions & 0 deletions upload/system/library/PagSeguro/src/Exception/PagSeguroRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use DOMDocument;
use DOMXPath;
use Throwable;
use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Domains\Error;
use ValdeirPsr\PagSeguro\Domains\Logger\Logger;
use Curl\Curl;

class PagSeguroRequest extends \Exception
Expand All @@ -15,6 +17,7 @@ class PagSeguroRequest extends \Exception
private $errors = [];

public function __construct(
Environment $env,
Curl $curl,
$requestBody,
string $message = null,
Expand All @@ -26,6 +29,13 @@ public function __construct(
$this->request = $curl;
$this->requestBody = $requestBody;

Logger::warning('Erro ao realizar pagamento', [
'e-mail' => $env->getEmail(),
'token' => $env->getToken(),
'request' => $requestBody,
'response' => $this->getResponse()
]);

$this->checkErrors();
}

Expand Down
20 changes: 18 additions & 2 deletions upload/system/library/PagSeguro/src/Request/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Domains\Transaction;
use ValdeirPsr\PagSeguro\Domains\Logger\Logger;
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException;
use ValdeirPsr\PagSeguro\Exception\PagSeguroRequest as PagSeguroRequestException;

Expand Down Expand Up @@ -39,16 +40,31 @@ protected function request(string $path = '')
{
$url = $this->buildUrl($path);

Logger::info('Checando uma notificação', [
'e-mail' => $this->env->getEmail(),
'token' => $this->env->getToken(),
'code' => $path
]);

$request = Factory::request($this->env);
$request->get($url);
$request->close();

if ($request->isSuccess()) {
return $request->getResponse();
$response = $request->getResponse();

Logger::info('Notificação verificada', [
'e-mail' => $this->env->getEmail(),
'token' => $this->env->getToken(),
'code' => $path,
'response' => $response
]);

return $response;
} elseif ($request->getHttpStatus() === 401) {
throw new AuthException('Check your credentials', 1000);
} else {
throw new PagSeguroRequestException($request, $path);
throw new PagSeguroRequestException($this->env, $request, $path);
}
}
}
27 changes: 24 additions & 3 deletions upload/system/library/PagSeguro/src/Request/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Domains\Payment;
use ValdeirPsr\PagSeguro\Domains\Transaction;
use ValdeirPsr\PagSeguro\Domains\Logger\Logger;
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException;
use ValdeirPsr\PagSeguro\Exception\PagSeguroRequest as PagSeguroRequestException;

Expand Down Expand Up @@ -110,6 +111,7 @@ protected function buildUrl(string $path = '', $apiVersion = 'v2'): string
protected function request($data, string $path = '', string $method = 'POST', $apiVersion = 'v2')
{
$url = $this->buildUrl($path, $apiVersion);
$uid = time() . ':' . uniqid();

$request = Factory::request($this->env);

Expand All @@ -123,16 +125,35 @@ protected function request($data, string $path = '', string $method = 'POST', $a
$request->post($url, $data);
} elseif ($method === 'GET') {
$request->get($url);
$data = $path;
}

$request->close();

Logger::info('Realiza uma requisição de pagamento', [
'uid' => $uid,
'e-mail' => $this->env->getEmail(),
'token' => $this->env->getToken(),
'type' => get_called_class(),
'request' => $data
]);

if ($request->isSuccess()) {
return $request->getResponse();
$response = $request->getResponse();

Logger::info('Realiza uma requisição de pagamento', [
'uid' => $uid,
'e-mail' => $this->env->getEmail(),
'token' => $this->env->getToken(),
'type' => get_called_class(),
'response' => $response
]);

return $response;
} elseif ($request->getHttpStatus() === 401) {
throw new AuthException('Check your credentials', 1000);
throw new AuthException($this->env, 'Check your credentials', 1000);
} else {
throw new PagSeguroRequestException($request, $data);
throw new PagSeguroRequestException($this->env, $request, $data);
}
}
}
18 changes: 16 additions & 2 deletions upload/system/library/PagSeguro/src/Request/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use ValdeirPsr\PagSeguro\Domains\Environment;
use ValdeirPsr\PagSeguro\Domains\Logger\Logger;
use ValdeirPsr\PagSeguro\Exception\Auth as AuthException;

class Session
Expand All @@ -26,6 +27,11 @@ public function generate(): string
{
$url = $this->buildUrl();

Logger::debug('Gerando sessão para o usuário', [
'e-mail' => $this->env->getEmail(),
'token' => $this->env->getToken()
]);

$request = Factory::request($this->env);
$request->post($url);
$request->close();
Expand All @@ -38,10 +44,18 @@ public function generate(): string
$session = $dom->getElementsByTagName('id');

if ($session->count() > 0) {
return trim($session->item(0)->textContent);
$session_id = trim($session->item(0)->textContent);

Logger::debug('Token gerado', [
'e-mail' => $this->env->getEmail(),
'token' => $this->env->getToken(),
'session' => $session_id
]);

return $session_id;
}
} elseif ($request->getHttpStatus() === 401) {
throw new AuthException('Check your credentials', 1000);
throw new AuthException($this->env, 'Check your credentials', 1000);
}
}

Expand Down

0 comments on commit ea74da5

Please sign in to comment.