Completed
Push — master ( 4968a9...664a15 )
by Anton
02:59
created

Facade::setCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
     * @var string|null
35
     */
36
    private static $url = null;
37
38
    /**
39
     * Set logger
40
     *
41
     * @param LoggerInterface $logger
42
     */
43
    public static function setLogger(LoggerInterface $logger)
44
    {
45
        self::$logger = $logger;
46
    }
47
48
    /**
49
     * Sets (or replaces) credentials
50
     *
51
     * @param CredentialsInterface $credentials
52
     */
53
    public static function setCredentials(CredentialsInterface $credentials)
54
    {
55
        self::$credentials = $credentials;
56
    }
57
58
    /**
59
     * Sets (or replaces) transport
60
     *
61
     * @param TransportInterface $transport
62
     */
63
    public static function setTransport(TransportInterface $transport)
64
    {
65
        self::$transport = $transport;
66
    }
67
68
    /**
69
     * Set custom Covery url
70
     *
71
     * @param string $url
72
     */
73
    public static function setOverrideUrl($url)
74
    {
75
        self::$url = $url;
76
    }
77
78
    /**
79
     * Utility method to take client
80
     *
81
     * @return PublicAPIClient
82
     * @throws Exception
83
     */
84
    private static function getClient()
85
    {
86
        if (self::$client === null) {
87
            if (self::$credentials !== null && self::$transport !== null) {
88
                self::$client = new PublicAPIClient(self::$credentials, self::$transport, self::$url, self::$logger);
89
            } else {
90
                throw new Exception('Unable to obtain client - credentials and/or transport not set');
91
            }
92
        }
93
94
        return self::$client;
95
    }
96
97
    /**
98
     * Sends request to Covery and returns access level, associated with
99
     * used credentials
100
     *
101
     * This method can be used for Covery health check and availability
102
     * On any problem (network, credentials, server side) this method
103
     * will throw an exception
104
     *
105
     * @return mixed
106
     * @throws Exception
107
     */
108
    public static function ping()
109
    {
110
        return self::getClient()->ping();
111
    }
112
113
    /**
114
     * Sends envelope to Covery and returns it's ID on Covery side
115
     * Before sending, validation is performed
116
     *
117
     * @param EnvelopeInterface $envelope
118
     * @return int
119
     * @throws Exception
120
     */
121
    public static function sendEvent(EnvelopeInterface $envelope)
122
    {
123
        return self::getClient()->sendEvent($envelope);
124
    }
125
126
    /**
127
     * Sends envelope to Covery for analysis
128
     *
129
     * @param EnvelopeInterface $envelope
130
     * @return Result
131
     * @throws Exception
132
     */
133
    public static function makeDecision(EnvelopeInterface $envelope)
134
    {
135
        return self::getClient()->makeDecision($envelope);
136
    }
137
}
138