Client::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace MaartenStaa\GameAnalytics;
2
3
use Http\Client\HttpClient;
4
use Http\Discovery\HttpClientDiscovery;
5
use Http\Discovery\MessageFactoryDiscovery;
6
use Http\Message\MessageFactory;
7
8
/**
9
 * Main GA client.
10
 *
11
 * @author Maarten Staa
12
 */
13
class Client
14
{
15
    const API_ENDPOINT = 'http://api.gameanalytics.com/';
16
    const API_ENDPOINT_SANDBOX = 'http://sandbox-api.gameanalytics.com/';
17
    const API_VERSION = 'v2';
18
19
    /**
20
     * The game key from GameAnalytics.
21
     *
22
     * @var string
23
     */
24
    protected $key;
25
26
    /**
27
     * The game's secret key from GameAnalytics.
28
     *
29
     * @var string
30
     */
31
    protected $secret;
32
33
    /**
34
     * The HTTP handler that should be used.
35
     *
36
     * @var \Http\Client\HttpClient
37
     */
38
    protected $http;
39
40
    /**
41
     * The HTTP message factory that should be used.
42
     *
43
     * @var \Http\Message\MessageFactory
44
     */
45
    protected $factory;
46
47
    /**
48
     * Whether this client should communicate with the sandbox servers instead
49
     * of the real API endpoints.
50
     *
51
     * @var bool
52
     */
53
    protected $sandbox = false;
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param string $key
59
     * @param string $secret
60
     * @param \Http\Client\HttpClient|null $http
61
     * @param \Http\Message\MessageFactory|null $factory
62
     */
63
    public function __construct($key, $secret, HttpClient $http = null, MessageFactory $factory = null)
64
    {
65
        $this->key = $key;
66
        $this->secret = $secret;
67
        $this->http = $http ?: HttpClientDiscovery::find();
68
        $this->factory = $factory ?: MessageFactoryDiscovery::find();
69
    }
70
71
    /**
72
     * Set whether this client should refer to the sandbox endpoint.
73
     *
74
     * @param bool $value
75
     */
76
    public function sandbox($value)
77
    {
78
        $this->sandbox = $value;
79
    }
80
81
    /**
82
     * Get the configured game's secret key from GameAnalytics.
83
     *
84
     * @return string
85
     */
86
    public function getSecret()
87
    {
88
        return $this->secret;
89
    }
90
91
    /**
92
     * Get the configured HTTP handler.
93
     *
94
     * @return \Http\Client\HttpAdapter
0 ignored issues
show
Documentation introduced by
Should the return type not be HttpClient?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
95
     */
96
    public function getHttp()
97
    {
98
        return $this->http;
99
    }
100
101
    /**
102
     * Get the configured HTTP message factory.
103
     *
104
     * @return \Http\Message\MessageFactory
105
     */
106
    public function getMessageFactory()
107
    {
108
        return $this->factory;
109
    }
110
111
    /**
112
     * Get the URL that events should be posted to.
113
     *
114
     * @return string
115
     */
116
    public function getEndpoint($api)
117
    {
118
        return ($this->sandbox ? self::API_ENDPOINT_SANDBOX : self::API_ENDPOINT) .
119
            self::API_VERSION . '/' . $this->key . '/'. $api;
120
    }
121
122
    /**
123
     * Get a new init message.
124
     *
125
     * @return \MaartenStaa\GameAnalytics\Message
126
     */
127
    public function init()
128
    {
129
        return new Message($this->getEndpoint('init'), $this);
130
    }
131
132
    /**
133
     * Get a new event message for the given category.
134
     *
135
     * @param  string $category
136
     * @return \MaartenStaa\GameAnalytics\Message
137
     */
138
    public function event($category)
139
    {
140
        $message = new Message($this->getEndpoint('events'), $this);
141
        $message->set('category', $category);
142
143
        return $message;
144
    }
145
146
    /**
147
     * Create a "user" event. Shortcut for event('user').
148
     *
149
     * @return \MaartenStaa\GameAnalytics\Message
150
     */
151
    public function user()
152
    {
153
        return $this->event('user');
154
    }
155
156
    /**
157
     * Create a "session_end" event. Shortcut for event('session_end').
158
     *
159
     * @return \MaartenStaa\GameAnalytics\Message
160
     */
161
    public function sessionEnd()
162
    {
163
        return $this->event('session_end');
164
    }
165
166
    /**
167
     * Create a "business" event. Shortcut for event('business').
168
     *
169
     * @return \MaartenStaa\GameAnalytics\Message
170
     */
171
    public function business()
172
    {
173
        return $this->event('business');
174
    }
175
176
    /**
177
     * Create a "resource" event. Shortcut for event('resource').
178
     *
179
     * @return \MaartenStaa\GameAnalytics\Message
180
     */
181
    public function resource()
182
    {
183
        return $this->event('resource');
184
    }
185
186
    /**
187
     * Create a "progression" event. Shortcut for event('progression').
188
     *
189
     * @return \MaartenStaa\GameAnalytics\Message
190
     */
191
    public function progression()
192
    {
193
        return $this->event('progression');
194
    }
195
196
    /**
197
     * Create a "design" event. Shortcut for event('design').
198
     *
199
     * @return \MaartenStaa\GameAnalytics\Message
200
     */
201
    public function design()
202
    {
203
        return $this->event('design');
204
    }
205
206
    /**
207
     * Create a "error" event. Shortcut for event('error').
208
     *
209
     * @return \MaartenStaa\GameAnalytics\Message
210
     */
211
    public function error()
212
    {
213
        return $this->event('error');
214
    }
215
}
216