| Total Complexity | 54 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GeoAnalysisController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GeoAnalysisController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class GeoAnalysisController extends MvcController |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * GeoAnalysis Provider |
||
| 41 | * @var GeoAnalysisProvider $provider |
||
| 42 | */ |
||
| 43 | protected $provider; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor for GeoAnalysis controller |
||
| 47 | * @param AbstractModule $module |
||
| 48 | */ |
||
| 49 | public function __construct(AbstractModule $module) { |
||
| 50 | parent::__construct($module); |
||
| 51 | |||
| 52 | $this->provider = $this->module->getProvider(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Pages |
||
| 57 | */ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * GeoAnalysis@index |
||
| 61 | */ |
||
| 62 | public function index() { |
||
| 63 | |||
| 64 | $controller = new PageController(); |
||
| 65 | $controller->setPageTitle(I18N::translate('Sosa Geographical dispersion')); |
||
| 66 | |||
| 67 | $data = new ViewBag(); |
||
| 68 | $data->set('title', $controller->getPageTitle()); |
||
| 69 | $data->set('has_analysis', false); |
||
| 70 | |||
| 71 | $ga_id = Filter::getInteger('ga_id'); |
||
| 72 | |||
| 73 | if($ga_id && $ga = $this->provider->getGeoAnalysis($ga_id)) { |
||
| 74 | $data->set('has_analysis', true); |
||
| 75 | $data->set('geoanalysis', $ga); |
||
| 76 | |||
| 77 | $controller |
||
| 78 | ->addExternalJavascript(Constants::WT_RAPHAEL_JS_URL()) |
||
| 79 | ->addInlineJavascript(' |
||
| 80 | jQuery("#geodispersion-tabs").tabs(); |
||
| 81 | jQuery("#geodispersion-tabs").css("visibility", "visible"); |
||
| 82 | |||
| 83 | jQuery.get( |
||
| 84 | "module.php", |
||
| 85 | { |
||
| 86 | "mod" : "'. $this->module->getName() .'", |
||
| 87 | "mod_action": "GeoAnalysis@dataTabs", |
||
| 88 | "ga_id" : "'.$ga_id.'" |
||
| 89 | }, |
||
| 90 | function(data){ |
||
| 91 | if(data){ |
||
| 92 | jQuery("#geodisp-data-general").html(data.generaltab); |
||
| 93 | jQuery("#geodisp-data-generations").html(data.generationstab); |
||
| 94 | } |
||
| 95 | jQuery(".loading-image").hide(); |
||
| 96 | }, |
||
| 97 | "json" |
||
| 98 | ); |
||
| 99 | '); |
||
| 100 | } |
||
| 101 | |||
| 102 | ViewFactory::make('GeoAnalysis', $this, $controller, $data)->render(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * GeoAnalysis@listAll |
||
| 107 | */ |
||
| 108 | public function listAll() { |
||
| 109 | |||
| 110 | $controller = new PageController(); |
||
| 111 | $controller->setPageTitle(I18N::translate('Sosa Geographical dispersion')); |
||
| 112 | |||
| 113 | $data = new ViewBag(); |
||
| 114 | $data->set('title', $controller->getPageTitle()); |
||
| 115 | $data->set('has_list', false); |
||
| 116 | |||
| 117 | $ga_list = $this->provider->getGeoAnalysisList(); |
||
| 118 | if(count($ga_list) > 0 ) { |
||
| 119 | $data->set('has_list', true); |
||
| 120 | $data->set('geoanalysislist', $ga_list); |
||
| 121 | } |
||
| 122 | |||
| 123 | ViewFactory::make('GeoAnalysisList', $this, $controller, $data)->render(); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * GeoAnalysis@setStatus |
||
| 128 | */ |
||
| 129 | public function setStatus() { |
||
| 130 | $controller = new JsonController(); |
||
| 131 | |||
| 132 | $ga_id = Filter::getInteger('ga_id'); |
||
| 133 | $ga = $this->provider->getGeoAnalysis($ga_id, false); |
||
| 134 | |||
| 135 | $controller->restrictAccess( |
||
| 136 | true // Filter::checkCsrf() -- Cannot use CSRF on a GET request (modules can only work with GET requests) |
||
| 137 | && Auth::isManager(Globals::getTree()) |
||
| 138 | && $ga !== null |
||
| 139 | ); |
||
| 140 | |||
| 141 | $status = Filter::getBool('status'); |
||
| 142 | $res = array('geoanalysis' => $ga->getId() , 'error' => null); |
||
| 143 | try{ |
||
| 144 | $this->provider->setGeoAnalysisStatus($ga, $status); |
||
|
|
|||
| 145 | $res['status'] = $status; |
||
| 146 | Log::addConfigurationLog('Module '.$this->module->getName().' : Geo Analysis ID "'.$ga->getId().'" has been '. ($status ? 'enabled' : 'disabled') .'.'); |
||
| 147 | } |
||
| 148 | catch (\Exception $ex) { |
||
| 149 | $res['error'] = $ex->getMessage(); |
||
| 150 | Log::addErrorLog('Module '.$this->module->getName().' : Geo Analysis ID "'.$ga->getId().'" could not be ' . ($status ? 'enabled' : 'disabled') .'. Error: '. $ex->getMessage()); |
||
| 151 | } |
||
| 152 | |||
| 153 | $controller->pageHeader(); |
||
| 154 | if($res['error']) http_response_code(500); |
||
| 155 | |||
| 156 | $controller->encode($res); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * GeoAnalysis@delete |
||
| 161 | */ |
||
| 162 | public function delete() { |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * GeoAnalysis@dataTabs |
||
| 192 | */ |
||
| 193 | public function dataTabs() { |
||
| 194 | $wt_tree = Globals::getTree(); |
||
| 195 | $controller = new JsonController(); |
||
| 196 | |||
| 197 | $ga_id = Filter::getInteger('ga_id'); |
||
| 198 | $ga = $this->provider->getGeoAnalysis($ga_id); |
||
| 199 | $sosa_provider = new SosaProvider($wt_tree, Auth::user()); |
||
| 200 | |||
| 201 | $controller |
||
| 202 | ->restrictAccess($ga && $sosa_provider->isSetup()) |
||
| 203 | ->pageHeader(); |
||
| 204 | |||
| 205 | $jsonArray = array(); |
||
| 206 | |||
| 207 | list($placesDispGeneral, $placesDispGenerations) = $ga->getAnalysisResults($sosa_provider->getAllSosaWithGenerations()); |
||
| 208 | |||
| 209 | $flags = array(); |
||
| 210 | if($placesDispGeneral && $ga->getOptions() && $ga->getOptions()->isUsingFlags()) { |
||
| 211 | $mapProvider = new GoogleMapsProvider(); |
||
| 212 | foreach($placesDispGeneral['places'] as $place => $count) { |
||
| 213 | $flags[$place] = $mapProvider->getPlaceIcon(new Place($place, $wt_tree)); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | $jsonArray['generaltab'] = $this->htmlPlacesAnalysisGeneralTab($ga, $placesDispGeneral, $flags); |
||
| 218 | $jsonArray['generationstab'] = $this->htmlPlacesAnalysisGenerationsTab($ga, $placesDispGenerations, $flags); |
||
| 219 | |||
| 220 | $controller->encode($jsonArray); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns HTML code for the GeoAnalysis general tab (can be either a map or a table). |
||
| 225 | * |
||
| 226 | * @param GeoAnalysis $ga Reference GeoAnalysis |
||
| 227 | * @param array $placesGeneralResults Analysis results at a general level |
||
| 228 | * @param (null|array) $flags Array of flags |
||
| 229 | * @return string HTML code for the general tab |
||
| 230 | */ |
||
| 231 | protected function htmlPlacesAnalysisGeneralTab(GeoAnalysis $ga, $placesGeneralResults, $flags= null) { |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns HTML code for the GeoAnalysis generations tab. |
||
| 299 | * |
||
| 300 | * @param GeoAnalysis $ga Reference GeoAnalysis |
||
| 301 | * @param array $placesGenerationsResults Analysis results at a generations level |
||
| 302 | * @param (null|array) $flags Array of flags |
||
| 303 | * @return string HTML code for the generations tab |
||
| 304 | */ |
||
| 305 | protected function htmlPlacesAnalysisGenerationsTab(GeoAnalysis $ga, $placesGenerationsResults, $flags = null) { |
||
| 367 | } |