| Total Complexity | 7 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 7 | class TwitApps |
||
| 8 | { |
||
| 9 | /** @var Collection */ |
||
| 10 | private $pool; |
||
| 11 | |||
| 12 | /** @var TwitApp */ |
||
| 13 | private $current; |
||
| 14 | |||
| 15 | public function __construct($apps) |
||
| 16 | { |
||
| 17 | $this->pool = collect($apps)->map(function ($config, $key) { |
||
| 18 | return new TwitApp($key, $config); |
||
| 19 | }); |
||
| 20 | } |
||
| 21 | |||
| 22 | /** @return TwitApp */ |
||
| 23 | public function getAvailable() |
||
| 24 | { |
||
| 25 | $this->current = $this->pool->first(function (TwitApp $app) { |
||
| 26 | return $app->available(); |
||
| 27 | }); |
||
| 28 | |||
| 29 | if (! $this->current) { |
||
| 30 | // We will return the first one to get the rate limits so we can sleep |
||
| 31 | // and wait for reset |
||
| 32 | $this->current = $this->pool->get(0); |
||
| 33 | } |
||
| 34 | |||
| 35 | return $this->current; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function waitTime() |
||
| 39 | { |
||
| 40 | $waitTime = $this->pool->map(function (TwitApp $app) { |
||
| 41 | if ($app->rateLimits) { |
||
| 42 | return $app->rateLimits->waitTime(); |
||
| 43 | } |
||
| 44 | })->filter()->min(); |
||
| 45 | |||
| 46 | return $waitTime ?: 0; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function resetRateLimits() |
||
| 53 | }); |
||
| 54 | } |
||
| 55 | } |
||
| 56 |