DomainVisitor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A visit() 0 15 4
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