summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/ImportRowModifier.php
blob: 76982c2eb128fde2e788c1b557105543cba6069e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace Icinga\Module\Director\Objects;

use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Objects\Extension\PriorityColumn;
use RuntimeException;

class ImportRowModifier extends DbObjectWithSettings implements InstantiatedViaHook
{
    use PriorityColumn;

    protected $table = 'import_row_modifier';

    protected $keyName = 'id';

    protected $autoincKeyName = 'id';

    protected $defaultProperties = [
        'id'              => null,
        'source_id'       => null,
        'property_name'   => null,
        'provider_class'  => null,
        'target_property' => null,
        'priority'        => null,
        'description'     => null,
    ];

    protected $settingsTable = 'import_row_modifier_setting';

    protected $settingsRemoteId = 'row_modifier_id';

    private $hookInstance;

    public function getInstance()
    {
        if ($this->hookInstance === null) {
            $class = $this->get('provider_class');
            /** @var PropertyModifierHook $obj */
            if (! class_exists($class)) {
                throw new RuntimeException(sprintf(
                    'Cannot instantiate Property modifier %s',
                    $class
                ));
            }
            $obj = new $class;
            $obj->setSettings($this->getSettings());
            $obj->setPropertyName($this->get('property_name'));
            $obj->setTargetProperty($this->get('target_property'));
            $obj->setDb($this->connection);
            $this->hookInstance = $obj;
        }

        return $this->hookInstance;
    }

    /**
     * @deprecated please use \Icinga\Module\Director\Data\Exporter
     * @return \stdClass
     */
    public function export()
    {
        $properties =  $this->getProperties();
        unset($properties['id']);
        unset($properties['source_id']);
        $properties['settings'] = $this->getInstance()->exportSettings();
        ksort($properties);

        return (object) $properties;
    }

    public function setSettings($settings)
    {
        $settings = $this->getInstance()->setSettings((array) $settings)->getSettings();

        return parent::setSettings($settings); // TODO: Change the autogenerated stub
    }

    protected function beforeStore()
    {
        if (! $this->hasBeenLoadedFromDb() && $this->get('priority') === null) {
            $this->setNextPriority('source_id');
        }
    }

    protected function onInsert()
    {
        $this->refreshPriortyProperty();
    }
}