ToResolveToExpectation::run()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 12
nc 8
nop 2
crap 6
1
<?php
2
3
namespace Overwatch\ServiceBundle\Expectation;
4
5
use Overwatch\ExpectationBundle\Exception as Result;
6
use Overwatch\ExpectationBundle\Expectation\ExpectationInterface;
7
8
/**
9
 * ToResolveToExpectation
10
 * Expectation classes are the actual runners of tests.
11
 * This is the runner for the "toResolveTo" expectation.
12
 */
13
class ToResolveToExpectation implements ExpectationInterface
14
{
15
    private $config;
16
    
17 29
    public function __construct($config)
18
    {
19 29
        $this->config = $config;
20 29
    }
21
    
22
    /**
23
     * @param string $actual
24
     * @param string|null $expected
25
     */
26 2
    public function run($actual, $expected = null)
27
    {
28 2
        $dnsRecords = dns_get_record($actual);
29 2
        $found = '';
30
        
31 2
        foreach ($dnsRecords as $dnsRecord) {
32 2
            if (!in_array($dnsRecord['type'], $this->config['record_types'])) {
33 2
                continue;
34
            }
35
            
36 2
            foreach (['mname', 'txt', 'target', 'ipv6', 'ip'] as $destination) {
37 2
                if (array_key_exists($destination, $dnsRecord)) {
38 2
                    $found = $dnsRecord[$destination];
39 2
                }
40 2
            }
41
            
42 2
            if ($found === $expected) {
43 1
                return $actual . ' has a ' . $dnsRecord['type'] . ' record that resolves to ' . $found;
44
            }
45 1
        }
46
        
47 1
        throw new Result\ExpectationFailedException("Expected $actual to resolve to $expected, actually resolves to $found");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $actual instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $expected instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $found instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
48
    }
49
}
50