summaryrefslogtreecommitdiffstats
path: root/library/Director/Data/Db/DbObjectTypeRegistry.php
blob: 0c226d6528ffb9666dcec7e4021c12efcc62cdf5 (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
<?php

namespace Icinga\Module\Director\Data\Db;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;

class DbObjectTypeRegistry
{
    /**
     * @param $type
     * @return string|DbObject Fake typehint for IDE
     */
    public static function classByType($type)
    {
        // allow for icinga_host and host
        $type = lcfirst(preg_replace('/^icinga_/', '', $type));

        // Hint: Sync/Import are not IcingaObjects, this should be reconsidered:
        if (strpos($type, 'import') === 0 || strpos($type, 'sync') === 0) {
            $prefix = '';
        } elseif (strpos($type, 'data') === false) {
            $prefix = 'Icinga';
        } else {
            $prefix = 'Director';
        }

        // TODO: Provide a more sophisticated solution
        if ($type === 'hostgroup') {
            $type = 'hostGroup';
        } elseif ($type === 'usergroup') {
            $type = 'userGroup';
        } elseif ($type === 'timeperiod') {
            $type = 'timePeriod';
        } elseif ($type === 'servicegroup') {
            $type = 'serviceGroup';
        } elseif ($type === 'service_set' || $type === 'serviceset') {
            $type = 'serviceSet';
        } elseif ($type === 'apiuser') {
            $type = 'apiUser';
        } elseif ($type === 'host_template_choice') {
            $type = 'templateChoiceHost';
        } elseif ($type === 'service_template_choice') {
            $type = 'TemplateChoiceService';
        } elseif ($type === 'scheduled_downtime' || $type === 'scheduled-downtime') {
            $type = 'ScheduledDowntime';
        }

        return 'Icinga\\Module\\Director\\Objects\\' . $prefix . ucfirst($type);
    }

    public static function tableNameByType($type)
    {
        $class = static::classByType($type);
        $dummy = $class::create([]);

        return $dummy->getTableName();
    }

    public static function shortTypeForObject(DbObject $object)
    {
        if ($object instanceof IcingaObject) {
            return $object->getShortTableName();
        }

        return $object->getTableName();
    }

    public static function newObject($type, $properties = [], Db $db = null)
    {
        /** @var DbObject $class fake hint for the IDE, it's a string */
        $class = self::classByType($type);
        return $class::create($properties, $db);
    }
}