Skip to content

Commit 1382b95

Browse files
committed
User session chages, user data api, room api
1 parent 501e313 commit 1382b95

File tree

6 files changed

+391
-69
lines changed

6 files changed

+391
-69
lines changed

src/Exceptions/ApiException.php

+34-36
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,6 @@
1515
*/
1616
class ApiException extends Exception
1717
{
18-
19-
/**
20-
* @internal
21-
* @param RequestException $e
22-
* @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException|
23-
* MethodNotAllowedException|NotFoundException|RateLimitExceededException|UnsupportedAcceptHeaderException|
24-
* UnsupportedContentTypeException|ValidationException
25-
*/
26-
public static function create(RequestException $e) {
27-
28-
if($response = $e->getResponse()) {
29-
switch ($response->getStatusCode()) {
30-
case 400:
31-
return new ValidationException($e);
32-
case 401:
33-
return new AuthenticationException($e);
34-
case 403:
35-
return new AccessDeniedException($e);
36-
case 404:
37-
return new NotFoundException($e);
38-
case 405:
39-
return new MethodNotAllowedException($e);
40-
case 406:
41-
return new UnsupportedAcceptHeaderException($e);
42-
case 409:
43-
return new ConflictingStateException($e);
44-
case 415:
45-
return new UnsupportedContentTypeException($e);
46-
case 429:
47-
return new RateLimitExceededException($e);
48-
}
49-
}
50-
51-
return new ApiException($e);
52-
}
53-
5418
/**
5519
* @var RequestException
5620
* @internal
@@ -69,6 +33,39 @@ public function getRequestException()
6933
return $this->exception;
7034
}
7135

36+
/**
37+
* @internal
38+
* @param RequestException $e
39+
* @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException|
40+
* MethodNotAllowedException|NotFoundException|RateLimitExceededException|UnsupportedAcceptHeaderException|
41+
* UnsupportedContentTypeException|ValidationException
42+
*/
43+
public static function create(RequestException $e) {
44+
if ($response = $e->getResponse()) {
45+
switch ($response->getStatusCode()) {
46+
case 400:
47+
return new ValidationException($e);
48+
case 401:
49+
return new AuthenticationException($e);
50+
case 403:
51+
return new AccessDeniedException($e);
52+
case 404:
53+
return new NotFoundException($e);
54+
case 405:
55+
return new MethodNotAllowedException($e);
56+
case 406:
57+
return new UnsupportedAcceptHeaderException($e);
58+
case 409:
59+
return new ConflictingStateException($e);
60+
case 415:
61+
return new UnsupportedContentTypeException($e);
62+
case 429:
63+
return new RateLimitExceededException($e);
64+
}
65+
}
66+
return new ApiException($e);
67+
}
68+
7269
/**
7370
* Exception constructor
7471
*
@@ -80,6 +77,7 @@ public function getRequestException()
8077
public function __construct(RequestException $e)
8178
{
8279
$this->exception = $e;
80+
$this->message = (string) $e->getMessage();
8381
parent::__construct();
8482
}
8583
}

src/Matrix.php

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Updivision\Matrix\Exceptions\ConflictingStateException;
1111
use Updivision\Matrix\Exceptions\RateLimitExceededException;
1212
use Updivision\Matrix\Exceptions\UnsupportedContentTypeException;
13+
use Updivision\Matrix\Resources\Room;
14+
use Updivision\Matrix\Resources\UserData;
1315
use Updivision\Matrix\Resources\UserSession;
1416

1517
/**
@@ -150,4 +152,14 @@ public function session()
150152
{
151153
return $this->session;
152154
}
155+
156+
public function user()
157+
{
158+
return new UserData($this);
159+
}
160+
161+
public function room()
162+
{
163+
return new Room($this);
164+
}
153165
}

src/Resources/AbstractResource.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
abstract class AbstractResource
1616
{
17-
1817
/**
1918
* @var Matrix
2019
* @internal
@@ -27,6 +26,14 @@ abstract class AbstractResource
2726
*/
2827
protected $endpoint;
2928

29+
/**
30+
* The resource endpoint
31+
*
32+
* @internal
33+
* @var string
34+
*/
35+
protected $data = null;
36+
3037
/**
3138
* Resource constructor
3239
*
@@ -39,19 +46,20 @@ abstract class AbstractResource
3946
public function __construct(Matrix $matrix)
4047
{
4148
$this->matrix = $matrix;
49+
$this->data = session('updivision_matrix_data');
4250
}
4351

4452
/**
4553
* Creates the endpoint
4654
*
47-
* @param null $id The endpoint terminator
55+
* @param null $resId The endpoint terminator
4856
* @return string
4957
* @internal
5058
*
5159
*/
52-
protected function endpoint($id = null)
60+
protected function endpoint($resId = null)
5361
{
54-
return $id === null ? $this->endpoint : $this->endpoint . '/' . $id;
62+
return $resId === null ? $this->endpoint : $this->endpoint . '/' . $resId;
5563
}
5664

5765
/**
@@ -62,4 +70,20 @@ protected function matrix()
6270
{
6371
return $this->matrix;
6472
}
73+
74+
protected function setData($data)
75+
{
76+
session(['updivision_matrix_data' => $data]);
77+
$this->data = $data;
78+
}
79+
80+
protected function getData()
81+
{
82+
return $this->data;
83+
}
84+
85+
protected function check()
86+
{
87+
return $this->data !== null;
88+
}
6589
}

src/Resources/Room.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Updivision\Matrix\Resources;
4+
5+
use Updivision\Matrix\Resources\AbstractResource;
6+
7+
/**
8+
* Room management
9+
*
10+
* This provides methods to create and update rooms
11+
*
12+
* @package Matrix\Resources
13+
*/
14+
class Room extends AbstractResource
15+
{
16+
/**
17+
* The resource endpoint
18+
*
19+
* @internal
20+
* @var string
21+
*/
22+
protected $endpoint = '';
23+
24+
public function createDirect($userId)
25+
{
26+
if ($this->check()) {
27+
return $this->matrix()->request('POST', $this->endpoint('createRoom'), [
28+
'preset' => 'trusted_private_chat',
29+
'visibility' => 'private',
30+
'invite' => [$userId],
31+
'is_direct' => true,
32+
'initial_state' => [[
33+
'content' => [
34+
'guest_access' => 'can_join'
35+
],
36+
'type' => 'm.room.guest_access',
37+
'state_key' => ''
38+
]]
39+
], [
40+
'access_token' => $this->data['access_token']
41+
]);
42+
}
43+
throw new \Exception('Not authenticated');
44+
}
45+
46+
//{
47+
// "preset":"trusted_private_chat",
48+
// "visibility":"private",
49+
// "invite":["@alyk:msg.pornfyre.com"],
50+
// "is_direct":true,
51+
// "initial_state":[
52+
// {
53+
// "content": {
54+
// "guest_access":"can_join"
55+
// },
56+
// "type":"m.room.guest_access",
57+
// "state_key":""
58+
// }
59+
// ]
60+
//}
61+
}

0 commit comments

Comments
 (0)