summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaRelatedObject.php
blob: d35bcb0b1f403b7c92edd051fecbd94405c1d0e8 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php

namespace Icinga\Module\Director\Objects;

use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Objects\IcingaObject;

/**
 * Related Object
 *
 * This class comes in handy when working with simple foreign key references. In
 * contrast to an ORM it helps to deal with lazy-loaded objects in a way allowing
 * us to render objects with references which to no longer (or not yet) exist
 */
class IcingaRelatedObject
{
    /** @var IcingaObject Main object with (optional) relation */
    protected $owner;

    /** @var int Related object id */
    protected $id;

    /** @var int Related object name */
    protected $name;

    /** @var int Relation property name, e.g. 'host' */
    protected $key;

    /** @var int Relation property, e.g. 'host_id' */
    protected $idKey;

    /** @var IcingaObject Related object once loaded */
    protected $object;

    /** @var string Related class name */
    protected $className;

    /**
     * IcingaRelatedObject constructor
     *
     * @param IcingaObject $owner Main object referring a related one
     * @param string       $key   Main objects (short) property name for this
     */
    public function __construct(IcingaObject $owner, $key)
    {
        $this->owner = $owner;
        $this->key   = $key;
        $this->idKey = $key . '_id';
    }

    /**
     * Set a specific id
     *
     * @param $id int
     *
     * @return self
     */
    public function setId($id)
    {
        if (! is_int($id)) {
            throw new ProgrammingError(
                'An id must be an integer'
            );
        }

        if ($this->object !== null) {
            if ($this->object->id === $id) {
                return $this;
            } else {
                $this->object = null;
            }
        }

        if ($this->object === null) {
            $this->name = null;
        }

        $this->id = $id;
        $this->owner->set($this->getRealPropertyName(), $id);

        return $this;
    }

    /**
     * Return the related objects id
     *
     * @return int
     */
    public function getId()
    {
        if ($this->id === null) {
            $this->id = $this->getObject()->id;
        }

        return $this->id;
    }

    /**
     * Lazy-load the related object
     *
     * @return IcingaObject
     */
    public function getObject()
    {
        // TODO: This is unfinished

        if ($this->object === null) {
            $class = $this->getClassName();

            if ($this->name === null) {
                if ($id = $this->getId()) {
                }
            } else {
                $this->object = $class::load($this->name, $this->owner->getConnection());
            }
        }
        return $this->object;
    }

    /**
     * The real property name pointing to this relation, e.g. 'host_id'
     *
     * @return string
     */
    public function getRealPropertyName()
    {
        return $this->key . '_id';
    }

    /**
     * Full related class name
     *
     * @return string
     */
    public function getClassName()
    {
        if ($this->className === null) {
            $this->className = __NAMESPACE__ . '\\' . $this->getShortClassName();
        }

        return $this->className;
    }

    /**
     * Related class name relative to Icinga\Module\Director\Objects
     *
     * @return string
     */
    public function getShortClassName()
    {
        return $this->owner->getRelationObjectClass($this->key);
    }

    /**
     * Set a related property
     *
     * This might be a string or an object
     * @param $related string|IcingaObject
     * @throws ProgrammingError
     *
     * return self
     */
    public function set($related)
    {
        if (is_string($related)) {
            $this->name = $related;
        } elseif (is_object($related)) {
            $className = $this->getClassName();
            if ($related instanceof $className) {
                $this->object = $related;
                $this->name = $object->object_name;
                $this->id = $object->id;
            } else {
                throw new ProgrammingError(
                    'Trying to set a related "%s" while expecting "%s"',
                    get_class($related),
                    $this->getShortClassName()
                );
            }
        } else {
            throw new ProgrammingError(
                'Related object can be name or object, got: %s',
                var_export($related, 1)
            );
        }

        return $this;
    }

    /**
     * Get the name of the related object
     *
     * @return string
     */
    public function getName()
    {
        if ($this->name === null) {
            return $this->owner->{$this->key};
        } else {
            return $this->name;
        }
    }

    /**
     * Conservative constructor to avoid issued with PHP GC
     */
    public function __destruct()
    {
        unset($this->owner);
    }
}