1 | <?php |
||
2 | /** |
||
3 | * User: ofields |
||
4 | * Date: 11/8/13 |
||
5 | * Time: 11:26 AM |
||
6 | */ |
||
7 | |||
8 | namespace elevate\util; |
||
9 | |||
10 | use elevate\HVObjects\Generic\CodableValue; |
||
11 | use elevate\HVObjects\Generic\Date\DateTime; |
||
12 | use elevate\HVObjects\Generic\Date\Time; |
||
13 | use elevate\HVObjects\MethodObjects\Get\Info; |
||
14 | use elevate\Serializer\XmlObjectDeserializationVisitor; |
||
15 | use JMS\Serializer\Naming\CamelCaseNamingStrategy; |
||
16 | use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; |
||
17 | use JMS\Serializer\EventDispatcher\EventDispatcher; |
||
18 | use JMS\Serializer\SerializerBuilder; |
||
19 | use elevate\HVObjects\Generic\Date\Date; |
||
20 | use elevate\HVObjects\MethodObjects\PersonInfo\PersonInfo; |
||
21 | use Mentis\BaseBundle\Utils\HealthVault\ResponseGroupParser; |
||
22 | use Mentis\BaseBundle\Services\MentisHVClient2; |
||
23 | use Mentis\BaseBundle\Services\UserManagerHelper; |
||
24 | |||
25 | use JMS\Serializer\Handler\HandlerRegistry; |
||
26 | |||
27 | /** |
||
28 | * Class HVClientHelper |
||
29 | * @package elevate\util |
||
30 | * |
||
31 | * Utility class to help parse, create objects for HealthVault calls |
||
32 | */ |
||
33 | class HVClientHelper { |
||
34 | |||
35 | /** |
||
36 | * An instance of the Health Vault Client. This is used to assist with the Health |
||
37 | * Vault calls (create item, put, get, etc.) |
||
38 | * |
||
39 | * @var MentisHVClient2 |
||
40 | */ |
||
41 | private $hv; |
||
42 | |||
43 | /** |
||
44 | * @var UserManagerHelper |
||
45 | */ |
||
46 | private $umh; |
||
47 | |||
48 | function __construct($hv, $umh) |
||
0 ignored issues
–
show
|
|||
49 | { |
||
50 | $this->hv = $hv; |
||
51 | $this->umh = $umh; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param object $info |
||
56 | * @return string |
||
57 | */ |
||
58 | static function HVInfoAsXML($info) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
HVInfoAsXML .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
59 | { |
||
60 | $preBuiltserializer = SerializerBuilder::create(); |
||
61 | $preBuiltserializer->configureListeners( |
||
62 | function(EventDispatcher $dispatcher) |
||
63 | { |
||
64 | $dispatcher->addSubscriber(new EventSubscriber()); |
||
65 | } |
||
66 | ); |
||
67 | $serializer = $preBuiltserializer->build(); |
||
68 | $xml = $serializer->serialize($info, 'xml'); |
||
69 | return $xml; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param $rawResponse |
||
74 | * Helper function to pre-deserialize and build |
||
75 | * @return array |
||
76 | */ |
||
77 | private static function HVDeserializeBuilder($rawResponse) |
||
78 | { |
||
79 | $xml = simplexml_load_string( $rawResponse ); |
||
80 | |||
81 | // Get the namespace for wc from the document and register for XPath |
||
82 | // There are two values coming back based on version of GetThings used |
||
83 | // Used for images -urn:com.microsoft.wc.methods.response.GetThings |
||
84 | // Used for rest - urn:com.microsoft.wc.methods.response.GetThings3 |
||
85 | $namespace = $xml->getDocNamespaces(true); |
||
86 | foreach($namespace as $key=>$value) |
||
87 | { |
||
88 | $xml->registerXPathNamespace($key, $value); |
||
89 | } |
||
90 | |||
91 | $serializer = SerializerBuilder::create(); |
||
92 | $serializer->configureListeners( |
||
93 | function(EventDispatcher $dispatcher) |
||
94 | { |
||
95 | $dispatcher->addSubscriber(new EventSubscriber()); |
||
96 | } |
||
97 | ); |
||
98 | $serializer->setDeserializationVisitor("xmlobject", new XmlObjectDeserializationVisitor(new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy())) ); |
||
99 | $serializerBuilder = $serializer->build(); |
||
100 | |||
101 | |||
102 | $serializer->configureHandlers(function(HandlerRegistry $registry) { |
||
103 | $registry->registerHandler('deserialization', 'rawxml', 'xmlobject', |
||
104 | function($visitor, $obj, array $type) { |
||
105 | return $obj->asXML(); |
||
106 | } |
||
107 | ); |
||
108 | $registry->registerHandler('deserialization', 'SimpleXMLElement', 'xmlobject', |
||
109 | function($visitor, $obj, array $type) { |
||
110 | return $obj; |
||
111 | } |
||
112 | ); |
||
113 | }) |
||
114 | ; |
||
115 | |||
116 | return array( |
||
117 | 'xml' => $xml, |
||
118 | 'serializerBuilder' => $serializerBuilder |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param $rawResponse |
||
124 | * Function that parses an HV response for Personinfo into a Personinfo object |
||
125 | * @return null|PersonInfo |
||
126 | */ |
||
127 | static function HVPersonFromXML ($rawResponse) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
HVPersonFromXML .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
128 | { |
||
129 | $info = HVClientHelper::HVDeserializeBuilder($rawResponse); |
||
0 ignored issues
–
show
As per coding style,
self should be used for accessing local static members.
This check looks for accesses to local static members using the fully qualified name instead
of <?php
class Certificate {
const TRIPLEDES_CBC = 'ASDFGHJKL';
private $key;
public function __construct()
{
$this->key = Certificate::TRIPLEDES_CBC;
}
}
While this is perfectly valid, the fully qualified name of ![]() |
|||
130 | |||
131 | $xmlObjects = $info['xml']->xpath('/response/wc:info/person-info'); |
||
132 | |||
133 | $response = NULL; |
||
134 | foreach ($xmlObjects as $xmlObject) |
||
135 | { |
||
136 | $response = $info['serializerBuilder']->deserialize( |
||
137 | $xmlObject, 'elevate\HVObjects\MethodObjects\PersonInfo\PersonInfo', 'xmlobject' |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | return $response; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param $rawResponse |
||
146 | * Function that parses an HV response for Things into an array of Thing objects |
||
147 | * @return array |
||
148 | */ |
||
149 | static function HVGroupsFromXML( $rawResponse ) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
HVGroupsFromXML .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
150 | { |
||
151 | $info = HVClientHelper::HVDeserializeBuilder($rawResponse); |
||
0 ignored issues
–
show
As per coding style,
self should be used for accessing local static members.
This check looks for accesses to local static members using the fully qualified name instead
of <?php
class Certificate {
const TRIPLEDES_CBC = 'ASDFGHJKL';
private $key;
public function __construct()
{
$this->key = Certificate::TRIPLEDES_CBC;
}
}
While this is perfectly valid, the fully qualified name of ![]() |
|||
152 | |||
153 | $groupXMLObjects = $info['xml']->xpath('/response/wc:info/group'); |
||
154 | $responseGroups = array(); |
||
155 | // $responseGroups = $serializerBuilder->deserialize($groupXMLObjects, 'array<elevate\\HVObjects\\MethodObjects\\ResponseGroup>', 'xmlobject'); |
||
156 | foreach ($groupXMLObjects as $group) |
||
157 | { |
||
158 | $responseGroups[] = $info['serializerBuilder']->deserialize($group, 'elevate\HVObjects\MethodObjects\ResponseGroup', 'xmlobject'); |
||
159 | } |
||
160 | |||
161 | return $responseGroups; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param $xmlResponse |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | static function parsePutThingsResponse($xmlResponse) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
parsePutThingsResponse .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
170 | { |
||
171 | $xml = simplexml_load_string( $xmlResponse ); |
||
172 | $xml->registerXPathNamespace('wc', 'urn:com.microsoft.wc.methods.response.PutThings2'); |
||
173 | $thingIds = $xml->xpath('//thing-id'); |
||
174 | $response = []; |
||
175 | |||
176 | $serializer = \JMS\Serializer\SerializerBuilder::create(); |
||
177 | $serializer->configureListeners( |
||
178 | function(EventDispatcher $dispatcher) |
||
179 | { |
||
180 | $dispatcher->addSubscriber(new EventSubscriber()); |
||
181 | } |
||
182 | ); |
||
183 | $serializer->setDeserializationVisitor("xmlobject", new XmlObjectDeserializationVisitor(new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy())) ); |
||
184 | $serializerBuilder = $serializer->build(); |
||
185 | |||
186 | foreach ($thingIds as $thing) |
||
187 | { |
||
188 | $response[] = $serializerBuilder->deserialize($thing, 'elevate\HVObjects\Generic\ThingId', 'xmlobject'); |
||
189 | } |
||
190 | |||
191 | return $response; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Takes PHP DateTime object and converts to Healthvault DateTime object |
||
196 | * @param $dt |
||
197 | * |
||
198 | * @return DateTime |
||
199 | */ |
||
200 | public static function convertPhpDateTimeToHvDateTime( $dt ) |
||
201 | { |
||
202 | $date = new DateTime( |
||
203 | self::convertPhpDateTimeToHvDate($dt), |
||
204 | self::convertPhpDateTimeToHvTime($dt), |
||
205 | new CodableValue( |
||
206 | $dt->getTimezone()->getName() |
||
207 | ) |
||
208 | ); |
||
209 | return $date; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Takes PHP DateTime object and converts to Healthvault Date object |
||
214 | * @param $dt |
||
215 | * |
||
216 | * @return Date |
||
217 | */ |
||
218 | public static function convertPhpDateTimeToHvDate( $dt ) |
||
219 | { |
||
220 | return new Date( |
||
221 | $dt->format('Y'), |
||
222 | $dt->format('m'), |
||
223 | $dt->format('d') |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Takes PHP DateTime object and converts to HealthVault Time object |
||
229 | * @param $dt |
||
230 | * |
||
231 | * @return DateTime |
||
232 | */ |
||
233 | public static function convertPhpDateTimeToHvTime(\DateTime $dt ) |
||
234 | { |
||
235 | return new Time( |
||
236 | $dt->format('H'), |
||
237 | $dt->format('i'), |
||
238 | $dt->format('s'), |
||
239 | $dt->format('u') |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Make a GET request to HV given the request groups and the user ID |
||
245 | * |
||
246 | * The response will be a nicely parsed array. |
||
247 | * |
||
248 | * @param $uid - Mentis Specific User ID |
||
249 | * @param $groups - HV Request Groups |
||
250 | * @param $version - version of GET request |
||
251 | * |
||
252 | * @return array - HV Things in a nicely formatted array |
||
253 | */ |
||
254 | public function makeHVGETRequest($uid, $groups, $version = 3) |
||
255 | { |
||
256 | $info = new Info($groups); |
||
257 | |||
258 | $response = $this->makeGenericHVRequest($uid, $info, 'GetThings', $version); |
||
259 | |||
260 | $responseGroups = HVClientHelper::HVGroupsFromXML($response); |
||
0 ignored issues
–
show
As per coding style,
self should be used for accessing local static members.
This check looks for accesses to local static members using the fully qualified name instead
of <?php
class Certificate {
const TRIPLEDES_CBC = 'ASDFGHJKL';
private $key;
public function __construct()
{
$this->key = Certificate::TRIPLEDES_CBC;
}
}
While this is perfectly valid, the fully qualified name of ![]() |
|||
261 | |||
262 | return ResponseGroupParser::parseResponseGroups($responseGroups); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * A generic HV request call. |
||
267 | * |
||
268 | * This call will set the user tokens and make an HV request based on the |
||
269 | * method, the version, and info object. |
||
270 | * |
||
271 | * @param $uid - Mentis specific user id |
||
272 | * @param $info - The Info request to send to healthvault |
||
273 | * @param $method - The type of request to make |
||
274 | * @param $version - The version of the request to make |
||
275 | * |
||
276 | * @return mixed |
||
277 | */ |
||
278 | public function makeGenericHVRequest($uid, $info, $method, $version) |
||
279 | { |
||
280 | $user = $this->umh->loadUserFromUserID($uid); |
||
281 | $this->hv->setRecordId($user->getHvRecordId()); |
||
282 | $this->hv->setPersonId($user->getHvPersonId()); |
||
283 | |||
284 | $response = $this->hv->callHealthVault($info, $method, $version); |
||
285 | return $response; |
||
286 | } |
||
287 | } |
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.
If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with
private
, and only raise it toprotected
if a sub-class needs to have access, orpublic
if an external class needs access.