Passed
Pull Request — master (#3)
by Hennik
04:48
created

Builder::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
ccs 7
cts 11
cp 0.6364
crap 4.7691
rs 9.9
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
                    $wkt = geoPHP::load(json_decode(json_encode($value->jsonSerialize()), false), 'json')
26 12
                                 ->out('wkt');
27
                } catch (\Exception $e) {
28
                    throw new SpatialParseException(
29
                        \sprintf('Unable to parse geometry data for column %s.', $key),
30
                        0,
31
                        $e
32
                    );
33
                }
34
35 12
                $value = $this->getQuery()->raw("ST_GeomFromText('{$wkt}')");
36
            }
37
        }
38
39 12
        return parent::update($values);
40
    }
41
}
42