ResourceManager::setup()   C
last analyzed

Complexity

Conditions 16
Paths 20

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 161.1113
Metric Value
cc 16
eloc 30
nc 20
nop 1
dl 0
loc 45
ccs 5
cts 29
cp 0.1724
crap 161.1113
rs 5.0151

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*********************************************************************************
3
 * SugarCRM Community Edition is a customer relationship management program developed by
4
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5
6
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
7
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU Affero General Public License version 3 as published by the
11
 * Free Software Foundation with the addition of the following permission added
12
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15
 *
16
 * This program is distributed in the hope that it will be useful, but WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License along with
22
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
 * 02110-1301 USA.
25
 *
26
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28
 *
29
 * The interactive user interfaces in modified source and object code versions
30
 * of this program must display Appropriate Legal Notices, as required under
31
 * Section 5 of the GNU Affero General Public License version 3.
32
 *
33
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34
 * these Appropriate Legal Notices must retain the display of the "Powered by
35
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
37
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38
 ********************************************************************************/
39
40
41
/**
42
 * ResourceManager.php
43
 * This class is responsible for resource management of SQL queries, file usage, etc.
44
 */
45
class ResourceManager {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
46
47
private static $instance;
48
private $_observers = array();
49
50
/**
51
 * The constructor; declared as private
52
 */
53 1
private function __construct() {
54
55 1
}
56
57
/**
58
 * getInstance
59
 * Singleton method to return static instance of ResourceManager
60
 * @return The static singleton ResourceManager instance
61
 */
62 1
static public function getInstance(){
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
63 1
    if (!isset(self::$instance)) {
64 1
        self::$instance = new ResourceManager();
65
    } // if
66 1
    return self::$instance;
67
}
68
69
/**
70
 * setup
71
 * Handles determining the appropriate setup based on client type.
72
 * It will create a SoapResourceObserver instance if the $module parameter is set to
73
 * 'Soap'; otherwise, it will try to create a WebResourceObserver instance.
74
 * @param $module The module value used to create the corresponding observer
75
 * @return boolean value indicating whether or not an observer was successfully setup
76
 */
77 1
public function setup($module) {
78
	//Check if config.php exists
79 1
	if(!file_exists('config.php') || empty($module)) {
80 1
	   return false;
81
	}
82
83 1
    if($module == 'Soap') {
84
      require_once('include/resource/Observers/SoapResourceObserver.php');
85
      $observer = new SoapResourceObserver('Soap');
86
    } elseif(defined('SUITE_PHPUNIT_RUNNER')) {
87 1
        return;
88
    }else{
89
      require_once('include/resource/Observers/WebResourceObserver.php');
90
      $observer = new WebResourceObserver($module);
91
    }
92
93
	//Load config
94
	if(!empty($observer->module)) {
95
		$limit = 0;
96
97
		if(isset($GLOBALS['sugar_config']['resource_management'])) {
98
			   $res = $GLOBALS['sugar_config']['resource_management'];
99
			if(!empty($res['special_query_modules']) &&
100
			   in_array($observer->module, $res['special_query_modules']) &&
101
			   !empty($res['special_query_limit']) &&
102
			   is_int($res['special_query_limit']) &&
103
			   $res['special_query_limit'] > 0) {
104
			   $limit = $res['special_query_limit'];
105
			} else if(!empty($res['default_limit']) && is_int($res['default_limit']) && $res['default_limit'] > 0) {
106
			   $limit = $res['default_limit'];
107
			}
108
		} //if
109
110
		if($limit) {
111
112
		   $db = DBManagerFactory::getInstance();
113
		   $db->setQueryLimit($limit);
114
		   $observer->setLimit($limit);
115
		   $this->_observers[] = $observer;
116
		}
117
		return true;
118
	}
119
120
	return false;
121
}
122
123
/**
124
 * notifyObservers
125
 * This method notifies the registered observers with the provided message.
126
 * @param $msg Message from language file to notify observers with
127
 */
128
public function notifyObservers($msg) {
129
130
	if(empty($this->_observers)) {
131
	   return;
132
	}
133
134
    //Notify observers limit has been reached
135
    if(empty($GLOBALS['app_strings'])) {
136
       $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
137
    }
138
    $limitMsg = $GLOBALS['app_strings'][$msg];
139
    foreach( $this->_observers as $observer) {
140
	    $limit = $observer->limit;
141
	    $module = $observer->module;
142
	    eval("\$limitMsg = \"$limitMsg\";");
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
143
	    $GLOBALS['log']->fatal($limitMsg);
144
	    $observer->notify($limitMsg);
145
    }
146
}
147
148
149
/*
150
 * getObservers
151
 * Returns the observer instances that have been setup for the ResourceManager instance
152
 * @return Array of ResourceObserver(s)
153
 */
154
function getObservers() {
155
    return $this->_observers;
156
}
157
158
}
159
?>