forked from restcord/restcord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverriddenGuzzleClient.php
88 lines (78 loc) · 2.39 KB
/
OverriddenGuzzleClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* Copyright 2017 Aaron Scherer
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE
*
* @package restcord/restcord
* @copyright Aaron Scherer 2017
* @license MIT
*/
namespace RestCord;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use RestCord\Override\OverrideInterface;
/**
* @author Aaron Scherer <[email protected]>
*
* Client Class
*
* @property Interfaces\Channel $channel
* @property Interfaces\Gateway $gateway
* @property Interfaces\Guild $guild
* @property Interfaces\Invite $invite
* @property Interfaces\Oauth2 $oauth2
* @property Interfaces\User $user
* @property Interfaces\Voice $voice
* @property Interfaces\Webhook $webhook
*/
class OverriddenGuzzleClient extends GuzzleClient
{
/**
* @var callable
*/
private $category;
/**
* @var callable|null
*/
private $responseToResultTransformer;
/**
* OverriddenGuzzleClient constructor.
*
* @param ClientInterface $client
* @param DescriptionInterface $description
* @param callable|null $responseToResultTransformer
* @param callable $category
*/
public function __construct(
ClientInterface $client,
DescriptionInterface $description,
callable $responseToResultTransformer = null,
$category
) {
parent::__construct($client, $description, null, $responseToResultTransformer);
$this->category = $category;
$this->responseToResultTransformer = $responseToResultTransformer;
}
public function __call($name, array $args)
{
/** @var OverrideInterface $cls */
$cls = 'RestCord\\Override\\'.ucwords($this->category).'\\'.ucfirst($name);
if (class_exists($cls)) {
$result = $cls::run($this, isset($args[0]) ? $args[0] : [], substr($name, -5) === 'Async');
$transform = $this->responseToResultTransformer;
if ($result->getReasonPhrase() !== 'NO CONTENT') {
return $transform(
$result,
null,
new Command($name)
);
}
return $result;
}
return parent::__call($name, $args);
}
}