Completed
Branch master (b03c4d)
by Andreas
06:02
created

AbstractGISWebServiceController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 59
ccs 30
cts 35
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getRecords() 0 37 7
A getConfig() 0 11 3
A getModel() 0 5 2
1
<?php
2
3
namespace Smindel\GIS\Control;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Permission;
9
use Smindel\GIS\GIS;
10
use Exception;
11
12
class AbstractGISWebServiceController extends Controller
13
{
14 5
    public function getModel($request)
15
    {
16 5
        $request = $request ?: $this->getRequest();
17 5
        $class = str_replace('-', '\\', $request->param('Model'));
18 5
        return $class;
19
    }
20
21 5
    public function getConfig($model)
22
    {
23 5
        $defaults = Config::inst()->get(static::class);
24 5
        $modelConfig = Config::inst()->get($model, strtolower(array_reverse(explode('\\', static::class))[0]));
25 5
        if (!$modelConfig) {
26
            return false;
27
        }
28 5
        $defaults['record_provider'] = null;
29 5
        $defaults['geometry_field'] = GIS::of($model);
30 5
        $defaults['searchable_fields'] = singleton($model)->searchableFields();
31 5
        return is_array($modelConfig) ? array_merge($defaults, $modelConfig) : $defaults;
32
    }
33
34 4
    public function getRecords($request)
35
    {
36 4
        $model = $this->getModel($request);
37 4
        $config = $this->getConfig($model);
38
39 4
        if (!is_a($model, DataObject::class, true)) {
40
            throw new Exception(sprintf('%s not found', $model), 404);
41
        }
42
43 4
        if (!$config) {
44
            throw new Exception(sprintf('%s not configured for %s', static::class, $model), 404);
45
        }
46
47 4
        if (isset($config['code']) && !Permission::check($config['code'])) {
48
            throw new Exception(sprintf('You are not allowed to access %s through %s', $model, static::class), 403);
49
        }
50
51 4
        $skip_filter = false;
52 4
        $list = is_callable($config['record_provider'])
53
            ? \Closure::fromCallable($config['record_provider'])($request, $skip_filter)
54 4
            : $model::get();
55
56 4
        if (!$skip_filter) {
0 ignored issues
show
introduced by
The condition $skip_filter is always false.
Loading history...
57 4
            $queryParams = array_intersect_ukey(
58 4
                $request->requestVars(),
59 4
                $config['searchable_fields'],
60 4
                function($a, $b) {
61 1
                    $a = explode(':', $a)[0];
62 1
                    $b = explode(':', $b)[0];
63 1
                    return strcmp($a, $b);
64 4
                }
65
            );
66
67
            $list = $list->filter($queryParams);
68
        }
69
70
        return $list;
71
    }
72
}
73