Completed
Push — master ( 2bf349...d68558 )
by Anton
03:05
created

Facade   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 109
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogger() 0 4 1
A setCredentials() 0 4 1
A setTransport() 0 4 1
A getClient() 0 12 4
A ping() 0 4 1
A sendEvent() 0 4 1
A makeDecision() 0 4 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
     * 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
    public static function setCredentials(CredentialsInterface $credentials)
49
    {
50
        self::$credentials = $credentials;
51
    }
52
53
    /**
54
     * Sets (or replaces) transport
55
     *
56
     * @param TransportInterface $transport
57
     */
58
    public static function setTransport(TransportInterface $transport)
59
    {
60
        self::$transport = $transport;
61
    }
62
63
    /**
64
     * Utility method to take client
65
     *
66
     * @return PublicAPIClient
67
     * @throws Exception
68
     */
69
    private static function getClient()
70
    {
71
        if (self::$client === null) {
72
            if (self::$credentials !== null && self::$transport !== null) {
73
                self::$client = new PublicAPIClient(self::$credentials, self::$transport, self::$logger);
74
            } else {
75
                throw new Exception('Unable to obtain client - credentials and/or transport not set');
76
            }
77
        }
78
79
        return self::$client;
80
    }
81
82
    /**
83
     * Sends request to Covery and returns access level, associated with
84
     * used credentials
85
     *
86
     * This method can be used for Covery health check and availability
87
     * On any problem (network, credentials, server side) this method
88
     * will throw an exception
89
     *
90
     * @return mixed
91
     * @throws Exception
92
     */
93
    public static function ping()
94
    {
95
        return self::getClient()->ping();
96
    }
97
98
    /**
99
     * Sends envelope to Covery and returns it's ID on Covery side
100
     * Before sending, validation is performed
101
     *
102
     * @param EnvelopeInterface $envelope
103
     * @return int
104
     * @throws Exception
105
     */
106
    public static function sendEvent(EnvelopeInterface $envelope)
107
    {
108
        return self::getClient()->sendEvent($envelope);
109
    }
110
111
    /**
112
     * Sends envelope to Covery for analysis
113
     *
114
     * @param EnvelopeInterface $envelope
115
     * @return Result
116
     * @throws Exception
117
     */
118
    public static function makeDecision(EnvelopeInterface $envelope)
119
    {
120
        return self::getClient()->makeDecision($envelope);
121
    }
122
}
123