This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf30037
commit ca45d4e
Showing
8 changed files
with
189 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Alert extends Model | ||
{ | ||
protected $fillable = ['threshold', 'type', 'deleted', 'entity', 'sensor', 'lastSent', 'alertId']; | ||
private $relType = ['minore di', 'maggiore di']; | ||
|
||
|
||
public function getType() | ||
{ | ||
return $this->relType[$this->type]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use App\Models\Alert; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\RequestException; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class AlertServiceProvider extends BasicProvider | ||
{ | ||
private $request; | ||
|
||
/** | ||
* EntityServiceProvider constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(app()); | ||
$this->request = new Client([ | ||
'base_uri' => config('app.api') . '/alerts', | ||
'headers' => [ | ||
'Content-Type' => 'application/json' | ||
] | ||
]); | ||
} | ||
|
||
/** | ||
* @param mixed $identifier | ||
* @return Alert | ||
*/ | ||
public function find($identifier) | ||
{ | ||
try { | ||
$response = json_decode($this->request->get('/alerts/' . $identifier, $this->setHeaders())->getBody()); | ||
$alert = new Alert(); | ||
$alert->fill((array)$response); | ||
return $alert; | ||
} catch (RequestException $e) { | ||
$this->isExpired($e); | ||
abort($e->getCode(), $e->getResponse()->getReasonPhrase()); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @return array|null | ||
*/ | ||
public function findAll() | ||
{ | ||
try { | ||
$response = json_decode($this->request->get('', $this->setHeaders())->getBody()); | ||
$alerts = ['enable' => [], 'disable' => []]; | ||
foreach ($response->enabled as $e) { | ||
$alert = new Alert(); | ||
$alert->fill((array)$e); | ||
$alerts['enable'][] = $alert; | ||
} | ||
foreach ($response->disabled as $e) { | ||
$alert = new Alert(); | ||
$alert->fill((array)$e); | ||
$alerts['disable'][] = $alert; | ||
} | ||
return $alerts; | ||
} catch (RequestException $e) { | ||
$this->isExpired($e); | ||
abort($e->getCode(), $e->getResponse()->getReasonPhrase()); | ||
return null; | ||
} | ||
} | ||
public function disable($identifier) | ||
{ | ||
$this->request->post('/alerts/' . $identifier, array_merge($this->setHeaders(), [ | ||
'query' => ['userId' => Auth::id(), 'enable' => false] | ||
])); | ||
} | ||
public function enable($identifier) | ||
{ | ||
$this->request->post('/alerts/' . $identifier, array_merge($this->setHeaders(), [ | ||
'query' => ['userId' => Auth::id(), 'enable' => true] | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters