mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-09 11:03:17 +00:00
Add api webhook adapter
This commit is contained in:
94
lib/classes/API/Webhook.php
Normal file
94
lib/classes/API/Webhook.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2014 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Alchemy\Phrasea\Application;
|
||||
use Alchemy\Phrasea\Exception\RuntimeException;
|
||||
|
||||
class API_Webhook
|
||||
{
|
||||
protected $appbox;
|
||||
protected $id;
|
||||
protected $type;
|
||||
protected $data;
|
||||
protected $created;
|
||||
|
||||
public function __construct(appbox $appbox, $id)
|
||||
{
|
||||
$this->appbox = $appbox;
|
||||
$this->id = $id;
|
||||
$sql = 'SELECT `type`, `data`, created
|
||||
FROM api_webhooks
|
||||
WHERE id = :id';
|
||||
$stmt = $this->appbox->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':id' => $id));
|
||||
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$row) {
|
||||
throw new RuntimeException('Webhooks not found');
|
||||
}
|
||||
|
||||
$stmt->closeCursor();
|
||||
|
||||
$this->type = $row['type'];
|
||||
$this->data = json_decode($row['data']);
|
||||
$this->created = new \DateTime($row['created']);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$sql = 'DELETE FROM api_webhooks WHERE id = :id';
|
||||
|
||||
$stmt = $this->appbox->get_connection()->prepare($sql);
|
||||
$stmt->execute(array(':id' => $this->id));
|
||||
$stmt->closeCursor();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public static function create(appbox $appbox, $type, array $data)
|
||||
{
|
||||
$sql = 'INSERT INTO api_webhooks (id, `type`, `data`, created)
|
||||
VALUES (null, :type, :data, NOW())';
|
||||
|
||||
$stmt = $appbox->get_connection()->prepare($sql);
|
||||
$stmt->execute([
|
||||
'type' => $type,
|
||||
'data' => json_encode($data),
|
||||
]);
|
||||
$stmt->closeCursor();
|
||||
|
||||
return new API_Webhook($appbox, $appbox->get_connection()->lastInsertId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function getCreated()
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
23
tests/classes/api/api_webhookTest.php
Normal file
23
tests/classes/api/api_webhookTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Alchemy\Phrasea\Exception\RuntimeException;
|
||||
|
||||
class API_WebhookTest extends PhraseanetPHPUnitAbstract
|
||||
{
|
||||
public function testsNewApiHook()
|
||||
{
|
||||
$w = \API_Webhook::create(self::$DI['app']['phraseanet.appbox'], 'new_feed', array('w1', 'salut' => 'you'));
|
||||
$this->assertInstanceOf('\API_webhook', $w);
|
||||
$w->delete();
|
||||
}
|
||||
|
||||
public function testNewApiHookObjectNotFound()
|
||||
{
|
||||
try {
|
||||
$w = new \API_Webhook(self::$DI['app']['phraseanet.appbox'], -1);
|
||||
$this->fail('It should raise an exception');
|
||||
} catch (RuntimeException $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user