AbstractGISWebServiceController::getRecords()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.3387

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 37
ccs 17
cts 21
cp 0.8095
crap 7.3387
rs 8.6346
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
use SilverStripe\Control\Director;
12
13
class AbstractGISWebServiceController extends Controller
14 5
{
15
    public function getModel($request)
16 5
    {
17 5
        $request = $request ?: $this->getRequest();
18 5
        $class = str_replace('-', '\\', $request->param('Model'));
19
        return $class;
20
    }
21 5
22
    public function getConfig($model)
23 5
    {
24 5
        $defaults = Config::inst()->get(static::class);
25 5
        $modelConfig = Config::inst()->get($model, strtolower(array_reverse(explode('\\', static::class))[0]));
26
        if (!$modelConfig) {
27
            return false;
28 5
        }
29 5
        $defaults['record_provider'] = null;
30 5
        $defaults['access_control_allow_origin'] = Director::absoluteURL('/');
31 5
        $defaults['geometry_field'] = GIS::of($model);
32
        $defaults['searchable_fields'] = singleton($model)->searchableFields();
33
        return is_array($modelConfig) ? array_merge($defaults, $modelConfig) : $defaults;
34 4
    }
35
36 4
    public function getRecords($request)
37 4
    {
38
        $model = $this->getModel($request);
39 4
        $config = $this->getConfig($model);
40
41
        if (!is_a($model, DataObject::class, true)) {
42
            throw new Exception(sprintf('%s not found', $model), 404);
43 4
        }
44
45
        if (!$config) {
46
            throw new Exception(sprintf('%s not configured for %s', static::class, $model), 404);
47 4
        }
48
49
        if (isset($config['code']) && !Permission::check($config['code'])) {
50
            throw new Exception(sprintf('You are not allowed to access %s through %s', $model, static::class), 403);
51 4
        }
52 4
53
        $skip_filter = false;
54 4
        $list = is_callable($config['record_provider'])
55
            ? \Closure::fromCallable($config['record_provider'])($request, $skip_filter)
56 4
            : $model::get();
57 4
58 4
        if (!$skip_filter) {
0 ignored issues
show
introduced by
The condition $skip_filter is always false.
Loading history...
59 4
            $queryParams = array_intersect_ukey(
60 4
                $request->requestVars(),
61 1
                $config['searchable_fields'],
62 1
                function ($a, $b) {
63 1
                    $a = explode(':', $a)[0];
64 4
                    $b = explode(':', $b)[0];
65
                    return strcmp($a, $b);
66
                }
67
            );
68
69
            $list = $list->filter($queryParams);
70
        }
71
72
        return $list;
73
    }
74
}
75