Completed
Push — master ( 1191a0...cc27d1 )
by yuuki
12s
created

LoggerClient   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 43
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A client() 0 10 1
A setTemplate() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 */
17
18
namespace Ytake\PrestoClient;
19
20
use GuzzleHttp\Client;
21
use GuzzleHttp\Middleware;
22
use GuzzleHttp\HandlerStack;
23
use GuzzleHttp\ClientInterface;
24
use GuzzleHttp\MessageFormatter;
25
use Psr\Log\LoggerInterface;
26
27
/**
28
 * Class LoggerClient
29
 *
30
 * @author Yuuki Takezawa <[email protected]>
31
 */
32
class LoggerClient
33
{
34
    /** @var LoggerInterface */
35
    protected $logger;
36
37
    /** @var string */
38
    protected $template = '{date_common_log} {uri} {req_headers} {req_body} {res_headers}';
39
40
    /**
41
     * LoggerClient constructor.
42
     *
43
     * @param LoggerInterface $logger
44
     */
45
    public function __construct(LoggerInterface $logger)
46
    {
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * @param callable|null $handler
52
     *
53
     * @return ClientInterface
54
     */
55
    public function client(callable $handler = null): ClientInterface
56
    {
57
        $handlerStack = HandlerStack::create($handler);
58
        $handlerStack->push(
59
            Middleware::log($this->logger, new MessageFormatter($this->template))
60
        );
61
        return new Client([
62
            'handler' => $handlerStack,
63
        ]);
64
    }
65
66
    /**
67
     * @codeCoverageIgnore
68
     * @param string $template
69
     */
70
    public function setTemplate(string $template)
71
    {
72
        $this->template = $template;
73
    }
74
}
75