Guest User

Untitled

a guest
Sep 14th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. class EwalletApiClientService implements EwalletApiClientServiceContract
  2. {
  3.     / @var \GuzzleHttp\Client */
  4.     private $http;
  5.     private $logger;
  6.  
  7.     public function __construct()
  8.     {
  9.         $messageFormats = [
  10.             'REQUEST: ',
  11.             'METHOD: {method}',
  12.             'URL: {uri}',
  13.             'HTTP/{version}',
  14.             'HEADERS: {req_headers}',
  15.             'Payload: {req_body}',
  16.             'RESPONSE: ',
  17.             'STATUS: {code}',
  18.             'BODY: {res_body}',
  19.             "\n"
  20.         ];
  21.  
  22.         $stack = $this->setLoggingHandler($messageFormats);
  23.  
  24.         $this->http = new Client(array_merge(config("ewalletapi.GuzzleClientConfig"), ['handler' => $stack]));
  25.     }
  26.  
  27.     / @return \GuzzleHttp\Client */
  28.     public function getHttpClient()
  29.     {
  30.         return $this->http;
  31.     }
  32.  
  33.     private function get_logger()
  34.     {
  35.         if (! $this->logger) {
  36.             $this->logger = with(new Logger('guzzle-log'))->pushHandler(
  37.                 new RotatingFileHandler(storage_path('logs/ewallet-api-log.log'))
  38.             );
  39.         }
  40.  
  41.         return $this->logger;
  42.     }
  43.     /
  44.      * Setup Middleware
  45.      */
  46.     private function setGuzzleMiddleware(string $messageFormat)
  47.     {
  48.         return Middleware::log(
  49.             $this->get_logger(),
  50.             new MessageFormatter($messageFormat)
  51.         );
  52.     }
  53.     /
  54.      * Setup Logging Handler Stack
  55.      */
  56.     private function setLoggingHandler(array $messageFormats)
  57.     {
  58.         $stack = HandlerStack::create();
  59.  
  60.         collect($messageFormats)->each(function ($messageFormat) use ($stack) {
  61.             // We'll use unshift instead of push, to add the middleware to the bottom of the stack, not the top
  62.             $stack->unshift(
  63.                 $this->setGuzzleMiddleware($messageFormat)
  64.             );
  65.         });
  66.  
  67.         return $stack;
  68.     }
  69. }
Add Comment
Please, Sign In to add comment