1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains GuzzleNetworkRetriever class. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.5 |
6
|
|
|
* |
7
|
|
|
* LICENSE: |
8
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
9
|
|
|
* which can be used to access the Eve Online API data and place it into a |
10
|
|
|
* database. |
11
|
|
|
* Copyright (C) 2014-2016 Michael Cummings |
12
|
|
|
* |
13
|
|
|
* This program is free software: you can redistribute it and/or modify it |
14
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
15
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
16
|
|
|
* option) any later version. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
19
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
21
|
|
|
* for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
24
|
|
|
* along with this program. If not, see |
25
|
|
|
* <http://www.gnu.org/licenses/>. |
26
|
|
|
* |
27
|
|
|
* You should be able to find a copy of this license in the LICENSE.md file. A |
28
|
|
|
* copy of the GNU GPL should also be available in the GNU-GPL.md file. |
29
|
|
|
* |
30
|
|
|
* @copyright 2014-2016 Michael Cummings |
31
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL |
32
|
|
|
* @author Michael Cummings <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
namespace Yapeal\Network; |
35
|
|
|
|
36
|
|
|
use GuzzleHttp\Client; |
37
|
|
|
use GuzzleHttp\Exception\RequestException; |
38
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
39
|
|
|
use Yapeal\Event\EveApiEventInterface; |
40
|
|
|
use Yapeal\Event\EveApiRetrieverInterface; |
41
|
|
|
use Yapeal\Event\MediatorInterface; |
42
|
|
|
use Yapeal\Log\Logger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class GuzzleNetworkRetriever |
46
|
|
|
* |
47
|
|
|
* @author Stephen Gulick <[email protected]> |
48
|
|
|
* @author Michael Cummings <[email protected]> |
49
|
|
|
*/ |
50
|
|
|
class GuzzleNetworkRetriever implements EveApiRetrieverInterface |
51
|
|
|
{ |
52
|
|
|
use EveApiEventEmitterTrait; |
53
|
|
|
/** |
54
|
|
|
* @param Client $client |
55
|
|
|
* @param bool $preserve |
56
|
|
|
*/ |
57
|
|
|
public function __construct(Client $client, $preserve = true) |
58
|
|
|
{ |
59
|
|
|
$this->setClient($client) |
60
|
|
|
->setRetrieve($preserve); |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* @param EveApiEventInterface $event |
64
|
|
|
* @param string $eventName |
65
|
|
|
* @param MediatorInterface $yem |
66
|
|
|
* |
67
|
|
|
* @return \Yapeal\Event\EveApiEventInterface |
68
|
|
|
* @throws \DomainException |
69
|
|
|
* @throws \InvalidArgumentException |
70
|
|
|
* @throws \LogicException |
71
|
|
|
*/ |
72
|
|
|
public function retrieveEveApi(EveApiEventInterface $event, $eventName, MediatorInterface $yem) |
73
|
|
|
{ |
74
|
|
|
if (!$this->shouldRetrieve()) { |
75
|
|
|
return $event; |
76
|
|
|
} |
77
|
|
|
$data = $event->getData(); |
78
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
79
|
|
|
Logger::DEBUG, |
80
|
|
|
$this->getReceivedEventMessage($data, $eventName, __CLASS__)); |
81
|
|
|
$uri = sprintf('/%1$s/%2$s.xml.aspx', strtolower($data->getEveApiSectionName()), $data->getEveApiName()); |
82
|
|
|
try { |
83
|
|
|
$response = $this->getClient() |
84
|
|
|
->post($uri, ['form_params' => $data->getEveApiArguments()]); |
85
|
|
|
} catch (RequestException $exc) { |
86
|
|
|
$messagePrefix = 'Could NOT retrieve XML data during'; |
87
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
88
|
|
|
Logger::DEBUG, |
89
|
|
|
$this->createEventMessage($messagePrefix, $data, $eventName), |
90
|
|
|
['exception' => $exc]); |
91
|
|
|
return $event; |
92
|
|
|
} |
93
|
|
|
$body = (string)$response->getBody(); |
94
|
|
|
if ('' === $body) { |
95
|
|
|
$messagePrefix = 'Received empty body during'; |
96
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
97
|
|
|
Logger::NOTICE, |
98
|
|
|
$this->createEventMessage($messagePrefix, $data, $eventName)); |
99
|
|
|
} |
100
|
|
|
$data->setEveApiXml($body); |
101
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->getFinishedEventMessage($data, $eventName)); |
102
|
|
|
return $event->setData($data) |
103
|
|
|
->setHandledSufficiently(); |
104
|
|
|
} |
105
|
|
|
/** |
106
|
|
|
* @param Client $value |
107
|
|
|
* |
108
|
|
|
* @return $this Fluent interface. |
109
|
|
|
*/ |
110
|
|
|
public function setClient(Client $value) |
111
|
|
|
{ |
112
|
|
|
$this->client = $value; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
/** |
116
|
|
|
* Turn on or off retrieving of Eve API data by this retriever. |
117
|
|
|
* |
118
|
|
|
* Allows class to stay registered for events but be enabled or disabled during runtime. |
119
|
|
|
* |
120
|
|
|
* @param boolean $value |
121
|
|
|
* |
122
|
|
|
* @return $this Fluent interface |
123
|
|
|
*/ |
124
|
|
|
public function setRetrieve($value = true) |
125
|
|
|
{ |
126
|
|
|
$this->retrieve = (boolean)$value; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
/** |
130
|
|
|
* @return Client |
131
|
|
|
* @throws \LogicException |
132
|
|
|
*/ |
133
|
|
|
protected function getClient() |
134
|
|
|
{ |
135
|
|
|
if (null === $this->client) { |
136
|
|
|
$mess = 'Tried to use client before it was set'; |
137
|
|
|
throw new \LogicException($mess); |
138
|
|
|
} |
139
|
|
|
return $this->client; |
140
|
|
|
} |
141
|
|
|
/** |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
|
|
private function shouldRetrieve() |
145
|
|
|
{ |
146
|
|
|
return $this->retrieve; |
147
|
|
|
} |
148
|
|
|
/** |
149
|
|
|
* @var Client $client |
150
|
|
|
*/ |
151
|
|
|
private $client; |
152
|
|
|
/** |
153
|
|
|
* @var bool $retrieve |
154
|
|
|
*/ |
155
|
|
|
private $retrieve = true; |
156
|
|
|
} |
157
|
|
|
|