Passed
Push — master ( b03c4d...7cc0ae )
by Andreas
05:12
created

GISSchemaManager::translateDistanceQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smindel\GIS\ORM;
4
5
use SilverStripe\ORM\DB;
6
use Smindel\GIS\ORM\FieldType\DBGeometry;
7
use Smindel\GIS\GIS;
8
9
trait GISSchemaManager
10
{
11
    // Ellipsoidal spatial data type.
12 3
    public function geography($values)
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

12
    public function geography(/** @scrutinizer ignore-unused */ $values)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
    {
14 3
        return 'geography';
15
    }
16
17
    // Planar spatial data type.
18
    public function geometry($values)
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    public function geometry(/** @scrutinizer ignore-unused */ $values)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        return 'geometry';
21
    }
22 4
    public function translateStGenericFilter($field, $value, $inclusive, $hint)
23
    {
24 4
        $null = $inclusive ? '' : ' OR ' . DB::get_conn()->nullCheckClause($field, true);
25 4
        $fragment = sprintf(
26 4
            '%sST_%s(%s, ST_GeomFromText(?, ?))%s',
27 4
            $inclusive ? '' : 'NOT ',
28 4
            $hint,
29 4
            $field,
30 4
            $null
31
        );
32 4
        return [$fragment => [GIS::create($value)->wkt, GIS::create($value)->srid]];
33
    }
34
35 1
    public function translateStContainsFilter($field, $value, $inclusive, $hint = false)
36
    {
37 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
38
    }
39
40 1
    public function translateStCrossesFilter($field, $value, $inclusive, $hint = false)
41
    {
42 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
43
    }
44
45 1
    public function translateStDisjointFilter($field, $value, $inclusive, $hint = false)
46
    {
47 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
48
    }
49
50 2
    public function translateStDistanceFilter($field, $value, $inclusive)
51
    {
52 2
        $null = $inclusive ? '' : ' OR ' . DB::get_conn()->nullCheckClause($field, true);
53 2
        $fragment = sprintf('ST_Distance(ST_GeomFromText(?, ?),%s) %s ?%s', $field, $inclusive ? '<=' : '> ', $null);
54 2
        $geo = GIS::create($value[0]);
55 2
        $distance = $value[1];
56 2
        return [$fragment => [$geo->wkt, $geo->srid, $distance]];
57
    }
58
59 1
    public function translateStEqualsFilter($field, $value, $inclusive, $hint = false)
60
    {
61 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
62
    }
63
64 1
    public function translateStGeometryTypeFilter($field, $value, $inclusive)
65
    {
66 1
        $null = $inclusive ? '' : ' OR ' . DB::get_conn()->nullCheckClause($field, true);
67 1
        $fragment = sprintf(
68 1
            '%sLOWER(ST_GeometryType(%s)) = ?%s',
69 1
            $inclusive ? '' : 'NOT ',
70 1
            $field,
71 1
            $null
72
        );
73 1
        return [$fragment => 'st_' . strtolower($value)];
74
    }
75 4
    public function translateStIntersectsFilter($field, $value, $inclusive, $hint = false)
76
    {
77 4
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
78
    }
79
80 1
    public function translateStOverlapsFilter($field, $value, $inclusive, $hint = false)
81
    {
82 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
83
    }
84
85 1
    public function translateStTouchesFilter($field, $value, $inclusive, $hint = false)
86
    {
87 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
88
    }
89
90 1
    public function translateStWithinFilter($field, $value, $inclusive, $hint = false)
91
    {
92 1
        return $this->translateStGenericFilter($field, $value, $inclusive, $hint);
93
    }
94
95 1
    public function translateDistanceQuery($geo1, $geo2)
96
    {
97 1
        $geo1 = GIS::create($geo1);
98 1
        $geo2 = GIS::create($geo2);
99 1
        return sprintf("ST_Distance(ST_GeomFromText('%s', %d),ST_GeomFromText('%s', %d))", $geo1->wkt, $geo1->srid, $geo2->wkt, $geo2->srid);
100
    }
101
}
102