Page MenuHomePhabricator

Hook.php
No OneTemporary

Hook.php

<?php namespace magic3w\hook\sdk;
use magic3w\http\url\reflection\URLReflection;
use spitfire\io\Request;
/*
* The MIT License
*
* Copyright 2018 César de la Cal Bretschneider <cesar@magic3w.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Hook
{
private $endpoint;
private $appid;
private $sso;
/**
*
* @var \signature\Signature
*/
private $signature;
public function __construct($endpoint, $sso) {
$reflection = URLReflection::fromURL($endpoint);
$location = sprintf('%s://%s:%s%s', $reflection->getProtocol(), $reflection->getServer(), $reflection->getPort(), $reflection->getPath());
$this->endpoint = rtrim($location, '/');
$this->appid = $reflection->getUser();
$this->signature = strval($sso->makeSignature($this->appid));
}
public function authenticate() {
$request = $this->request(rtrim($this->endpoint, '/') . '/app/authenticate.json');
$request->get('signature', (string) $this->signature);
return $request->send()->expect(200)->json();
}
public function on($src, $target) {
$request = $this->request(rtrim($this->endpoint, '/') . '/listener/on/' . $src . '.json');
$request->get('signature', (string)$this->signature);
$request->get('target', $target);
return $request->send()->expect(200)->json();
}
public function listeningFor($appId) {
$request = $this->request(rtrim($this->endpoint, '/') . '/listener/target/' . $appId . '.json');
$request->get('signature', (string)$this->signature);
return $request->send()->expect(200)->json();
}
public function trigger($event, $payload = null, $defer = null) {
if ($defer > time()) {
$schedule = $defer;
$defer = null;
}
else {
$schedule = null;
}
$request = $this->request(rtrim($this->endpoint, '/') . '/listener/trigger.json');
$request->get('event', $event);
$request->get('defer', $defer);
$request->get('schedule', $schedule);
$request->get('signature', (string)$this->signature);
$request->header('Content-type', 'application/json');
$request->post(json_encode($payload));
$response = $request->send()->expect(200)->json();
return $response;
}
public function register($src, $tgt, $id, $event, $url, $defer = 0, $format = 'json') {
$request = $this->request(rtrim($this->endpoint, '/') . '/listener/register.json');
$request->get('signature', (string)$this->signature);
$request->post('app', $src);
$request->post('target', $tgt);
$request->post('hid', $id);
$request->post('listen', $event);
$request->post('url', $url);
$request->post('defer', $defer);
$request->post('format', $format);
return $request->send()->expect(200)->json();
}
private function request($url) {
return new Request($url);
}
}

File Metadata

Mime Type
text/x-php
Expires
Apr 12 2021, 5:23 PM (9 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5298
Default Alt Text
Hook.php (3 KB)

Event Timeline