Completed
Push — master ( 1b0622...b59bdb )
by François
01:52
created

HttpLogger::getHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer\Logger;
13
14
use Bouncer\Identity;
15
use Bouncer\Request;
16
17
class HttpLogger implements LoggerInterface
18
{
19
20
    protected $endpoint;
21
22
    protected $httpClient;
23
24
    public function __construct($endpoint)
25
    {
26
        $this->endpoint = $endpoint;
27
    }
28
29
    public function getHttpClient()
30
    {
31
        if (empty($this->httpClient)) {
32
            $this->httpClient = new \Bouncer\Http\SimpleClient;
33
        }
34
        return $this->httpClient;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function log($connection, Identity $identity, Request $request)
41
    {
42
        $context = $connection;
43
        $context['request']  = $request->toArray();
44
        $context['identity'] = $identity->toArray();
45
46
        $result = $this->getHttpClient()->post($this->endpoint, $context);
47
48
        if (!$result) {
49
            error_log("Error while logging to Http endpoint: $this->endpoint");
50
        }
51
    }
52
53
}
54