Builder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
ccs 7
cts 11
cp 0.6364
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A update() 0 21 4
1
<?php
2
3
namespace LaravelSpatial\Eloquent;
4
5
use GeoJson\Geometry\Geometry;
6
use geoPHP\geoPHP;
7
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8
use LaravelSpatial\Exceptions\SpatialParseException;
9
10
/**
11
 * Class Builder
12
 *
13
 * @package LaravelSpatial\Eloquent
14
 */
15
class Builder extends EloquentBuilder
16
{
17
    /**
18
     * @inheritDoc
19
     */
20 12
    public function update(array $values)
21
    {
22 12
        foreach ($values as $key => &$value) {
23 12
            if ($value instanceof Geometry) {
24
                try {
25 12
                    $decoded = json_decode(json_encode($value->jsonSerialize(), JSON_THROW_ON_ERROR), false, 512, JSON_THROW_ON_ERROR);
26 12
                    $wkt     = geoPHP::load($decoded, 'json')
27
                                 ->out('wkt');
28
                } catch (\Exception $e) {
29
                    throw new SpatialParseException(
30
                        \sprintf('Unable to parse geometry data for column %s.', $key),
31
                        0,
32
                        $e
33
                    );
34
                }
35 12
36
                $value = $this->getQuery()->raw("ST_GeomFromText('{$wkt}')");
37
            }
38
        }
39 12
40
        return parent::update($values);
41
    }
42
}
43