URLType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 16
c 1
b 1
f 0
dl 0
loc 48
ccs 15
cts 15
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getValue() 0 24 8
A isExternal() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items\DataTypes;
15
16
use Wszetko\Sitemap\Helpers\Url;
17
18
/**
19
 * Class URLType.
20
 *
21
 * @package Wszetko\Sitemap\Items\DataTypes
22
 */
23
class URLType extends StringType
24
{
25
    /**
26
     * Determine if URL CAN be external.
27
     *
28
     * @var bool
29
     */
30
    protected $external = false;
31
32
    /**
33
     * @inheritDoc
34
     *
35
     * @throws \InvalidArgumentException
36
     */
37 104
    public function getValue()
38
    {
39 104
        if (null === $this->value || !is_string($this->value)) {
40 18
            return null;
41
        }
42
43 90
        if ($this->isExternal() && false !== Url::normalizeUrl($this->value)) {
44 8
            $value = $this->value;
45
        } else {
46 86
            $domain = $this->getDomain() ?? '';
47 86
            $value = str_replace($domain, '', $this->value);
48
49 86
            if ('' !== $value) {
50 86
                $value = $domain . '/' . ltrim($value, '/');
51
            }
52
        }
53
54 90
        $attributes = $this->getAttributes();
55
56 90
        if ('' !== $value && [] !== $attributes) {
57 14
            return [$value => $attributes];
58
        }
59
60 82
        return $value;
61
    }
62
63
    /**
64
     * Return if URL can be external.
65
     *
66
     * @return bool
67
     */
68 90
    public function isExternal(): bool
69
    {
70 90
        return $this->external;
71
    }
72
}
73