1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Covery\Client; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Facade |
9
|
|
|
* |
10
|
|
|
* Static access facade to covery client |
11
|
|
|
* |
12
|
|
|
* @package Covery\Client |
13
|
|
|
*/ |
14
|
|
|
class Facade |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var CredentialsInterface |
18
|
|
|
*/ |
19
|
|
|
private static $credentials; |
20
|
|
|
/** |
21
|
|
|
* @var TransportInterface |
22
|
|
|
*/ |
23
|
|
|
private static $transport; |
24
|
|
|
/** |
25
|
|
|
* @var PublicAPIClient |
26
|
|
|
*/ |
27
|
|
|
private static $client; |
28
|
|
|
/** |
29
|
|
|
* @var LoggerInterface|null |
30
|
|
|
*/ |
31
|
|
|
private static $logger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set logger |
35
|
|
|
* |
36
|
|
|
* @param LoggerInterface $logger |
37
|
|
|
*/ |
38
|
|
|
public static function setLogger(LoggerInterface $logger) |
39
|
|
|
{ |
40
|
|
|
self::$logger = $logger; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sets (or replaces) credentials |
45
|
|
|
* |
46
|
|
|
* @param CredentialsInterface $credentials |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public static function setCredentials(CredentialsInterface $credentials) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
self::$credentials = $credentials; |
51
|
|
|
if (self::$transport !== null) { |
52
|
|
|
self::$client = new PublicAPIClient(self::$credentials, self::$transport, self::$logger); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets (or replaces) transport |
58
|
|
|
* |
59
|
|
|
* @param TransportInterface $transport |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public static function setTransport(TransportInterface $transport) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
self::$transport = $transport; |
64
|
|
|
if (self::$credentials !== null) { |
65
|
|
|
self::$client = new PublicAPIClient(self::$credentials, self::$transport, self::$logger); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sends request to Covery and returns access level, associated with |
71
|
|
|
* used credentials |
72
|
|
|
* |
73
|
|
|
* This method can be used for Covery health check and availability |
74
|
|
|
* On any problem (network, credentials, server side) this method |
75
|
|
|
* will throw an exception |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
* @throws Exception |
79
|
|
|
*/ |
80
|
|
|
public static function ping() |
81
|
|
|
{ |
82
|
|
|
if (self::$client === null) { |
83
|
|
|
throw new Exception('Credentials and/or transport not provided'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return self::$client->ping(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sends envelope to Covery and returns it's ID on Covery side |
91
|
|
|
* Before sending, validation is performed |
92
|
|
|
* |
93
|
|
|
* @param EnvelopeInterface $envelope |
94
|
|
|
* @return int |
95
|
|
|
* @throws Exception |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public static function sendEvent(EnvelopeInterface $envelope) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if (self::$client === null) { |
100
|
|
|
throw new Exception('Credentials and/or transport not provided'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return self::$client->sendEvent($envelope); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sends envelope to Covery for analysis |
108
|
|
|
* |
109
|
|
|
* @param EnvelopeInterface $envelope |
110
|
|
|
* @return Result |
111
|
|
|
* @throws Exception |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public static function makeDecision(EnvelopeInterface $envelope) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
if (self::$client === null) { |
116
|
|
|
throw new Exception('Credentials and/or transport not provided'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return self::$client->makeDecision($envelope); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.