Passed
Push — master ( 009d48...0bee53 )
by Nicolaas
09:28
created

ExternalURLFinder::find()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
nc 4
nop 4
dl 0
loc 24
rs 9.6333
c 1
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\ExternalURLField;
4
5
use SilverStripe\ORM\FieldType\DBField;
6
7
class ExternalURLFinder
8
{
9
    /**
10
     * finds an object with a matching URL.
11
     * it assumes that links are stored as lower case URLs.
12
     * without the final slash!
13
     *
14
     * @param string $link
15
     * @param string $className
16
     * @param string|null $field
17
     *
18
     * @return DataObject|null
0 ignored issues
show
Bug introduced by
The type Sunnysideup\ExternalURLField\DataObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     */
20
    public static function find(string $link, string $className, string $field, ?bool $removeFinalSlash = true)
21
    {
22
        $link = strtolower($link);
23
        if($removeFinalSlash) {
24
            $link = rtrim($link, '/');
25
        }
26
        $linkObject = DBField::create_field('ExternalURL', $link);
27
        $domain = $linkObject->Domain();
28
        if(strpos($domain, 'www.') === 0) {
29
            $domainWWW = $domain;
30
            $domainNoWWW = $linkObject->Domain()->noWWW();
31
        } else {
32
            $domainWWW = 'www' . $domain;
33
            $domainNoWWW = $domain;
34
        }
35
        $items = [
36
            $domainWWW,
37
            $domainNoWWW,
38
            'https://' . $domainWWW,
39
            'https://' . $domainNoWWW,
40
            'http://' . $domainWWW,
41
            'http://' . $domainNoWWW,
42
        ];
43
        return $className::get()->filter([$field => $items])->first();
44
    }
45
}
46