@@ -7,121 +7,121 @@ |
||
| 7 | 7 | |
| 8 | 8 | class ChuckAPIService |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * If user has not its own parameters set, value will be 'Chuck'. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - private $firstName; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * If user has not its own parameters set, value will be 'Norris'. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - private $lastName; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * After n seconds, cancel. |
|
| 26 | - * |
|
| 27 | - * @var int |
|
| 28 | - */ |
|
| 29 | - private $timeout; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @param string $firstName |
|
| 33 | - * @param string $lastName |
|
| 34 | - * @param int $timeout |
|
| 35 | - */ |
|
| 36 | - public function __construct($firstName = 'Chuck', $lastName = 'Norris', $timeout = 2) |
|
| 37 | - { |
|
| 38 | - $this |
|
| 39 | - ->setFirstName($firstName) |
|
| 40 | - ->setLastName($lastName) |
|
| 41 | - ->setTimeout($timeout) |
|
| 42 | - ; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param string $firstName |
|
| 47 | - * |
|
| 48 | - * @return $this |
|
| 49 | - */ |
|
| 50 | - public function setFirstName($firstName) |
|
| 51 | - { |
|
| 52 | - $this->firstName = $firstName; |
|
| 53 | - |
|
| 54 | - return $this; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $lastName |
|
| 59 | - * |
|
| 60 | - * @return $this |
|
| 61 | - */ |
|
| 62 | - public function setLastName($lastName) |
|
| 63 | - { |
|
| 64 | - $this->lastName = $lastName; |
|
| 65 | - |
|
| 66 | - return $this; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param int $timeout |
|
| 71 | - * |
|
| 72 | - * @return $this |
|
| 73 | - */ |
|
| 74 | - public function setTimeout($timeout) |
|
| 75 | - { |
|
| 76 | - $this->timeout = $timeout; |
|
| 77 | - |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get fact from Internet Chuck Norris Database. |
|
| 83 | - * |
|
| 84 | - * @return string|false |
|
| 85 | - */ |
|
| 86 | - public function getFact() |
|
| 87 | - { |
|
| 88 | - try { |
|
| 89 | - $response = $this->getResponse(); |
|
| 90 | - if ($response->getStatusCode() == 200) { |
|
| 91 | - $datas = json_decode($response->getBody()); |
|
| 92 | - |
|
| 93 | - return html_entity_decode(stripslashes($datas->value->joke)); |
|
| 94 | - } |
|
| 95 | - } catch (ConnectException $e) { |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @return \Psr\Http\Message\ResponseInterface |
|
| 102 | - */ |
|
| 103 | - private function getResponse() |
|
| 104 | - { |
|
| 105 | - $client = $this->getClient(); |
|
| 106 | - |
|
| 107 | - $response = $client->get('random', [ |
|
| 108 | - 'query' => [ |
|
| 109 | - 'firstName' => $this->firstName, |
|
| 110 | - 'lastName' => $this->lastName, |
|
| 111 | - ], |
|
| 112 | - ]); |
|
| 113 | - |
|
| 114 | - return $response; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return Client |
|
| 119 | - */ |
|
| 120 | - private function getClient() |
|
| 121 | - { |
|
| 122 | - return new Client([ |
|
| 123 | - 'base_uri' => 'http://api.icndb.com/jokes/', |
|
| 124 | - 'timeout' => $this->timeout, |
|
| 125 | - ]); |
|
| 126 | - } |
|
| 10 | + /** |
|
| 11 | + * If user has not its own parameters set, value will be 'Chuck'. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + private $firstName; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * If user has not its own parameters set, value will be 'Norris'. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + private $lastName; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * After n seconds, cancel. |
|
| 26 | + * |
|
| 27 | + * @var int |
|
| 28 | + */ |
|
| 29 | + private $timeout; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @param string $firstName |
|
| 33 | + * @param string $lastName |
|
| 34 | + * @param int $timeout |
|
| 35 | + */ |
|
| 36 | + public function __construct($firstName = 'Chuck', $lastName = 'Norris', $timeout = 2) |
|
| 37 | + { |
|
| 38 | + $this |
|
| 39 | + ->setFirstName($firstName) |
|
| 40 | + ->setLastName($lastName) |
|
| 41 | + ->setTimeout($timeout) |
|
| 42 | + ; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param string $firstName |
|
| 47 | + * |
|
| 48 | + * @return $this |
|
| 49 | + */ |
|
| 50 | + public function setFirstName($firstName) |
|
| 51 | + { |
|
| 52 | + $this->firstName = $firstName; |
|
| 53 | + |
|
| 54 | + return $this; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $lastName |
|
| 59 | + * |
|
| 60 | + * @return $this |
|
| 61 | + */ |
|
| 62 | + public function setLastName($lastName) |
|
| 63 | + { |
|
| 64 | + $this->lastName = $lastName; |
|
| 65 | + |
|
| 66 | + return $this; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param int $timeout |
|
| 71 | + * |
|
| 72 | + * @return $this |
|
| 73 | + */ |
|
| 74 | + public function setTimeout($timeout) |
|
| 75 | + { |
|
| 76 | + $this->timeout = $timeout; |
|
| 77 | + |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get fact from Internet Chuck Norris Database. |
|
| 83 | + * |
|
| 84 | + * @return string|false |
|
| 85 | + */ |
|
| 86 | + public function getFact() |
|
| 87 | + { |
|
| 88 | + try { |
|
| 89 | + $response = $this->getResponse(); |
|
| 90 | + if ($response->getStatusCode() == 200) { |
|
| 91 | + $datas = json_decode($response->getBody()); |
|
| 92 | + |
|
| 93 | + return html_entity_decode(stripslashes($datas->value->joke)); |
|
| 94 | + } |
|
| 95 | + } catch (ConnectException $e) { |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @return \Psr\Http\Message\ResponseInterface |
|
| 102 | + */ |
|
| 103 | + private function getResponse() |
|
| 104 | + { |
|
| 105 | + $client = $this->getClient(); |
|
| 106 | + |
|
| 107 | + $response = $client->get('random', [ |
|
| 108 | + 'query' => [ |
|
| 109 | + 'firstName' => $this->firstName, |
|
| 110 | + 'lastName' => $this->lastName, |
|
| 111 | + ], |
|
| 112 | + ]); |
|
| 113 | + |
|
| 114 | + return $response; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return Client |
|
| 119 | + */ |
|
| 120 | + private function getClient() |
|
| 121 | + { |
|
| 122 | + return new Client([ |
|
| 123 | + 'base_uri' => 'http://api.icndb.com/jokes/', |
|
| 124 | + 'timeout' => $this->timeout, |
|
| 125 | + ]); |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -13,44 +13,44 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ConsoleTerminateListener |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * @var ChuckAPIService |
|
| 18 | - */ |
|
| 19 | - private $chuckApiService; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - private $enabledEnvironments; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private $env; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * ConsoleTerminateListener constructor. |
|
| 33 | - * @param ChuckAPIService $chuckAPIService |
|
| 34 | - */ |
|
| 35 | - public function __construct(ChuckAPIService $chuckAPIService, $environments = [], $env) |
|
| 36 | - { |
|
| 37 | - $this->chuckApiService = $chuckAPIService; |
|
| 38 | - $this->enabledEnvironments = $environments; |
|
| 39 | - $this->env = $env; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param ConsoleTerminateEvent $event |
|
| 44 | - */ |
|
| 45 | - public function onConsoleTerminate(ConsoleTerminateEvent $event) |
|
| 46 | - { |
|
| 47 | - if ( |
|
| 48 | - !$event->getCommand() instanceof ChuckCommand |
|
| 49 | - && in_array($this->env, $this->enabledEnvironments) |
|
| 50 | - && $fact = $this->chuckApiService->getFact() |
|
| 51 | - ) { |
|
| 52 | - |
|
| 53 | - $event->getOutput()->writeln($fact); |
|
| 54 | - } |
|
| 55 | - } |
|
| 16 | + /** |
|
| 17 | + * @var ChuckAPIService |
|
| 18 | + */ |
|
| 19 | + private $chuckApiService; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + private $enabledEnvironments; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private $env; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * ConsoleTerminateListener constructor. |
|
| 33 | + * @param ChuckAPIService $chuckAPIService |
|
| 34 | + */ |
|
| 35 | + public function __construct(ChuckAPIService $chuckAPIService, $environments = [], $env) |
|
| 36 | + { |
|
| 37 | + $this->chuckApiService = $chuckAPIService; |
|
| 38 | + $this->enabledEnvironments = $environments; |
|
| 39 | + $this->env = $env; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param ConsoleTerminateEvent $event |
|
| 44 | + */ |
|
| 45 | + public function onConsoleTerminate(ConsoleTerminateEvent $event) |
|
| 46 | + { |
|
| 47 | + if ( |
|
| 48 | + !$event->getCommand() instanceof ChuckCommand |
|
| 49 | + && in_array($this->env, $this->enabledEnvironments) |
|
| 50 | + && $fact = $this->chuckApiService->getFact() |
|
| 51 | + ) { |
|
| 52 | + |
|
| 53 | + $event->getOutput()->writeln($fact); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | 56 | } |