src/Controller/SystemController.php line 26

Open in your IDE?
  1. <?php namespace App\Controller;
  2. use AdScore\Common\Definition\Signature;
  3. use Symfony\Component\HttpFoundation\{RequestResponseJsonResponse};
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use App\Component\{NotificationSiv};
  6. use AdScore\Traffic\Utils\GeoIp;
  7. use GuzzleHttp\{ClientRequestOptions};
  8. use Psr\Log\LoggerInterface;
  9. /**
  10.  * Environment and resources entrypoint
  11.  * @TODO: Conforms to 2019.11 ASAPI standards
  12.  */
  13. class SystemController extends Controller {
  14.     
  15.     const CURRENCY_OVERRIDES = [
  16.         'USD' => ['priority' => 3'name' => 'United states dollar'],
  17.         'EUR' => ['priority' => 2],
  18.         'GBP' => ['priority' => 1]
  19.     ];
  20.     
  21.     /**
  22.      * @Route("/", name="default")
  23.      */
  24.     public function defaultAction() {
  25.         return new JsonResponse(
  26.             ['status' => 'ok',]
  27.         );
  28.     }
  29.     
  30.     /**
  31.      * @Route("/list/environment", name="list_environment", methods="GET")
  32.      */
  33.     public function listEnvironmentAction() {
  34.         $geoData GeoIp::examine();
  35.         if (empty($geoData['geo.country']) && isset($_SERVER['HTTP_CF_IPCOUNTRY']))
  36.             $geoData['geo.country'] = $_SERVER['HTTP_CF_IPCOUNTRY'];
  37.         return new JsonResponse(
  38.             ['status' => 'ok''country' => $geoData['geo.country'], 'time_zone' => $geoData['geo.timezone']]
  39.         );
  40.     }
  41.     
  42.     /**
  43.      * @Route("/list/currency", name="list_currency", methods="GET")
  44.      */
  45.     public function listCurrencyAction(LoggerInterface $logger) {
  46.         $client = new Client();
  47.         $response $client->get('https://restcountries.com/v3.1/all');
  48.         $data json_decode($response->getBody(), true);
  49.         $output = [];
  50.         $overrides self::CURRENCY_OVERRIDES;
  51.         foreach ($data as $country) {
  52.             foreach (($country['currencies'] ?? []) as $currencyCode => $currency) {
  53.                 $currency['code'] = $currencyCode;
  54.                 if ( (!isset($currency['code']) || !isset($currency['name'])) || ($currency['code'] == '(none)') || ($currency['name'] == '(none)') || ($currency['name'][0] == '[') ) {
  55.                     continue;
  56.                 }
  57.                 if (!isset($currency['symbol']) || ($currency['symbol'] == '(none)')) {
  58.                     $currency['symbol'] = null;
  59.                 }
  60.                 if (isset($output[$currency['code']])) {
  61.                     if (!in_array($country['cca2'], $output[$currency['code']]['countries'])) {
  62.                         $output[$currency['code']]['countries'][] = $country['cca2'];
  63.                     }
  64.                 } else {
  65.                     $output[$currency['code']] = [
  66.                         'code' => $currency['code'], 
  67.                         'name' => mb_convert_case($overrides[$currency['code']]['name'] ?? $currency['name'], MB_CASE_TITLE'UTF-8'), 
  68.                         'symbol' => $currency['symbol'] ?? null
  69.                         'countries' => [$country['cca2']], 
  70.                         'priority' => $overrides[$currency['code']]['priority'] ?? 0
  71.                     ];
  72.                 }
  73.             }
  74.         }
  75.         $output array_values($output);
  76.         usort($output, function($a$b) {
  77.             if ($a['priority'] !== $b['priority']) {
  78.                 return ($b['priority'] - $a['priority']);
  79.             } else {
  80.                 return strcmp($a['code'], $b['code']);            
  81.             }
  82.         });
  83.         foreach ($output as &$item) {
  84.             unset($item['countries']);
  85.             unset($item['priority']);
  86.         }
  87.         return new JsonResponse(
  88.             ['status' => 'ok''currency' => $output]
  89.         );
  90.     }    
  91.     /**
  92.      * @Route("/list/time_zone", name="list_time_zone", methods="GET")
  93.      */
  94.     public function listTimeZoneAction() {
  95.         return new JsonResponse(
  96.             ['status' => 'ok''time_zone' => timezone_identifiers_list()]
  97.         );
  98.     }
  99.     
  100.      /**
  101.      * @Route("/support/message", name="support_message", methods="POST")
  102.      */
  103.     public function supportMessageAction(Request $request) {
  104.         $errors = [];
  105.         $data $request->getContent();
  106.         if (Siv::jsonParseStrict($data, ['email''name''subject''content'], [], $errors)) {
  107.             Siv::email($data['email'], 'email'$errors);
  108.             Siv::notEmpty($data['name'], 'name'$errors);
  109.             Siv::notEmpty($data['subject'], 'subject'$errors);
  110.             Siv::notEmpty($data['content'], 'content'$errors);
  111.         }
  112.         if (!empty($errors))
  113.             return new JsonResponse(['status' => 'error''errors' => $errors], JsonResponse::HTTP_BAD_REQUEST);
  114.         Notification::sendSingle($data['subject'], $data['content'], [['name' => 'Support Form''email' => 'support@adscore.com']], ['name' => $data['name'], 'email' => $data['email']]);
  115.         return new JsonResponse(['status' => 'ok''message' => $data], JsonResponse::HTTP_OK);
  116.     }
  117.     /**
  118.      * @Route("/list/zone_fields", name="zone_list_fields", methods="GET")
  119.      */
  120.     public function listZoneFieldsAction() {
  121.         return $this->jsonOkResponse(['fields' => Signature::getFieldsRef()]);
  122.     }
  123. }