Completed
Pull Request — master (#154)
by
unknown
04:41 queued 52s
created

AnAvailableUrlExistsForTheDomain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 3 1
A __construct() 0 2 1
A passes() 0 25 4
1
<?php
2
3
namespace App\Rules;
4
5
use App\Domain;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Support\Facades\Log;
8
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()
64
    {
65
        return $this->errorMessage;
66
    }
67
}
68