clrghouz/app/Events/Matrix/Base.php

64 lines
1.6 KiB
PHP

<?php
namespace App\Events\Matrix;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use App\Models\Echoarea;
abstract class Base
{
protected $_data = [];
public function __construct(array $request)
{
Log::info(sprintf('EMb:- Event Initialised [%s]',get_class($this)));
$this->_data = json_decode(json_encode($request));
}
/**
* Enable getting values for keys in the response
*
* @note: This method is limited to certain values to ensure integrity reasons
* @note: Classes should return:
* + channel_id,
* + team_id,
* + ts,
* + user_id
* @param string $key
* @return mixed|object
* @throws ConnectionException
*/
public function __get(string $key)
{
switch ($key) {
case 'echoarea':
$rooms = collect(config('matrix.rooms'));
return Echoarea::where('name',$rooms->get($this->room_id))->single();
case 'room':
$room_alias = Http::withToken(config('matrix.as_token'))
->get(sprintf('https://%s/_matrix/client/v3/rooms/%s/state/m.room.canonical_alias',config('matrix.server'),$this->room_id));
return $room_alias->json('alias',$this->room_id);
case 'topic':
$subject = Http::withToken(config('matrix.as_token'))
->get(sprintf('https://%s/_matrix/client/v3/rooms/%s/state/m.room.topic',config('matrix.server'),$this->room_id));
return $subject->json('topic','Message from Matrix');
case 'ts':
return object_get($this->_data,'origin_server_ts');
case 'event_id':
case 'room_id':
default:
return object_get($this->_data,$key);
}
}
}