| Total Complexity | 6 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class AnAvailableUrlExistsForTheDomain implements Rule |
||
| 10 | { |
||
| 11 | protected $errorMessage = null; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Create a new rule instance. |
||
| 15 | * |
||
| 16 | * @return void |
||
| 17 | */ |
||
| 18 | public function __construct() |
||
| 19 | { |
||
| 20 | // |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Determine if the validation rule passes. |
||
| 25 | * |
||
| 26 | * @param string $attribute |
||
| 27 | * @param mixed $value |
||
| 28 | * |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | public function passes($attribute, $value) |
||
| 32 | { |
||
| 33 | |||
| 34 | // TODO: Inject mocked Guzzle\Client to getDomainURL to test these responses here. |
||
| 35 | $result = Domain::getDomainURL($value); |
||
| 36 | // Domain is available with http:// or https:// |
||
| 37 | if (is_string($result)) { |
||
| 38 | return true; |
||
| 39 | } |
||
| 40 | |||
| 41 | // Domain is only available with or without www. |
||
| 42 | elseif (isset($result) && $result->isNotEmpty()) { |
||
| 43 | Log::info('Domain not available: '.$value); |
||
| 44 | Log::info('Alternative domain is available: '.$result['alternativeAvailable']); |
||
| 45 | |||
| 46 | $this->errorMessage = $result->get('notAvailable').' is not available. Did you mean '.$result->get('alternativeAvailable').'?'; |
||
| 47 | |||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | Log::info('Domain not available: '.$value); |
||
| 52 | |||
| 53 | $this->errorMessage = $value.' is not available.'; |
||
| 54 | |||
| 55 | return false; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the validation error message. |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function message() |
||
| 66 | } |
||
| 67 | } |
||
| 68 |