Passed
Push — master ( 179526...2874e4 )
by Andreas
28:11
created

get_component_for_class()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 28
nc 5
nop 1
dl 0
loc 38
ccs 30
cts 30
cp 1
crap 4
rs 9.472
c 1
b 0
f 0
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 Doctrine\Common\Util\ClassUtils;
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 string $__midcom_class_name__ = __CLASS__;
48
 *      public string $__mgdschema_class_name__ = 'midgard_article';
49
 *
50
 * </code>
51
 *
52
 * @package midcom.services
53
 */
54
class midcom_services_dbclassloader
55
{
56
    private array $map;
57
58 2
    public function __construct(array $map)
59
    {
60 2
        $this->map = $map;
61
    }
62
63
    /**
64
     * Get component name associated with a class name to get its DBA classes defined
65
     */
66 31
    public function get_component_for_class(string $classname) : ?string
67
    {
68 31
        $class_parts = array_filter(explode('_', $classname));
69
        // Fix for incorrectly named classes
70 31
        $component_map = [
71 31
            'midgard' => 'midcom',
72 31
            'org.openpsa.campaign' => 'org.openpsa.directmarketing',
73 31
            'org.openpsa.link' => 'org.openpsa.directmarketing',
74 31
            'org.openpsa.document' => 'org.openpsa.documents',
75 31
            'org.openpsa.organization' => 'org.openpsa.contacts',
76 31
            'org.openpsa.person' => 'org.openpsa.contacts',
77 31
            'org.openpsa.member' => 'org.openpsa.contacts',
78 31
            'org.openpsa.salesproject' => 'org.openpsa.sales',
79 31
            'org.openpsa.offer' => 'org.openpsa.sales',
80 31
            'org.openpsa.event' => 'org.openpsa.calendar',
81 31
            'org.openpsa.eventmember' => 'org.openpsa.calendar',
82 31
            'org.openpsa.invoice' => 'org.openpsa.invoices',
83 31
            'org.openpsa.billing' => 'org.openpsa.invoices',
84 31
            'org.openpsa.query' => 'org.openpsa.reports',
85 31
            'org.openpsa.task' => 'org.openpsa.projects',
86 31
            'org.openpsa.project' => 'org.openpsa.projects',
87 31
            'org.openpsa.role' => 'org.openpsa.projects',
88 31
            'org.openpsa.hour' => 'org.openpsa.expenses'
89 31
        ];
90
91 31
        while (!empty($class_parts)) {
92 31
            $component = implode('.', $class_parts);
93 31
            if (array_key_exists($component, $component_map)) {
94 21
                $component = $component_map[$component];
95
            }
96
97 31
            if (array_key_exists($component, $this->map)) {
98 30
                return $component;
99
            }
100 29
            array_pop($class_parts);
101
        }
102
103 1
        return null;
104
    }
105
106
    /**
107
     * Get a MidCOM DB class name for a MgdSchema Object.
108
     * We also ensure that the corresponding component has been loaded.
109
     */
110 324
    public function get_midcom_class_name_for_mgdschema_object(string|object $classname) : ?string
111
    {
112 324
        static $dba_classes_by_mgdschema = [];
113
114 324
        if (is_object($classname)) {
115 162
            $classname = ClassUtils::getClass($classname);
116
        }
117
118 324
        if (!array_key_exists($classname, $dba_classes_by_mgdschema)) {
119 11
            foreach ($this->map as $mapping) {
120 11
                if (array_key_exists($classname, $mapping)) {
121 11
                    $dba_classes_by_mgdschema[$classname] = $mapping[$classname];
122 11
                    break;
123
                }
124
            }
125 11
            if (!array_key_exists($classname, $dba_classes_by_mgdschema)) {
126
                debug_add("{$classname} cannot be resolved to any DBA class name");
127
                $dba_classes_by_mgdschema[$classname] = null;
128
            }
129
        }
130
131 324
        return $dba_classes_by_mgdschema[$classname];
132
    }
133
134
    /**
135
     * Get an MgdSchema class name for a MidCOM DBA class name
136
     *
137
     * @return string The corresponding MidCOM DBA class name, false otherwise.
138
     */
139 500
    public function get_mgdschema_class_name_for_midcom_class(string $classname)
140
    {
141 500
        static $mapping = [];
142
143 500
        if (!array_key_exists($classname, $mapping)) {
144 14
            $mapping[$classname] = false;
145
146 14
            if (class_exists($classname)) {
147 14
                if ($this->is_midcom_db_object($classname)) {
148 14
                    $mapping[$classname] = (new $classname)->__mgdschema_class_name__;
149
                }
150
            }
151
        }
152
153 500
        return $mapping[$classname];
154
    }
155
156
    /**
157
     * Simple helper to check whether we are dealing with a MidCOM Database object
158
     * or a subclass thereof.
159
     */
160 526
    public function is_midcom_db_object(object|string $object) : bool
161
    {
162 526
        return is_subclass_of($object, midcom_core_dbaobject::class)
163 526
            || is_a($object, midcom_core_dbaproxy::class, true);
164
    }
165
166 6
    public function get_component_classes(string $component) : array
167
    {
168 6
        return $this->map[$component];
169
    }
170
}
171