| Conditions | 29 |
| Paths | 924 |
| Total Lines | 127 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 4 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | protected function _install($ocode) |
||
| 20 | { |
||
| 21 | $this->command->line("Installing <fg=green>$ocode</>"); |
||
| 22 | |||
| 23 | $babelConfig=json_decode(file_get_contents(babel_path("Extension/$ocode/babel.json")),true); |
||
| 24 | |||
| 25 | // support __cur__ variables |
||
| 26 | if($babelConfig["version"]=="__cur__") $babelConfig["version"]=explode("-",version())[0]; |
||
| 27 | if($babelConfig["require"]["NOJ"]=="__cur__") $babelConfig["require"]["NOJ"]=explode("-",version())[0]; |
||
| 28 | |||
| 29 | // check version info |
||
| 30 | if(!isset($babelConfig["version"]) || is_null($babelConfig["version"]) || trim($babelConfig["version"])==""){ |
||
| 31 | $this->command->line("\n <bg=red;fg=white> Lack version info, aborting. </>\n"); |
||
| 32 | return; |
||
| 33 | } |
||
| 34 | |||
| 35 | // check require info |
||
| 36 | if(!isset($babelConfig["require"]["NOJ"]) || is_null($babelConfig["require"]["NOJ"]) || trim($babelConfig["require"]["NOJ"])==""){ |
||
| 37 | $this->command->line("\n <bg=red;fg=white> Lack NOJ compability info, aborting. </>\n"); |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | // check requirement |
||
| 42 | try { |
||
| 43 | if(!($this->versionParser->parse($babelConfig["require"]["NOJ"])->complies(new Version(explode("-",version())[0])))){ |
||
| 44 | $this->command->line("Your Current NOJ Version <fg=yellow>".version()."</> doesn't support the following extension: "); |
||
| 45 | $this->command->line(" - <fg=green>$ocode</> requires NOJ version <fg=yellow>{$babelConfig['require']['NOJ']}</>"); |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | } catch(InvalidVersionException $e) { |
||
| 49 | $this->command->line("\n <bg=red;fg=white> Illegal Compability Info, aborting. </>\n"); |
||
| 50 | return; |
||
| 51 | } |
||
| 52 | |||
| 53 | DB::beginTransaction(); |
||
| 54 | |||
| 55 | // get current installed version info |
||
| 56 | $info=OJModel::basic(OJModel::oid($ocode)); |
||
| 57 | |||
| 58 | // if there isn't, create one |
||
| 59 | if(empty($info)){ |
||
| 60 | $this->oid=OJModel::insertOJ([ |
||
| 61 | "ocode"=>$babelConfig["code"], |
||
| 62 | "name"=>$babelConfig["name"], |
||
| 63 | "home_page"=>$babelConfig["website"], |
||
| 64 | "logo"=>"/static/img/oj/default.png", |
||
| 65 | "status"=>1, |
||
| 66 | "version"=>"", |
||
| 67 | "compiler_timestamp"=>"", |
||
| 68 | ]); |
||
| 69 | } else { |
||
| 70 | // check legal version format |
||
| 71 | try { |
||
| 72 | $currentVersion=new Version($babelConfig["version"]); |
||
| 73 | } catch(InvalidVersionException $e) { |
||
| 74 | DB::rollback(); |
||
| 75 | $this->command->line("\n <bg=red;fg=white> Illegal Version Info, aborting. </>\n"); |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | // check there is a not null version |
||
| 80 | if(isset($info["version"]) && !is_null($info["version"]) && trim($info["version"])!=""){ |
||
| 81 | try { |
||
| 82 | $installedVersion=new Version($info["version"]); |
||
| 83 | } catch(InvalidVersionException $e) { |
||
| 84 | DB::rollback(); |
||
| 85 | $this->command->line("\n <bg=red;fg=white> Illegal Version Info, aborting. </>\n"); |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!($currentVersion->isGreaterThan($installedVersion))) { |
||
| 90 | // lower version or even |
||
| 91 | DB::rollback(); |
||
| 92 | $this->command->line("Nothing to install or update"); |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->oid=$info["oid"]; |
||
| 98 | } |
||
| 99 | |||
| 100 | // retrieve compiler config and then import it |
||
| 101 | $installed_timestamp=0; |
||
| 102 | if(isset($info["compiler_timestamp"]) && !is_null($info["compiler_timestamp"]) && trim($info["compiler_timestamp"])!=""){ |
||
| 103 | $installed_timestamp=intval($info["compiler_timestamp"]); |
||
| 104 | } |
||
| 105 | $ConpilerConfig = glob(babel_path("Extension/$ocode/compiler/*.*")); |
||
| 106 | foreach($ConpilerConfig as $file) { |
||
| 107 | if(intval(basename($file)) > $installed_timestamp) { |
||
| 108 | try { |
||
| 109 | $this->commitCompiler($file,json_decode(file_get_contents($file), true)); |
||
| 110 | } catch (Exception $e) { |
||
| 111 | DB::rollback(); |
||
| 112 | $this->command->line("<fg=red>Error: ".$e->getMessage()."</>"); |
||
| 113 | $this->command->line("\n <bg=red;fg=white> Compiler info import failure, aborting. </>\n"); |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // import css |
||
| 120 | try { |
||
| 121 | if (isset($babelConfig["custom"]["css"]) && !is_null($babelConfig["custom"]["css"]) && trim($babelConfig["custom"]["css"])!="") { |
||
| 122 | $cssPath=babel_path("Extension/$ocode/".$babelConfig["custom"]["css"]); |
||
| 123 | }else{ |
||
| 124 | $cssPath=null; |
||
| 125 | } |
||
| 126 | $this->applyCustom($ocode, $cssPath); |
||
| 127 | }catch(Exception $e){ |
||
| 128 | DB::rollback(); |
||
| 129 | $this->command->line("\n <bg=red;fg=white> Unable to add an custom css for this extension, aborting. </>\n"); |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | // import icon |
||
| 135 | try{ |
||
| 136 | $imgPath=babel_path("Extension/$ocode/".$babelConfig["icon"]); |
||
| 137 | $storePath=$this->applyIcon($ocode, $imgPath); |
||
| 138 | OJModel::updateOJ($this->oid,["logo"=>$storePath]); |
||
| 139 | }catch(Exception $e){ |
||
| 140 | DB::rollback(); |
||
| 141 | $this->command->line("\n <bg=red;fg=white> Unable to add an icon for this extension, aborting. </>\n"); |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | |||
| 145 | DB::commit(); |
||
| 146 | } |
||
| 245 |