SugarRelationshipFactory   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.15%
Metric Value
dl 0
loc 174
ccs 69
cts 82
cp 0.8415
rs 8.8
wmc 36
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 6 2
A rebuildCache() 0 4 1
A deleteCache() 0 8 2
C getRelationship() 0 47 13
A getRelationshipDef() 0 9 2
A loadRelationships() 0 10 2
C buildRelationshipCache() 0 54 12
A getCacheFile() 0 3 1
1
<?php
2
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
42
43
require_once("data/Relationships/SugarRelationship.php");
44
45
/**
46
 * Create relationship objects
47
 * @api
48
 */
49
class SugarRelationshipFactory {
50
    static $rfInstance;
51
52
    protected $relationships;
53
54 1
    protected function __construct(){
55
        //Load the relationship definitions from the cache.
56 1
        $this->loadRelationships();
57 1
    }
58
59
    /**
60
     * @static
61
     * @return SugarRelationshipFactory
62
     */
63 128
    public static function getInstance()
64
    {
65 128
        if (is_null(self::$rfInstance))
66 1
            self::$rfInstance = new SugarRelationshipFactory();
67 128
        return self::$rfInstance;
68
    }
69
70
    public static function rebuildCache()
71
    {
72
        self::getInstance()->buildRelationshipCache();
73
    }
74
75 2
    public static function deleteCache()
76
    {
77 2
        $file = self::getInstance()->getCacheFile();
78 2
        if(sugar_is_file($file))
79
        {
80 1
            unlink($file);
81
        }
82 2
    }
83
84
    /**
85
     * @param  $relationshipName String name of relationship to load
86
     * @return void
87
     *
88
     *
89
     *
90
     */
91 126
    public function getRelationship($relationshipName)
92
    {
93 126
        if (empty($this->relationships[$relationshipName])) {
94 3
            $GLOBALS['log']->error("Unable to find relationship $relationshipName");
95 3
            return false;
96
        }
97
98 126
        $def = $this->relationships[$relationshipName];
99
100 126
        $type = isset($def['true_relationship_type']) ? $def['true_relationship_type'] : $def['relationship_type'];
101
        switch($type)
102
        {
103 126
            case "many-to-many":
104 47
                if (isset($def['rhs_module']) && $def['rhs_module'] == 'EmailAddresses')
105
                {
106 2
                    require_once("data/Relationships/EmailAddressRelationship.php");
107 2
                    return new EmailAddressRelationship($def);
108
                }
109 47
                require_once("data/Relationships/M2MRelationship.php");
110 47
                return new M2MRelationship($def);
111
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
112 110
            case "one-to-many":
113 109
                require_once("data/Relationships/One2MBeanRelationship.php");
114
                //If a relationship has no table or join keys, it must be bean based
115 109
                if (empty($def['true_relationship_type']) || (empty($def['table']) && empty($def['join_table'])) || empty($def['join_key_rhs'])){
116 108
                    return new One2MBeanRelationship($def);
117
                }
118
                else {
119 1
                    return new One2MRelationship($def);
120
                }
121
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
122 3
            case "one-to-one":
123 3
                if (empty($def['true_relationship_type'])){
124 3
                    require_once("data/Relationships/One2OneBeanRelationship.php");
125 3
                    return new One2OneBeanRelationship($def);
126
                }
127
                else {
128
                    require_once("data/Relationships/One2OneRelationship.php");
129
                    return new One2OneRelationship($def);
130
                }
131
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
132
        }
133
134
        $GLOBALS['log']->fatal ("$relationshipName had an unknown type $type ");
135
136
        return false;
137
    }
138
139 3
    public function getRelationshipDef($relationshipName)
140
    {
141 3
        if (empty($this->relationships[$relationshipName])) {
142
            $GLOBALS['log']->error("Unable to find relationship $relationshipName");
143
            return false;
144
        }
145
146 3
        return $this->relationships[$relationshipName];
147
    }
148
149
150 1
    protected function loadRelationships()
151
    {
152 1
        if(sugar_is_file($this->getCacheFile()))
153
        {
154
            include($this->getCacheFile());
155
            $this->relationships = $relationships;
156
        } else {
157 1
            $this->buildRelationshipCache();
158
        }
159 1
    }
160
161 1
    protected function buildRelationshipCache()
162
    {
163 1
        global $beanList, $dictionary, $buildingRelCache;
164 1
        if ($buildingRelCache)
165
            return;
166 1
        $buildingRelCache = true;
167 1
        include("modules/TableDictionary.php");
168
169 1
        if (empty($beanList))
170
            include("include/modules.php");
171
        //Reload ALL the module vardefs....
172 1
        foreach($beanList as $moduleName => $beanName)
173
        {
174 1
            VardefManager::loadVardef($moduleName, BeanFactory::getObjectName($moduleName), false, array(
175
                //If relationships are not yet loaded, we can't figure out the rel_calc_fields.
176 1
                "ignore_rel_calc_fields" => true,
177
            ));
178
        }
179
180 1
        $relationships = array();
181
182
        //Grab all the relationships from the dictionary.
183 1
        foreach ($dictionary as $key => $def)
184
        {
185 1
            if (!empty($def['relationships']))
186
            {
187 1
                foreach($def['relationships'] as $relKey => $relDef)
188
                {
189 1
                    if ($key == $relKey) //Relationship only entry, we need to capture everything
190 1
                        $relationships[$key] = array_merge(array('name' => $key), (array)$def, (array)$relDef);
191
                    else {
192 1
                        $relationships[$relKey] = array_merge(array('name' => $relKey), (array)$relDef);
193 1
                        if(!empty($relationships[$relKey]['join_table']) && empty($relationships[$relKey]['fields'])
194 1
                            && isset($dictionary[$relationships[$relKey]['join_table']]['fields'])) {
195 1
                            $relationships[$relKey]['fields'] = $dictionary[$relationships[$relKey]['join_table']]['fields'];
196
                        }
197
                    }
198
                }
199
            }
200
        }
201
        //Save it out
202 1
        sugar_mkdir(dirname($this->getCacheFile()), null, true);
203 1
        $out = "<?php \n \$relationships = " . var_export($relationships, true) . ";";
204 1
        sugar_file_put_contents_atomic($this->getCacheFile(), $out);
205
206 1
        $this->relationships = $relationships;
207
208
        //Now load all vardefs a second time populating the rel_calc_fields
209 1
        foreach ($beanList as $moduleName => $beanName) {
210 1
            VardefManager::loadVardef($moduleName, BeanFactory::getObjectName($moduleName));
211
        }
212
213 1
        $buildingRelCache = false;
214 1
    }
215
216 3
	protected function getCacheFile() {
217 3
		return sugar_cached("Relationships/relationships.cache.php");
218
	}
219
220
221
222
}
223