Completed
Push — master ( b785b5...60f4f4 )
by Andreas
17:14
created

midcom_services_dbclassloader   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 81
c 1
b 0
f 0
dl 0
loc 169
ccs 48
cts 66
cp 0.7272
rs 10
wmc 26

6 Methods

Rating   Name   Duplication   Size   Complexity  
A is_mgdschema_object() 0 11 3
A get_mgdschema_class_name_for_midcom_class() 0 17 4
A get_component_classes() 0 7 2
A is_midcom_db_object() 0 10 5
A get_component_for_class() 0 38 4
B get_midcom_class_name_for_mgdschema_object() 0 46 8
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midgard\portable\api\mgdobject;
10
11
/**
12
 * <b>How to write database class definitions:</b>
13
 *
14
 * The general idea is to provide MidCOM with a way to hook into every database interaction
15
 * between the component and the Midgard core.
16
 *
17
 * Since PHP does not allow for multiple inheritance (which would be really useful here),
18
 * a decorator pattern is used, which connects the class you actually use in your component
19
 * and the original MgdSchema class while at the same time routing all function calls through
20
 * midcom_core_dbaobject.
21
 *
22
 * The class loader does not require much information when registering classes:
23
 * An example declaration looks like this:
24
 *
25
 * <code>
26
 * [
27
 *     'midgard_article' => 'midcom_db_article'
28
 * ]
29
 * </code>
30
 *
31
 * The key is the MgdSchema class name from that you want to use. The class specified must exist.
32
 *
33
 * The value is the name of the MidCOM base class you intend to create.
34
 * It is checked for basic validity against the PHP restrictions on symbol naming, but the
35
 * class itself is not checked for existence. You <i>must</i> declare the class as listed at
36
 * all times, as typecasting and detection is done using this metadata property in the core.
37
 *
38
 * <b>Inherited class requirements</b>
39
 *
40
 * The classes you inherit from the intermediate stub classes must at this time satisfy one
41
 * requirement: You have to declare the midcom and mgdschema classnames:
42
 *
43
 * <code>
44
 * class midcom_db_article
45
 *     extends midcom_core_dbaobject
46
 * {
47
 *      public $__midcom_class_name__ = __CLASS__;
48
 *      public $__mgdschema_class_name__ = 'midgard_article';
49
 *
50
 * </code>
51
 *
52
 * @package midcom.services
53
 */
54
class midcom_services_dbclassloader
55
{
56
    /**
57
     * Simple helper to check whether we are dealing with a MgdSchema or MidCOM DBA
58
     * object or a subclass thereof.
59
     *
60
     * @param object $object The object to check
61
     */
62 5
    public function is_mgdschema_object($object) : bool
63
    {
64
        // Sometimes we might get class string instead of an object
65 5
        if (is_string($object)) {
0 ignored issues
show
introduced by
The condition is_string($object) is always false.
Loading history...
66 3
            $object = new $object;
67
        }
68 5
        if ($this->is_midcom_db_object($object)) {
69
            return true;
70
        }
71
72 5
        return is_a($object, mgdobject::class);
73
    }
74
75
    /**
76
     * Get component name associated with a class name to get its DBA classes defined
77
     */
78 31
    public function get_component_for_class(string $classname) : ?string
79
    {
80 31
        $class_parts = array_filter(explode('_', $classname));
81
        // Fix for incorrectly named classes
82
        $component_map = [
83 31
            'midgard' => 'midcom',
84
            'org.openpsa.campaign' => 'org.openpsa.directmarketing',
85
            'org.openpsa.link' => 'org.openpsa.directmarketing',
86
            'org.openpsa.document' => 'org.openpsa.documents',
87
            'org.openpsa.organization' => 'org.openpsa.contacts',
88
            'org.openpsa.person' => 'org.openpsa.contacts',
89
            'org.openpsa.role' => 'org.openpsa.contacts',
90
            'org.openpsa.member' => 'org.openpsa.contacts',
91
            'org.openpsa.salesproject' => 'org.openpsa.sales',
92
            'org.openpsa.offer' => 'org.openpsa.sales',
93
            'org.openpsa.event' => 'org.openpsa.calendar',
94
            'org.openpsa.eventmember' => 'org.openpsa.calendar',
95
            'org.openpsa.invoice' => 'org.openpsa.invoices',
96
            'org.openpsa.billing' => 'org.openpsa.invoices',
97
            'org.openpsa.query' => 'org.openpsa.reports',
98
            'org.openpsa.task' => 'org.openpsa.projects',
99
            'org.openpsa.project' => 'org.openpsa.projects',
100
            'org.openpsa.hour' => 'org.openpsa.expenses'
101
        ];
102
103 31
        while (!empty($class_parts)) {
104 31
            $component = implode('.', $class_parts);
105 31
            if (array_key_exists($component, $component_map)) {
106 25
                $component = $component_map[$component];
107
            }
108
109 31
            if (midcom::get()->componentloader->is_installed($component)) {
110 30
                return $component;
111
            }
112 29
            array_pop($class_parts);
113
        }
114
115 1
        return null;
116
    }
117
118
    /**
119
     * Get a MidCOM DB class name for a MgdSchema Object.
120
     * We also ensure that the corresponding component has been loaded.
121
     *
122
     * @param string|object $object The object (or classname) to check
123
     * @return string The corresponding MidCOM DB class name, false otherwise.
124
     */
125 291
    public function get_midcom_class_name_for_mgdschema_object($object)
126
    {
127 291
        static $dba_classes_by_mgdschema = [];
128
129 291
        if (is_string($object)) {
130
            // In some cases we get a class name instead
131 217
            $classname = $object;
132 169
        } elseif (is_object($object)) {
133 169
            $classname = get_class($object);
134
        } else {
135
            debug_print_r("Invalid input provided", $object, MIDCOM_LOG_WARN);
136
            return false;
137
        }
138
139 291
        if (isset($dba_classes_by_mgdschema[$classname])) {
140 289
            return $dba_classes_by_mgdschema[$classname];
141
        }
142
143 5
        if (!$this->is_mgdschema_object($object)) {
0 ignored issues
show
Bug introduced by
It seems like $object can also be of type string; however, parameter $object of midcom_services_dbclassl...::is_mgdschema_object() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

143
        if (!$this->is_mgdschema_object(/** @scrutinizer ignore-type */ $object)) {
Loading history...
144
            debug_add("{$classname} is not an MgdSchema object, not resolving to MidCOM DBA class", MIDCOM_LOG_WARN);
145
            $dba_classes_by_mgdschema[$classname] = false;
146
            return false;
147
        }
148
149 5
        if ($classname == midcom::get()->config->get('person_class')) {
150
            $component = 'midcom';
151
        } else {
152 5
            $component = $this->get_component_for_class($classname);
153 5
            if (!$component) {
154
                debug_add("Component for class {$classname} cannot be found", MIDCOM_LOG_WARN);
155
                $dba_classes_by_mgdschema[$classname] = false;
156
                return false;
157
            }
158
        }
159 5
        $definitions = $this->get_component_classes($component);
160
161
        //TODO: This allows components to override midcom classes fx. Do we want that?
162 5
        $dba_classes_by_mgdschema = array_merge($dba_classes_by_mgdschema, $definitions);
163
164 5
        if (array_key_exists($classname, $dba_classes_by_mgdschema)) {
165 5
            return $dba_classes_by_mgdschema[$classname];
166
        }
167
168
        debug_add("{$classname} cannot be resolved to any DBA class name");
169
        $dba_classes_by_mgdschema[$classname] = false;
170
        return false;
171
    }
172
173
    /**
174
     * Get an MgdSchema class name for a MidCOM DBA class name
175
     *
176
     * @param string $classname The MidCOM DBA classname to check
177
     * @return string The corresponding MidCOM DBA class name, false otherwise.
178
     */
179 6
    public function get_mgdschema_class_name_for_midcom_class(string $classname)
180
    {
181 6
        static $mapping = [];
182
183 6
        if (!array_key_exists($classname, $mapping)) {
184 4
            $mapping[$classname] = false;
185
186 4
            if (class_exists($classname)) {
187 4
                $dummy_object = new $classname();
188 4
                if (!$this->is_midcom_db_object($dummy_object)) {
189
                    return false;
190
                }
191 4
                $mapping[$classname] = $dummy_object->__mgdschema_class_name__;
192
            }
193
        }
194
195 6
        return $mapping[$classname];
196
    }
197
198
    /**
199
     * Simple helper to check whether we are dealing with a MidCOM Database object
200
     * or a subclass thereof.
201
     *
202
     * @param object|string $object The object (or classname) to check
203
     */
204 516
    public function is_midcom_db_object($object) : bool
205
    {
206 516
        if (is_object($object)) {
207 516
            return ($object instanceof midcom_core_dbaobject || $object instanceof midcom_core_dbaproxy);
208
        }
209
        if (is_string($object) && class_exists($object)) {
210
            return $this->is_midcom_db_object(new $object);
211
        }
212
213
        return false;
214
    }
215
216 9
    public function get_component_classes(string $component) : array
217
    {
218 9
        $map = midcom::get()->componentloader->get_manifest($component)->class_mapping;
219 9
        if ($component == 'midcom') {
220
            $map[midcom::get()->config->get('person_class')] = midcom_db_person::class;
221
        }
222 9
        return $map;
223
    }
224
}
225