DomainVisitor::visit()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 5
nop 2
1
<?php
2
3
namespace BenTools\HostnameExtractor\Visitor;
4
5
use BenTools\HostnameExtractor\ParsedHostname;
6
use function BenTools\Violin\string;
7
8
/**
9
 * Class DomainVisitor
10
 * @internal
11
 */
12
final class DomainVisitor implements HostnameVisitorInterface
13
{
14
15
    /**
16
     * @inheritDoc
17
     */
18
    public function visit($hostname, ParsedHostname $parsedHostname): void
19
    {
20
        $hostname = string($hostname);
21
        if (!$parsedHostname->isIp()) {
22
            $suffix = $parsedHostname->getSuffix();
23
            $withoutSuffix = null !== $suffix ? $hostname->removeRight(\sprintf('.%s', $suffix)) : $hostname;
24
            if ($withoutSuffix->contains('.')) {
25
                $domainParts = \explode('.', (string) $withoutSuffix);
26
                $parsedHostname->setDomain(\array_pop($domainParts));
27
                $parsedHostname->setSubdomain(\implode('.', $domainParts));
28
            } else {
29
                $parsedHostname->setDomain((string) $withoutSuffix);
30
            }
31
        }
32
    }
33
}
34