@@ -9,26 +9,26 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ChuckCommand extends ContainerAwareCommand |
| 11 | 11 | { |
| 12 | - protected function configure() |
|
| 13 | - { |
|
| 14 | - $this |
|
| 15 | - ->setName('chuck:fact') |
|
| 16 | - ->setDescription('A Chuck Norris Fact everyday keeps the doctor away') |
|
| 17 | - ->addArgument('firstName', InputArgument::OPTIONAL, 'Replace Chuck by this string', 'Chuck') |
|
| 18 | - ->addArgument('lastName', InputArgument::OPTIONAL, 'Replace Norris by this string', 'Norris') |
|
| 19 | - ; |
|
| 20 | - } |
|
| 12 | + protected function configure() |
|
| 13 | + { |
|
| 14 | + $this |
|
| 15 | + ->setName('chuck:fact') |
|
| 16 | + ->setDescription('A Chuck Norris Fact everyday keeps the doctor away') |
|
| 17 | + ->addArgument('firstName', InputArgument::OPTIONAL, 'Replace Chuck by this string', 'Chuck') |
|
| 18 | + ->addArgument('lastName', InputArgument::OPTIONAL, 'Replace Norris by this string', 'Norris') |
|
| 19 | + ; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
| 23 | - { |
|
| 24 | - $fact = $this->getContainer()->get('kk_labs_chuck_console.fact_service') |
|
| 25 | - ->setFirstName($input->getArgument('firstName')) |
|
| 26 | - ->setLastName($input->getArgument('lastName')) |
|
| 27 | - ->getFact() |
|
| 28 | - ; |
|
| 22 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
| 23 | + { |
|
| 24 | + $fact = $this->getContainer()->get('kk_labs_chuck_console.fact_service') |
|
| 25 | + ->setFirstName($input->getArgument('firstName')) |
|
| 26 | + ->setLastName($input->getArgument('lastName')) |
|
| 27 | + ->getFact() |
|
| 28 | + ; |
|
| 29 | 29 | |
| 30 | - if ($fact) { |
|
| 31 | - $output->writeln($fact); |
|
| 32 | - } |
|
| 33 | - } |
|
| 30 | + if ($fact) { |
|
| 31 | + $output->writeln($fact); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -8,68 +8,68 @@ |
||
| 8 | 8 | |
| 9 | 9 | class ChuckFactServiceTest extends \PHPUnit_Framework_TestCase |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var ContainerInterface |
|
| 13 | - */ |
|
| 14 | - private $container; |
|
| 11 | + /** |
|
| 12 | + * @var ContainerInterface |
|
| 13 | + */ |
|
| 14 | + private $container; |
|
| 15 | 15 | |
| 16 | - protected function setUp() |
|
| 17 | - { |
|
| 18 | - $kernel = new AppKernel('test', true); |
|
| 19 | - $kernel->boot(); |
|
| 16 | + protected function setUp() |
|
| 17 | + { |
|
| 18 | + $kernel = new AppKernel('test', true); |
|
| 19 | + $kernel->boot(); |
|
| 20 | 20 | |
| 21 | - $this->randomFirstName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 22 | - $this->randomLastName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 21 | + $this->randomFirstName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 22 | + $this->randomLastName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 23 | 23 | |
| 24 | - $this->container = $kernel->getContainer(); |
|
| 25 | - } |
|
| 24 | + $this->container = $kernel->getContainer(); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - public function testServiceIsDefinedInContainer() |
|
| 28 | - { |
|
| 29 | - $service = $this->container->get('kk_labs_chuck_console.fact_service'); |
|
| 27 | + public function testServiceIsDefinedInContainer() |
|
| 28 | + { |
|
| 29 | + $service = $this->container->get('kk_labs_chuck_console.fact_service'); |
|
| 30 | 30 | |
| 31 | - $this->assertInstanceOf('KK\Labs\ChuckConsoleBundle\ChuckFactService\ChuckAPIService', $service); |
|
| 32 | - } |
|
| 31 | + $this->assertInstanceOf('KK\Labs\ChuckConsoleBundle\ChuckFactService\ChuckAPIService', $service); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function testGetCustomFact() |
|
| 35 | - { |
|
| 36 | - $this->assertRegExp( |
|
| 37 | - "/$this->randomFirstName|$this->randomLastName/", |
|
| 38 | - $this->getFacts($this->randomFirstName, $this->randomLastName) |
|
| 39 | - ); |
|
| 40 | - } |
|
| 34 | + public function testGetCustomFact() |
|
| 35 | + { |
|
| 36 | + $this->assertRegExp( |
|
| 37 | + "/$this->randomFirstName|$this->randomLastName/", |
|
| 38 | + $this->getFacts($this->randomFirstName, $this->randomLastName) |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function testGetChuckFact() |
|
| 43 | - { |
|
| 44 | - $this->assertRegExp('/Chuck|Norris/', $this->getFacts()); |
|
| 45 | - } |
|
| 42 | + public function testGetChuckFact() |
|
| 43 | + { |
|
| 44 | + $this->assertRegExp('/Chuck|Norris/', $this->getFacts()); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param string $firstName |
|
| 49 | - * @param string $lastName |
|
| 50 | - * |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - private function getFacts($firstName = 'Chuck', $lastName = 'Norris') |
|
| 54 | - { |
|
| 55 | - $chuckService = $this->getChuckService($firstName, $lastName); |
|
| 47 | + /** |
|
| 48 | + * @param string $firstName |
|
| 49 | + * @param string $lastName |
|
| 50 | + * |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + private function getFacts($firstName = 'Chuck', $lastName = 'Norris') |
|
| 54 | + { |
|
| 55 | + $chuckService = $this->getChuckService($firstName, $lastName); |
|
| 56 | 56 | |
| 57 | - $facts = ''; |
|
| 58 | - for ($i = 0; $i < 5; ++$i) { |
|
| 59 | - $facts .= $chuckService->getFact(); |
|
| 60 | - } |
|
| 57 | + $facts = ''; |
|
| 58 | + for ($i = 0; $i < 5; ++$i) { |
|
| 59 | + $facts .= $chuckService->getFact(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return $facts; |
|
| 63 | - } |
|
| 62 | + return $facts; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param string $firstName |
|
| 67 | - * @param string $lastName |
|
| 68 | - * |
|
| 69 | - * @return ChuckAPIService |
|
| 70 | - */ |
|
| 71 | - private function getChuckService($firstName = 'Chuck', $lastName = 'Norris') |
|
| 72 | - { |
|
| 73 | - return new ChuckAPIService($firstName, $lastName, 5); |
|
| 74 | - } |
|
| 65 | + /** |
|
| 66 | + * @param string $firstName |
|
| 67 | + * @param string $lastName |
|
| 68 | + * |
|
| 69 | + * @return ChuckAPIService |
|
| 70 | + */ |
|
| 71 | + private function getChuckService($firstName = 'Chuck', $lastName = 'Norris') |
|
| 72 | + { |
|
| 73 | + return new ChuckAPIService($firstName, $lastName, 5); |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -9,35 +9,35 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ChuckCommandTest extends \PHPUnit_Framework_TestCase |
| 11 | 11 | { |
| 12 | - public function testExecute() |
|
| 13 | - { |
|
| 14 | - $firstName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 15 | - $lastName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 16 | - |
|
| 17 | - $application = new Application(); |
|
| 18 | - $application->add(new ChuckCommand()); |
|
| 19 | - |
|
| 20 | - $command = $application->find('chuck:fact'); |
|
| 21 | - $command->setContainer($this->getMockContainer()); |
|
| 22 | - $commandTester = new CommandTester($command); |
|
| 23 | - $commandTester->execute(array( |
|
| 24 | - 'command' => $command->getName(), |
|
| 25 | - 'firstName' => $firstName, |
|
| 26 | - 'lastName' => $lastName, |
|
| 27 | - )); |
|
| 28 | - |
|
| 29 | - $this->assertRegExp("/$firstName|$lastName/", $commandTester->getDisplay()); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - protected function getMockContainer() |
|
| 33 | - { |
|
| 34 | - $mockContainer = $this->getMock('Symfony\Component\DependencyInjection\Container'); |
|
| 35 | - $mockContainer |
|
| 36 | - ->expects($this->once()) |
|
| 37 | - ->method('get') |
|
| 38 | - ->with('kk_labs_chuck_console.fact_service') |
|
| 39 | - ->willReturn(new ChuckAPIService()); |
|
| 40 | - |
|
| 41 | - return $mockContainer; |
|
| 42 | - } |
|
| 12 | + public function testExecute() |
|
| 13 | + { |
|
| 14 | + $firstName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 15 | + $lastName = substr(str_shuffle(MD5(microtime())), 0, 10); |
|
| 16 | + |
|
| 17 | + $application = new Application(); |
|
| 18 | + $application->add(new ChuckCommand()); |
|
| 19 | + |
|
| 20 | + $command = $application->find('chuck:fact'); |
|
| 21 | + $command->setContainer($this->getMockContainer()); |
|
| 22 | + $commandTester = new CommandTester($command); |
|
| 23 | + $commandTester->execute(array( |
|
| 24 | + 'command' => $command->getName(), |
|
| 25 | + 'firstName' => $firstName, |
|
| 26 | + 'lastName' => $lastName, |
|
| 27 | + )); |
|
| 28 | + |
|
| 29 | + $this->assertRegExp("/$firstName|$lastName/", $commandTester->getDisplay()); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + protected function getMockContainer() |
|
| 33 | + { |
|
| 34 | + $mockContainer = $this->getMock('Symfony\Component\DependencyInjection\Container'); |
|
| 35 | + $mockContainer |
|
| 36 | + ->expects($this->once()) |
|
| 37 | + ->method('get') |
|
| 38 | + ->with('kk_labs_chuck_console.fact_service') |
|
| 39 | + ->willReturn(new ChuckAPIService()); |
|
| 40 | + |
|
| 41 | + return $mockContainer; |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -7,41 +7,41 @@ |
||
| 7 | 7 | |
| 8 | 8 | class AppKernel extends Kernel |
| 9 | 9 | { |
| 10 | - public function registerBundles() |
|
| 11 | - { |
|
| 12 | - return array( |
|
| 13 | - new \KK\Labs\ChuckConsoleBundle\KKLabsChuckConsoleBundle(), |
|
| 14 | - ); |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - public function registerContainerConfiguration(LoaderInterface $loader) |
|
| 18 | - { |
|
| 19 | - $loader->load(__DIR__.'/config/config_test.yml'); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @return string |
|
| 24 | - */ |
|
| 25 | - public function getCacheDir() |
|
| 26 | - { |
|
| 27 | - $cacheDir = sys_get_temp_dir().'/cache'; |
|
| 28 | - if (!is_dir($cacheDir)) { |
|
| 29 | - mkdir($cacheDir, 0777, true); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - return $cacheDir; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - public function getLogDir() |
|
| 39 | - { |
|
| 40 | - $logDir = sys_get_temp_dir().'/logs'; |
|
| 41 | - if (!is_dir($logDir)) { |
|
| 42 | - mkdir($logDir, 0777, true); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return $logDir; |
|
| 46 | - } |
|
| 10 | + public function registerBundles() |
|
| 11 | + { |
|
| 12 | + return array( |
|
| 13 | + new \KK\Labs\ChuckConsoleBundle\KKLabsChuckConsoleBundle(), |
|
| 14 | + ); |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + public function registerContainerConfiguration(LoaderInterface $loader) |
|
| 18 | + { |
|
| 19 | + $loader->load(__DIR__.'/config/config_test.yml'); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @return string |
|
| 24 | + */ |
|
| 25 | + public function getCacheDir() |
|
| 26 | + { |
|
| 27 | + $cacheDir = sys_get_temp_dir().'/cache'; |
|
| 28 | + if (!is_dir($cacheDir)) { |
|
| 29 | + mkdir($cacheDir, 0777, true); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + return $cacheDir; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + public function getLogDir() |
|
| 39 | + { |
|
| 40 | + $logDir = sys_get_temp_dir().'/logs'; |
|
| 41 | + if (!is_dir($logDir)) { |
|
| 42 | + mkdir($logDir, 0777, true); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return $logDir; |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) { |
| 4 | - throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?'); |
|
| 4 | + throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?'); |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | require $autoloadFile; |
@@ -14,20 +14,20 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class KKLabsChuckConsoleExtension extends Extension |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function load(array $configs, ContainerBuilder $container) |
|
| 21 | - { |
|
| 22 | - $configuration = new Configuration(); |
|
| 23 | - $config = $this->processConfiguration($configuration, $configs); |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function load(array $configs, ContainerBuilder $container) |
|
| 21 | + { |
|
| 22 | + $configuration = new Configuration(); |
|
| 23 | + $config = $this->processConfiguration($configuration, $configs); |
|
| 24 | 24 | |
| 25 | - $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 26 | - $loader->load('services.xml'); |
|
| 25 | + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 26 | + $loader->load('services.xml'); |
|
| 27 | 27 | |
| 28 | - $container->setParameter('chuck.who.first_name', $config['who']['first_name']); |
|
| 29 | - $container->setParameter('chuck.who.last_name', $config['who']['last_name']); |
|
| 30 | - $container->setParameter('chuck.timeout', $config['timeout']); |
|
| 31 | - $container->setParameter('chuck.enabled_environments', $config['environments']); |
|
| 32 | - } |
|
| 28 | + $container->setParameter('chuck.who.first_name', $config['who']['first_name']); |
|
| 29 | + $container->setParameter('chuck.who.last_name', $config['who']['last_name']); |
|
| 30 | + $container->setParameter('chuck.timeout', $config['timeout']); |
|
| 31 | + $container->setParameter('chuck.enabled_environments', $config['environments']); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -12,31 +12,31 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Configuration implements ConfigurationInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - public function getConfigTreeBuilder() |
|
| 19 | - { |
|
| 20 | - $treeBuilder = new TreeBuilder(); |
|
| 21 | - $rootNode = $treeBuilder->root('kk_labs_chuck_console'); |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + public function getConfigTreeBuilder() |
|
| 19 | + { |
|
| 20 | + $treeBuilder = new TreeBuilder(); |
|
| 21 | + $rootNode = $treeBuilder->root('kk_labs_chuck_console'); |
|
| 22 | 22 | |
| 23 | - $rootNode |
|
| 24 | - ->children() |
|
| 25 | - ->arrayNode('who') |
|
| 26 | - ->addDefaultsIfNotSet() |
|
| 27 | - ->children() |
|
| 28 | - ->scalarNode('first_name')->defaultValue('Chuck')->end() |
|
| 29 | - ->scalarNode('last_name')->defaultValue('Norris')->end() |
|
| 30 | - ->end() |
|
| 31 | - ->end()//who |
|
| 32 | - ->scalarNode('timeout') |
|
| 33 | - ->defaultValue(10) |
|
| 34 | - ->end() |
|
| 35 | - ->arrayNode('environments') |
|
| 36 | - ->prototype('scalar')->end() |
|
| 37 | - ->defaultValue(['dev']) |
|
| 38 | - ->end(); |
|
| 23 | + $rootNode |
|
| 24 | + ->children() |
|
| 25 | + ->arrayNode('who') |
|
| 26 | + ->addDefaultsIfNotSet() |
|
| 27 | + ->children() |
|
| 28 | + ->scalarNode('first_name')->defaultValue('Chuck')->end() |
|
| 29 | + ->scalarNode('last_name')->defaultValue('Norris')->end() |
|
| 30 | + ->end() |
|
| 31 | + ->end()//who |
|
| 32 | + ->scalarNode('timeout') |
|
| 33 | + ->defaultValue(10) |
|
| 34 | + ->end() |
|
| 35 | + ->arrayNode('environments') |
|
| 36 | + ->prototype('scalar')->end() |
|
| 37 | + ->defaultValue(['dev']) |
|
| 38 | + ->end(); |
|
| 39 | 39 | |
| 40 | - return $treeBuilder; |
|
| 41 | - } |
|
| 40 | + return $treeBuilder; |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -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 | } |