Skip to content

Commit 35b198e

Browse files
committed
Fix classes
1 parent f94df4e commit 35b198e

File tree

11 files changed

+193
-117
lines changed

11 files changed

+193
-117
lines changed

config/descom_lib.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'notification_manager' => [
5+
'url' => "http://notification-manager.descom.es/api",
6+
'token' => ""
7+
]
8+
];

config/notification_manager.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/NotificationManagerServiceProvider.php renamed to src/DescomLibServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Descom\NotificationManager;
3+
namespace DescomLib;
44

55
use Illuminate\Support\ServiceProvider;
66

7-
class NotificationManagerServiceProvider extends ServiceProvider
7+
class DescomLibServiceProvider extends ServiceProvider
88
{
99
/**
1010
* Bootstrap the application services.
@@ -13,7 +13,7 @@ public function boot()
1313
{
1414
if ($this->app->runningInConsole()) {
1515
$this->publishes([
16-
__DIR__.'/../config/notification_manager.php' => config_path('notification_manager.php'),
16+
__DIR__.'/../config/descom_lib.php' => config_path('descom_lib.php'),
1717
], 'config');
1818
}
1919
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace DescomLib\Exceptions;
3+
4+
use Exception;
5+
6+
/**
7+
* Exception temporal al aprovisionar en nutrium
8+
*/
9+
class PermanentException extends Exception
10+
{
11+
public function __contruct($message = null, $code = 0)
12+
{
13+
if (!$message) {
14+
throw new $this('Unknown '. self::class);
15+
}
16+
17+
parent::__construct($message, $code);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace DescomLib\Exceptions;
3+
4+
use Exception;
5+
6+
/**
7+
* Exception temporal al aprovisionar en nutrium
8+
*/
9+
class TemporaryException extends Exception
10+
{
11+
public function __contruct($message = null, $code = 0)
12+
{
13+
if (!$message) {
14+
throw new $this('Unknown '. self::class);
15+
}
16+
17+
parent::__construct($message, $code);
18+
}
19+
}

src/NotificationManagerClass.php

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace DescomLib\Services\NotificationManager;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\RequestException;
7+
use DescomLib\Exceptions\PermanentException;
8+
use DescomLib\Exceptions\TemporaryException;
9+
10+
class NotificationManager
11+
{
12+
protected $client = null;
13+
protected $url = null;
14+
protected $token = null;
15+
16+
/**
17+
* Create a new NotificationManager Instance.
18+
*/
19+
public function __construct()
20+
{
21+
$this->client = new Client();
22+
$this->url = config('notification_manager.url');
23+
$this->token = config('notification_manager.token');
24+
}
25+
26+
/**
27+
* Undocumented function
28+
*
29+
* @param Client $client
30+
* @return void
31+
*/
32+
public function setClient(Client $client)
33+
{
34+
$this->client = $client;
35+
}
36+
37+
/**
38+
* Undocumented function
39+
*
40+
* @param [type] $function
41+
* @param [type] $data
42+
* @return void
43+
*/
44+
public function send($function, $data)
45+
{
46+
try {
47+
$response = $this->client->request(
48+
'POST',
49+
$this->url . '/' . $function,
50+
[
51+
'headers' => [
52+
'Accept' => 'application/json',
53+
'Authorization' => $this->token
54+
],
55+
'http_errors' => false,
56+
'connect_timeout' => 30,
57+
'json' => $data
58+
]
59+
);
60+
61+
if ($response->getStatusCode() < 300) {
62+
return $response->json();
63+
}
64+
if ($response->getStatusCode() == 503) {
65+
throw new TemporaryException("Temporal error", 503);
66+
}
67+
throw new PermanentException("Permanent error", 503);
68+
} catch (RequestException $e) {
69+
throw new TemporaryException($e->getMessage(), 503);
70+
}
71+
}
72+
}

tests/ExampleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Descom\NotificationManager\Tests;
3+
namespace Tests;
44

55
use PHPUnit\Framework\TestCase;
66

tests/NotificationManagerTest.php

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Tests\Services\NotificationManager;
4+
5+
use Tests\TestCase;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Psr7\Response;
9+
use GuzzleHttp\Handler\MockHandler;
10+
use DescomLib\Exceptions\PermanentException;
11+
use DescomLib\Exceptions\TemporaryException;
12+
use DescomLib\Services\NotificationManager\NotificationManager;
13+
14+
class NotificationManagerTest extends TestCase
15+
{
16+
public function testLoggedEmail()
17+
{
18+
$data = [];
19+
$responseExpected = [
20+
'message' => 'Notificaciones enviadas con éxito'
21+
];
22+
23+
$mock = new MockHandler([new Response(200, [], json_encode($responseExpected))]);
24+
$handlerStack = HandlerStack::create($mock);
25+
$client = new Client(['handler' => $handlerStack]);
26+
27+
$notificationManager = new NotificationManager;
28+
$notificationManager->setClient($client);
29+
30+
$response = $notificationManager->send("logged_email", $data);
31+
32+
$this->assertEquals($responseExpected, $response);
33+
}
34+
35+
public function testLoggedEmailPermanentException()
36+
{
37+
$data = [];
38+
39+
$mock = new MockHandler([new Response(404, [], null)]);
40+
$handlerStack = HandlerStack::create($mock);
41+
$client = new Client(['handler' => $handlerStack]);
42+
43+
$notificationManager = new NotificationManager;
44+
$notificationManager->setClient($client);
45+
46+
$response = $notificationManager->send("logged_email", $data);
47+
48+
$this->assertEquals(get_class(PermanentException), get_class($response));
49+
}
50+
51+
public function testLoggedEmailTemporaryException()
52+
{
53+
$data = [];
54+
55+
$mock = new MockHandler([new Response(503, [], null)]);
56+
$handlerStack = HandlerStack::create($mock);
57+
$client = new Client(['handler' => $handlerStack]);
58+
59+
$notificationManager = new NotificationManager;
60+
$notificationManager->setClient($client);
61+
62+
$response = $notificationManager->send("logged_email", $data);
63+
64+
$this->assertEquals(get_class(TemporaryException), get_class($response));
65+
}
66+
}

0 commit comments

Comments
 (0)