WebSocket app
This commit is contained in:
76
ws/common/Listener/Currencies.php
Normal file
76
ws/common/Listener/Currencies.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Listener;
|
||||
|
||||
use ProVM\Common\Define\Event\Request;
|
||||
use ProVM\Common\Define\Event\Response;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Money\Currency;
|
||||
|
||||
class Currencies {
|
||||
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currencies = $factory->find(Currency::class)->many();
|
||||
array_walk($currencies, function(&$item) {
|
||||
$item = $item->asArray();
|
||||
});
|
||||
$response->getBody()->write(compact('currencies'));
|
||||
return $response;
|
||||
}
|
||||
public function get(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currency_id = $request->getBody()->read()['currency_id'];
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$response->getBody()->write(['currency' => $currency->asArray()]);
|
||||
return $response;
|
||||
}
|
||||
public function latest(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currency_id = $request->getBody()->read()['currency_id'];
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'currency' => null,
|
||||
'value' => null
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->latest()) {
|
||||
$output['value'] = $currency->latest()->asArray();
|
||||
}
|
||||
}
|
||||
$response->getBody()->write($output);
|
||||
return $response;
|
||||
}
|
||||
public function getSources(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currency_id = $request->getBody()->read()['currency_id'];
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'currency' => null,
|
||||
'sources' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->sources()) {
|
||||
$output['sources'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->sources());
|
||||
}
|
||||
}
|
||||
$response->getBody()->write($output);
|
||||
return $response;
|
||||
}
|
||||
public function getValues(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currency_id = $request->getBody()->read()['currency_id'];
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'currency' => null,
|
||||
'values' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->values()) {
|
||||
$output['values'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->values());
|
||||
}
|
||||
}
|
||||
$response->getBody()->write($output);
|
||||
return $response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user