Completed
Push — master ( 37faaa...541bbf )
by Raffael
10:18 queued 06:30
created

Feedback   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 26 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Feedback;
13
14
use GuzzleHttp\ClientInterface as GuzzleHttpClientInterface;
15
use Psr\Log\LoggerInterface;
16
17
class Feedback
18
{
19
    /**
20
     * Logger.
21
     *
22
     * @var LoggerInterface
23
     */
24
    protected $logger;
25
26
    /**
27
     * Client.
28
     *
29
     * @var GuzzleHttpClientInterface
30
     */
31
    protected $client;
32
33
    /**
34
     * Remote.
35
     *
36
     * @var string
37
     */
38
    protected $remote;
39
40
    /**
41
     * Constructor.
42
     */
43
    public function __construct(GuzzleHttpClientInterface $client, LoggerInterface $logger)
44
    {
45
        $this->client = $client;
46
        $this->logger = $logger;
47
    }
48
49
    /**
50
     * Handle.
51
     */
52
    public function handle(): bool
53
    {
54
        $input = fopen('php://input', 'r');
55
56
        $this->logger->info('send feedback report to [remote]', [
57
            'category' => get_class($this),
58
            'remote' => $this->client->getConfig()['base_uri'] ?? '',
59
        ]);
60
61
        $res = $this->client->post('', [
62
            'body' => $input,
63
            'http_errors' => false,
64
        ]);
65
66
        if ($res->getStatusCode() !== 201) {
67
            $this->logger->error('sending feedback report failed with code [code] and message [error]', [
68
                'category' => get_class($this),
69
                'code' => $res->getStatusCode(),
70
                'error' => $res->getBody()->read(10240), //only read 2KB which should be enaugh as error description
71
            ]);
72
73
            throw new Exception\InvalidFeedback('processing feedback failed with http code '.$res->getStatusCode());
74
        }
75
76
        return true;
77
    }
78
}
79