1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api\Providers; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Exceptions\AuthException; |
6
|
|
|
use seregazhuk\PinterestBot\Exceptions\InvalidRequestException; |
7
|
|
|
|
8
|
|
|
class ProviderWrapper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Provider |
12
|
|
|
*/ |
13
|
|
|
private $provider; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Provider|object $provider |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Provider $provider) |
19
|
|
|
{ |
20
|
|
|
$this->provider = $provider; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Proxies a call to a provider with a login check |
25
|
|
|
* before every method if needed. |
26
|
|
|
* |
27
|
|
|
* @param $method |
28
|
|
|
* @param $arguments |
29
|
|
|
* |
30
|
|
|
* @throws AuthException |
31
|
|
|
* @throws InvalidRequestException |
32
|
|
|
* |
33
|
|
|
* @return mixed|null |
34
|
|
|
*/ |
35
|
|
|
public function __call($method, $arguments) |
36
|
|
|
{ |
37
|
|
|
if (method_exists($this->provider, $method)) { |
38
|
|
|
$this->checkMethodForLoginRequired($method); |
39
|
|
|
|
40
|
|
|
return call_user_func_array([$this->provider, $method], $arguments); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$errorMessage = $this->getErrorMethodCallMessage($method, "Method $method does'n exist."); |
44
|
|
|
throw new InvalidRequestException($errorMessage); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Checks if method requires login and if true, |
49
|
|
|
* checks logged in status. |
50
|
|
|
* |
51
|
|
|
* @param $method |
52
|
|
|
* |
53
|
|
|
* @throws AuthException if is not logged in |
54
|
|
|
*/ |
55
|
|
|
protected function checkMethodForLoginRequired($method) |
56
|
|
|
{ |
57
|
|
|
$isLoggedIn = $this->provider->getRequest()->isLoggedIn(); |
58
|
|
|
$methodRequiresLogin = $this->provider->checkMethodRequiresLogin($method); |
59
|
|
|
|
60
|
|
|
if ($methodRequiresLogin && !$isLoggedIn) { |
61
|
|
|
$errorMessage = $this->getErrorMethodCallMessage($method, "You must log in before."); |
62
|
|
|
throw new AuthException($errorMessage); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $method |
68
|
|
|
* @param string $message |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function getErrorMethodCallMessage($method, $message) |
72
|
|
|
{ |
73
|
|
|
$providerClass = get_class($this->provider); |
74
|
|
|
|
75
|
|
|
return "Error calling $providerClass::$method method. $message"; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|