RequestManager::send()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 4
crap 12
1
<?php
2
3
namespace Happyr\TranslationBundle\Http;
4
5
use Happyr\TranslationBundle\Exception\HttpException;
6
use Happyr\TranslationBundle\Translation\FilesystemUpdater;
7
use Http\Client\Exception\TransferException;
8
use Http\Client\HttpClient;
9
use Http\Client\Plugin\ErrorPlugin;
10
use Http\Client\Plugin\LoggerPlugin;
11
use Http\Client\Plugin\PluginClient;
12
use Http\Discovery\HttpClientDiscovery;
13
use Http\Discovery\MessageFactoryDiscovery;
14
use Http\Message\MessageFactory;
15
use Psr\Log\LoggerInterface;
16
17
class RequestManager
18
{
19
    /**
20
     * @var HttpClient
21
     */
22
    private $client;
23
24
    /**
25
     * @var MessageFactory
26
     */
27
    private $messageFactory;
28
29
    /**
30
     *
31
     * @param HttpClient $client
32
     * @param MessageFactory $messageFactory
33
     */
34 1
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
35
    {
36 1
        $this->client = new PluginClient($client, [new ErrorPlugin()]);
0 ignored issues
show
Deprecated Code introduced by
The class Http\Client\Plugin\ErrorPlugin has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\ErrorPlugin} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The class Http\Client\Plugin\PluginClient has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\PluginClient} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
37 1
        $this->messageFactory = $messageFactory;
38 1
    }
39
40
    /**
41
     * @param array $data
42
     *
43
     * @return mixed
44
     */
45
    public function downloadFiles(FilesystemUpdater $filesystem, array $data)
46
    {
47
        $factory = $this->getMessageFactory();
48
        $client = $this->getClient();
49
50
        foreach ($data as $url => $fileName) {
51
            $response = $client->sendRequest($factory->createRequest('GET', $url));
52
            $filesystem->writeToFile($fileName, $response->getBody()->__toString());
53
        }
54
    }
55
56
    /**
57
     * @param string $method
58
     * @param string $url
59
     * @param array  $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
     *
61
     * @return array
62
     *
63
     * @throws HttpException
64
     */
65
    public function send($method, $url, $body = null, $headers = array())
66
    {
67
        $request = $this->getMessageFactory()->createRequest($method, $url, $headers, $body);
68
69
        try {
70
            $response = $this->getClient()->sendRequest($request);
71
        } catch (TransferException $e) {
72
            $message = 'Error sending request. ';
73
            if ($e instanceof \Http\Client\Exception\HttpException) {
74
                $message .= (string) $e->getResponse()->getBody();
75
            }
76
77
            throw new HttpException($message, $e->getCode(), $e);
78
        }
79
80
        // TODO add more error checks
81
        return json_decode($response->getBody()->__toString(), true);
82
    }
83
84
    /**
85
     * @return HttpClient
86
     */
87
    public function getClient()
88
    {
89
        return $this->client;
90
    }
91
92
    /**
93
     *
94
     * @return \Http\Message\MessageFactory
95
     */
96
    private function getMessageFactory()
97
    {
98
        return $this->messageFactory;
99
    }
100
}
101