Passed
Push — master ( 37791d...444a9e )
by Robbie
02:28
created

CKANRegistryPageController::readSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Page;
4
5
use PageController;
0 ignored issues
show
Bug introduced by
The type PageController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\CKANRegistry\Model\Resource;
7
use SilverStripe\CKANRegistry\Model\ResourceField;
8
use SilverStripe\CKANRegistry\Model\ResourceFilter;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Control\HTTPResponse;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\View\Requirements;
14
15
class CKANRegistryPageController extends PageController
16
{
17
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
18
        'readSchema',
19
    ];
20
21
    private static $url_handlers = [
0 ignored issues
show
introduced by
The private property $url_handlers is not used, and could be removed.
Loading history...
22
        // The "view" action is routed to index. The frontend implementation should take care of the frontend
23
        // routing for sub URLs. We will route /schema and /view/123/schema to the readSchema method though.
24
        'GET view/$Item/schema' => 'readSchema',
25
        'view/$Item' => 'index',
26
        'GET schema' => 'readSchema',
27
    ];
28
29
    protected function init()
30
    {
31
        parent::init();
32
33
        Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js');
34
        Requirements::add_i18n_javascript('silverstripe/ckan-registry: client/lang');
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\View\Requir...::add_i18n_javascript() has been deprecated. ( Ignorable by Annotation )

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

34
        /** @scrutinizer ignore-deprecated */ Requirements::add_i18n_javascript('silverstripe/ckan-registry: client/lang');
Loading history...
35
    }
36
37
    /**
38
     * Loads model data encapsulated as JSON in order to power front end technologies used to render that
39
     * data. Includes critical info such as the CKAN site to query (e.g. which domain, datastore, etc.)
40
     * but also can be extended to be used for configuring the component used to show this (e.g. React.js
41
     * or Vue.js component configuration).
42
     *
43
     * @param DataObject $holder
44
     * @return array
45
     */
46
    public function getCKANClientConfig(DataObject $holder = null)
47
    {
48
        if (!$holder) {
49
            $holder = $this->data();
50
        }
51
52
        /** @var Resource $resource */
53
        $resource = $holder->getComponent('DataResource');
54
55
        $config = [
56
            'spec' => [
57
                'endpoint' => $resource->Endpoint,
58
                'dataset' => $resource->DataSet,
59
                'identifier' => $resource->Identifier,
60
            ],
61
            'name' => $resource->Name,
62
            'resourceName' => $resource->ResourceName,
63
            'basePath' => $this->getBasePath($holder),
64
            'fields' => array_map(
65
                function (ResourceField $field) {
66
                    return [
67
                        'OriginalLabel' => $field->OriginalLabel,
68
                        'ReadableLabel' => $field->ReadableLabel,
69
                        'ShowInResultsView' => (bool) $field->ShowInResultsView,
70
                        'ShowInDetailView' => (bool) $field->ShowInDetailView,
71
                        'DisplayConditions' => json_decode($field->DisplayConditions, true),
72
                        'RemoveDuplicates' => (bool) $field->RemoveDuplicates,
73
                        'Type' => $field->Type,
74
                    ];
75
                },
76
                $resource->Fields()->filterAny([
77
                    'ShowInResultsView' => true,
78
                    'ShowInDetailView' => true,
79
                    'RemoveDuplicates' => true,
80
                ])->Sort('Position', 'ASC')->toArray()
81
            ),
82
            'filters' => array_map(
83
                function (ResourceFilter $filter) {
84
                    $explodedClassName = explode('\\', get_class($filter));
85
                    return [
86
                        'type' => array_pop($explodedClassName),
87
                    ] + $filter->getClientConfig();
88
                },
89
                $resource->Filters()->sort('Order')->toArray()
90
            )
91
        ];
92
93
        $this->extend('updateCKANClientConfig', $config);
94
95
        return $config;
96
    }
97
98
    /**
99
     * Returns the frontend application client configuration schema
100
     *
101
     * @param HTTPRequest $request
102
     */
103
    public function readSchema(HTTPRequest $request)
104
    {
105
        $response = HTTPResponse::create()
106
            ->addHeader('Content-Type', 'application/json')
107
            ->setBody(json_encode($this->getCKANClientConfig()));
108
        return $response;
109
    }
110
111
    /**
112
     * Returns the base path for the resource's page with a leading slash
113
     *
114
     * @param DataObject $holder
115
     * @return string
116
     */
117
    public function getBasePath(DataObject $holder = null)
118
    {
119
        if (!$holder) {
120
            return '/';
121
        }
122
123
        $link = $holder->RelativeLink();
124
        return Director::baseURL() . trim($link, '/');
125
    }
126
}
127