🏗️ Allow access to Config from Collectors
This commit is contained in:
		| @@ -14,6 +14,14 @@ abstract class Collector | ||||
|         $this->pool = $pool; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return \Shikiryu\WebGobbler\Config | ||||
|      */ | ||||
|     public function getConfig(): Config | ||||
|     { | ||||
|         return $this->pool->getConfig(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return string | ||||
|      */ | ||||
|   | ||||
| @@ -22,13 +22,13 @@ class YahooImageCollector extends Collector | ||||
|      */ | ||||
|     public function getRandomImage() | ||||
|     { | ||||
|         $word_to_search = $this->generateRandomWord(); | ||||
|         $word_to_search = $this->getConfig()->get('collector.keywords.keywords', $this->generateRandomWord()); | ||||
|  | ||||
|         $client = new Client(); | ||||
|         $crawler = $client->request('GET', sprintf(self::SEARCH_URL, $word_to_search)); | ||||
|         $imgs = $crawler->filter('noscript img'); | ||||
|         if ($imgs->count() < 20) { | ||||
|             return $this->getRandomImage(); | ||||
|             return $this->getRandomImage(); // FIXME possible infinite loop if keywords is given | ||||
|         } | ||||
|  | ||||
|         $img_url = $imgs->eq(random_int(0, $imgs->count() - 1))->attr('src'); | ||||
|   | ||||
							
								
								
									
										27
									
								
								src/Pool.php
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								src/Pool.php
									
									
									
									
									
								
							| @@ -21,16 +21,23 @@ class Pool | ||||
|      */ | ||||
|     protected $nb_images = 0; | ||||
|  | ||||
|     /** | ||||
|      * @var \Shikiryu\WebGobbler\Config | ||||
|      */ | ||||
|     protected $config; | ||||
|  | ||||
|     /** | ||||
|      * Pool constructor. | ||||
|      * | ||||
|      * @param array $config | ||||
|      */ | ||||
|     public function __construct(array $config) | ||||
|     public function __construct(Config $config) | ||||
|     { | ||||
|         $this->config = $config; | ||||
|         $pool_config = $config->get('pool'); | ||||
|         $collectors = []; | ||||
|         if (array_key_exists('collectors', $config)) { | ||||
|             $collectors = $config['collectors']; | ||||
|         if (array_key_exists('collectors', $pool_config)) { | ||||
|             $collectors = $pool_config['collectors']; | ||||
|         } | ||||
|  | ||||
|         $all_collectors = Collector::getAllCollectors($this); | ||||
| @@ -44,18 +51,25 @@ class Pool | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         $pool_dir = $config['directory']; | ||||
|         $pool_dir = $pool_config['directory']; | ||||
|         if (!is_dir($pool_dir) && !mkdir($pool_dir) && !is_dir($pool_dir)) { | ||||
|             throw new \RuntimeException(sprintf('Directory "%s" was not created', $pool_dir)); | ||||
|         } | ||||
|  | ||||
|         $this->pool_directory = $pool_dir; | ||||
|  | ||||
|         $this->nb_images = $config['nb_images']; // FIXME | ||||
|         $this->nb_images = $pool_config['nb_images']; // FIXME | ||||
|  | ||||
|         $this->prepareCollectors(); | ||||
|     } | ||||
|  | ||||
|     public function getConfig() | ||||
|     { | ||||
|         return $this->config; | ||||
|     } | ||||
|     /** | ||||
|      * @return string | ||||
|      */ | ||||
|     public function getPoolDirectory() | ||||
|     { | ||||
|         return $this->pool_directory; | ||||
| @@ -100,6 +114,9 @@ class Pool | ||||
|     { | ||||
|         foreach ($this->collectors as $collector) { | ||||
|             $directory = $collector->getPoolDirectory(); | ||||
|             if (!is_dir($directory) && !mkdir($directory) && !is_dir($directory)) { | ||||
|                 throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory)); | ||||
|             } | ||||
|             $images = glob($directory.'/*.{jpg,gif,png}', GLOB_BRACE); | ||||
|             if (count($images) < $this->nb_images) { | ||||
|                 $collector->getRandomImages($this->nb_images - count($images)); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user