|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ant\RedirectChecker; |
| 4 | + |
| 5 | +use Nette\Neon\Neon; |
| 6 | + |
| 7 | +class Checker |
| 8 | +{ |
| 9 | + |
| 10 | + const PASSED = 'ok'; |
| 11 | + const WARNING = 'warning'; |
| 12 | + const FAILED = 'failed'; |
| 13 | + |
| 14 | + private $configFile; |
| 15 | + |
| 16 | + public function __construct($configFile = '') |
| 17 | + { |
| 18 | + $this->configFile = __DIR__ . '/../../' . $configFile; |
| 19 | + } |
| 20 | + |
| 21 | + public function run() |
| 22 | + { |
| 23 | + return $this->checkListFromNeon($this->configFile); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Will check list from associative array of rules. |
| 28 | + * Index => origin url, Value => needed url |
| 29 | + * |
| 30 | + * @param [] $list |
| 31 | + * |
| 32 | + * @return array [OVERALL STATUS, [[SINGLE STATUS, [additional single info]]]] |
| 33 | + */ |
| 34 | + public function checkList($list) |
| 35 | + { |
| 36 | + $out = self::PASSED; |
| 37 | + $responses = []; |
| 38 | + foreach ($list as $originUrl => $finalUrl) { |
| 39 | + list($result, $response) = $this->check($originUrl, $finalUrl); |
| 40 | + switch ($result) { |
| 41 | + case self::WARNING: |
| 42 | + if ($out == self::PASSED) { |
| 43 | + $out = $result; |
| 44 | + } |
| 45 | + break; |
| 46 | + case self::FAILED: |
| 47 | + if ($out !== self::FAILED) { |
| 48 | + $out = $result; |
| 49 | + } |
| 50 | + break; |
| 51 | + } |
| 52 | + $responses[] = [$result, $response]; |
| 53 | + } |
| 54 | + return [$out, $responses]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Checks single url |
| 59 | + * |
| 60 | + * @param string $originUrl Full url with http:// |
| 61 | + * @param string $finalUrl Full url with http:// |
| 62 | + * @param integer $maxFollowRedirects |
| 63 | + * |
| 64 | + * @return array [STATUS, [additional info for output]] |
| 65 | + */ |
| 66 | + public function check($originUrl, $finalUrl, $maxFollowRedirects = 10) |
| 67 | + { |
| 68 | + $out = self::FAILED; |
| 69 | + $client = \EasyRequest::create($originUrl, 'GET', ['follow_redirects' => $maxFollowRedirects]); |
| 70 | + $client->send(); |
| 71 | + $outList = $list = $client->getRedirectedUrls(); |
| 72 | + |
| 73 | + if (empty($list)) { |
| 74 | + if ($originUrl == $finalUrl) { |
| 75 | + $out = self::PASSED; |
| 76 | + } |
| 77 | + } elseif (in_array($finalUrl, $list)) { |
| 78 | + if (array_pop($list) == $finalUrl) { |
| 79 | + $out = self::PASSED; |
| 80 | + } else { |
| 81 | + $out = self::WARNING; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + array_shift($outList); |
| 86 | + return [$out, ['from' => $originUrl, 'to' => $finalUrl, 'hops' => $outList, 'hopsCount' => count($outList)]]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Will check list of rules from a neon file. |
| 91 | + * |
| 92 | + * @param string $filefilename |
| 93 | + * |
| 94 | + * @return array |
| 95 | + * @throws \Exception |
| 96 | + */ |
| 97 | + public function checkListFromNeon($filefilename) |
| 98 | + { |
| 99 | + if (!file_exists($filefilename)) { |
| 100 | + throw new \Exception("File '$filefilename' not found."); |
| 101 | + } |
| 102 | + $configuration = Neon::decode(file_get_contents($filefilename)); |
| 103 | + if (!isset($configuration['redirects']) || empty($configuration['redirects'])) { |
| 104 | + throw new \Exception("Section 'redirects' not found or empty in configuration file: '{$filefilename}'"); |
| 105 | + } |
| 106 | + return $this->checkList($configuration['redirects']); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments