Created
February 18, 2016 14:47
-
-
Save useless-stuff/2d69d6994d5626347e9c to your computer and use it in GitHub Desktop.
PHP - InfiniteIterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Use InfititeIterator for traversing a circular data structure | |
| // https://en.wikipedia.org/wiki/Circular_buffer | |
| /** | |
| * Interface AvailabilityService | |
| */ | |
| interface AvailabilityService | |
| { | |
| /** | |
| * @param Service $site | |
| * @return mixed | |
| */ | |
| public function checkAvailability(\Service $site); | |
| } | |
| /** | |
| * Interface IHttpAdapter | |
| */ | |
| interface IHttpAdapter | |
| { | |
| /** | |
| * @param $url | |
| * @return mixed | |
| */ | |
| public function get($url); | |
| /** | |
| * @param $url | |
| * @param $data | |
| * @return mixed | |
| */ | |
| public function post($url, $data); | |
| /** | |
| * @param $url | |
| * @param $data | |
| * @return mixed | |
| */ | |
| public function put($url, $data); | |
| /** | |
| * @param $url | |
| * @return mixed | |
| */ | |
| public function delete($url); | |
| /** | |
| * @return mixed | |
| */ | |
| public function getCallInfo(); | |
| } | |
| /** | |
| * Class HttpAdapterCurl | |
| */ | |
| class HttpAdapterCurl implements IHttpAdapter | |
| { | |
| protected $info; | |
| /** | |
| * @param $url | |
| * @return mixed | |
| */ | |
| public function get($url) | |
| { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $this->response = curl_exec($ch); | |
| $this->info = curl_getinfo($ch); | |
| curl_close($ch); | |
| return $this->getResponse(); | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| protected function getResponse() | |
| { | |
| return $this->response; | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| protected function getInfo() | |
| { | |
| return $this->info; | |
| } | |
| public function post($url, $data) | |
| { | |
| // TODO: Implement post() method. | |
| } | |
| public function put($url, $data) | |
| { | |
| // TODO: Implement put() method. | |
| } | |
| public function delete($url) | |
| { | |
| // TODO: Implement delete() method. | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| public function getCallInfo() | |
| { | |
| return $this->getInfo(); | |
| } | |
| } | |
| /** | |
| * Class HttpClient | |
| */ | |
| abstract class HttpClient | |
| { | |
| protected $adapter; | |
| /** | |
| * HttpClient constructor. | |
| * @param IHttpAdapter $adapter | |
| */ | |
| public function __construct(\IHttpAdapter $adapter) | |
| { | |
| $this->adapter = $adapter; | |
| } | |
| /** | |
| * @param $url | |
| * @return mixed | |
| */ | |
| protected function get($url) | |
| { | |
| /** @var */ | |
| return $this->adapter->get($url); | |
| } | |
| } | |
| /** | |
| * Class SiteCheckerException | |
| */ | |
| class SiteCheckerException extends \Exception | |
| { | |
| protected $message = 'The site was down'; | |
| } | |
| /** | |
| * Class SiteChecker | |
| */ | |
| class ServiceMonitor extends HttpClient implements AvailabilityService | |
| { | |
| public function checkAvailability(\Service $site) | |
| { | |
| $this->get($site->getUrl()); | |
| if ($this->adapter->getCallInfo()['http_code'] === 200) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| } | |
| /** | |
| * Class Site | |
| */ | |
| class Service | |
| { | |
| protected $url; | |
| public function __construct($url) | |
| { | |
| $this->url = $url; | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| public function getUrl() | |
| { | |
| return $this->url; | |
| } | |
| /** | |
| * @param mixed $url | |
| */ | |
| public function setUrl($url) | |
| { | |
| $this->url = $url; | |
| } | |
| } | |
| $servicesCollection = new ArrayIterator(); | |
| $servicesCollection->append(new Service('http://diego.anniballo.com/testwrong.php')); | |
| $servicesCollection->append(new Service('http://diego.anniballo.com/testwrong.php')); | |
| $servicesCollection->append(new Service('http://diego.anniballo.com')); | |
| $serviceMonitor = new ServiceMonitor(new HttpAdapterCurl()); | |
| $servicesLoop = new InfiniteIterator($servicesCollection); | |
| foreach ($servicesLoop as $service) { | |
| /** @var Service $service */ | |
| if ($serviceMonitor->checkAvailability($service)) { | |
| echo 'Service '.$service->getUrl().'is ready'; | |
| break; | |
| } | |
| echo 'Service '.$service->getUrl().'is not ready yet'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment