Completed
Pull Request — master (#33)
by Márk
31:08
created

MessageJournal::addFailure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\Client\Exception;
6
use Http\Client\Plugin\Journal;
7
use Http\Message\Formatter;
8
use Http\Message\Formatter\SimpleFormatter;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
14
15
/**
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
class MessageJournal extends DataCollector implements Journal
19
{
20
    /**
21
     * @var Formatter
22
     */
23
    private $formatter;
24
25
    /**
26
     * @param Formatter $formatter
27
     */
28
    public function __construct(Formatter $formatter = null)
29
    {
30
        $this->formatter = $formatter ?: new SimpleFormatter();
31
        $this->data = ['success' => [], 'failure' => []];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function addSuccess(RequestInterface $request, ResponseInterface $response)
38
    {
39
        $this->data['success'][] = [
40
            'request' => $this->formatter->formatRequest($request),
41
            'response' => $this->formatter->formatResponse($response),
42
        ];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addFailure(RequestInterface $request, Exception $exception)
49
    {
50
        if ($exception instanceof Exception\HttpException) {
51
            $formattedResponse = $this->formatter->formatResponse($exception->getResponse());
52
        } elseif ($exception instanceof Exception\TransferException) {
53
            $formattedResponse = $exception->getMessage();
54
        } else {
55
            $formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
56
        }
57
58
        $this->data['failure'][] = [
59
            'request' => $this->formatter->formatRequest($request),
60
            'response' => $formattedResponse,
61
        ];
62
    }
63
64
    /**
65
     * Get the successful request-resonse pairs.
66
     *
67
     * @return array
68
     */
69
    public function getSucessfulRequests()
70
    {
71
        return $this->data['success'];
72
    }
73
74
    /**
75
     * Get the failed request-resonse pairs.
76
     *
77
     * @return array
78
     */
79
    public function getFailedRequests()
80
    {
81
        return $this->data['failure'];
82
    }
83
84
    /**
85
     * Get the total number of request made.
86
     *
87
     * @return int
88
     */
89
    public function getTotalRequests()
90
    {
91
        return count($this->data['success']) + count($this->data['failure']);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function collect(Request $request, Response $response, \Exception $exception = null)
98
    {
99
        // We do not need to collect any data form the Symfony Request and Response
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getName()
106
    {
107
        return 'httplug';
108
    }
109
}
110