Completed
Push — master ( 4a2c54...cce130 )
by
unknown
15s
created

Resource::getCKANClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Model;
4
5
use SilverStripe\CKANRegistry\Service\ResourcePopulatorInterface;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\HasManyList;
9
10
/**
11
 * A CKAN Resource that belongs to a DataSet/Package, as to be accessed via the CKAN API.
12
 *
13
 * @property string Name
14
 * @property string ResourceName
15
 * @property string Endpoint
16
 * @property string DataSet
17
 * @property string Identifier
18
 * @method HasManyList Fields
19
 * @method HasManyList Filters
20
 */
21
class Resource extends DataObject
22
{
23
    private static $table_name = 'CKANResource';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
24
25
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
26
        'Name' => 'Varchar',
27
        'ResourceName' => 'Varchar',
28
        'Endpoint' => 'Varchar',
29
        'DataSet' => 'Varchar',
30
        'Identifier' => 'Varchar',
31
        'ItemsPerPage' => 'Int',
32
    ];
33
34
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
35
        'Fields' => ResourceField::class,
36
        'Filters' => ResourceFilter::class,
37
    ];
38
39
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
40
        'ItemsPerPage' => 30,
41
    ];
42
43
    /**
44
     * Whenever the CKAN resource identifier is changed we should clear any existing field and filter configurations
45
     *
46
     * {@inheritdoc}
47
     */
48
    public function onAfterWrite()
49
    {
50
        if ($this->isChanged('Identifier')) {
51
            $this->Fields()->each(function (ResourceField $field) {
52
                $field->delete();
53
            });
54
55
            /** @var ResourcePopulatorInterface $populator */
56
            $populator = Injector::inst()->get(ResourcePopulatorInterface::class);
57
            $populator->populateFields($this);
58
59
            // Remove the existing filters and add a default text entry to search all ResourceFields
60
            $this->Filters()
61
                ->each(function (ResourceFilter $filter) {
62
                    $filter->delete();
63
                })
64
                ->add(ResourceFilter::create());
65
        }
66
67
        parent::onAfterWrite();
68
    }
69
70
    /**
71
     * Whenever the CKAN resource identifier is changed, populate a new set of metadata for the new resource
72
     *
73
     * {@inheritdoc}
74
     */
75
    public function onBeforeWrite()
76
    {
77
        if ($this->isChanged('Identifier')) {
78
            /** @var ResourcePopulatorInterface $populator */
79
            $populator = Injector::inst()->get(ResourcePopulatorInterface::class);
80
            $populator->populateMetadata($this);
81
        }
82
83
        parent::onBeforeWrite();
84
    }
85
86
    /**
87
     * Loads model data encapsulated as JSON in order to power front end technologies used to render that
88
     * data. Includes critical info such as the CKAN site to query (e.g. which domain, datastore, etc.)
89
     * but also can be extended to be used for configuring the component used to show this (e.g. React.js
90
     * or Vue.js component configuration).
91
     *
92
     * @return string
93
     */
94
    public function getCKANClientConfig()
95
    {
96
        $config = '{}';
97
        $this->extend('updateCKANClientConfig', $config);
98
        return $config;
99
    }
100
}
101