Completed
Pull Request — master (#154)
by Marcel
08:11 queued 04:04
created

AnAvailableUrlExistsForTheDomain::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Rules;
4
5
use App\Domain;
6
use Illuminate\Contracts\Validation\Rule;
7
8
class AnAvailableUrlExistsForTheDomain implements Rule
9
{
10
    protected $errorMessage = null;
11
12
    /**
13
     * Create a new rule instance.
14
     *
15
     * @return void
16
     */
17
    public function __construct()
18
    {
19
        //
20
    }
21
22
    /**
23
     * Determine if the validation rule passes.
24
     *
25
     * @param string $attribute
26
     * @param mixed  $value
27
     *
28
     * @return bool
29
     */
30
    public function passes($attribute, $value)
31
    {
32
33
        // TODO: Inject mocked Guzzle\Client to getDomainURL to test these responses here.
34
        $result = Domain::getDomainURL($value);
35
        // Domain is available with http:// or https://
36
        if (is_string($result)) {
37
            return true;
38
        }
39
40
        // Domain is only available with or without www.
41
        elseif (isset($result) && $result->isNotEmpty()) {
42
            $this->errorMessage = $result->get('notAvailable').' is not available. Did you mean '.$result->get('alternativeAvailable').'?';
43
44
            return false;
45
        }
46
47
        $this->errorMessage = $value.' is not available.';
48
49
        return false;
50
    }
51
52
    /**
53
     * Get the validation error message.
54
     *
55
     * @return string
56
     */
57
    public function message()
58
    {
59
        return $this->errorMessage;
60
    }
61
}
62