| Total Complexity | 55 |
| Total Lines | 399 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Certificate 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 Certificate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Certificate extends Media { |
||
| 37 | |||
| 38 | const URL_PREFIX = 'module.php?mod=myartjaub_certificates&mod_action=Certificate&cid='; |
||
| 39 | |||
| 40 | /** @var string The "TITL" value from the GEDCOM |
||
| 41 | * This is a tweak to overcome the private level from the parent object... |
||
| 42 | */ |
||
| 43 | protected $title = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Certificate provider |
||
| 47 | * @var CertificateProviderInterface $provider |
||
| 48 | */ |
||
| 49 | protected $provider = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Certificate type |
||
| 53 | * @var string $certType |
||
| 54 | */ |
||
| 55 | protected $certType = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Certificate date |
||
| 59 | * @var (null|Date) $certDate |
||
| 60 | */ |
||
| 61 | protected $certDate = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Certificate description |
||
| 65 | * @var unknown $certDetails |
||
|
|
|||
| 66 | */ |
||
| 67 | protected $certDetails = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Certificate source |
||
| 71 | * @var unknown $source |
||
| 72 | */ |
||
| 73 | protected $source = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Extends the Media constructor. |
||
| 77 | * Create a certificate from the file path |
||
| 78 | * @param string $data |
||
| 79 | * @param Tree $tree Reference tree |
||
| 80 | * @param CertificateProviderInterface $provider |
||
| 81 | */ |
||
| 82 | public function __construct($data, Tree $tree, CertificateProviderInterface $provider) { |
||
| 83 | $this->provider = $provider; |
||
| 84 | // Data is only the file name |
||
| 85 | $data = str_replace("\\", '/', $data); |
||
| 86 | $xref = Functions::encryptToSafeBase64($data); |
||
| 87 | $gedcom = sprintf( |
||
| 88 | '0 @%1$s@ OBJE'.PHP_EOL. |
||
| 89 | '1 FILE %2$s', |
||
| 90 | $xref, $data |
||
| 91 | ); |
||
| 92 | parent::__construct($xref, $gedcom, '', $tree); |
||
| 93 | |||
| 94 | $this->title = basename($this->getFilename(), '.'.$this->extension()); |
||
| 95 | |||
| 96 | $match = null; |
||
| 97 | $ct = preg_match("/(?<year>\d{1,4})(\.(?<month>\d{1,2}))?(\.(?<day>\d{1,2}))?( (?<type>[A-Z]{1,2}) )?(?<details>.*)/", $this->title, $match); |
||
| 98 | if($ct > 0){ |
||
| 99 | $monthId = (int) $match['month']; |
||
| 100 | $calendarShortMonths = Functions::getCalendarShortMonths(); |
||
| 101 | $monthShortName = array_key_exists($monthId, $calendarShortMonths) ? $calendarShortMonths[$monthId] : $monthId; |
||
| 102 | $this->certDate = new Date($match['day'].' '.strtoupper($monthShortName).' '.$match['year']); |
||
| 103 | $this->certType = $match['type']; |
||
| 104 | $this->certDetails = $match['details']; |
||
| 105 | } else { |
||
| 106 | $this->certDetails = $this->title; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inhericDoc} |
||
| 112 | * @see \Fisharebest\Webtrees\GedcomRecord::getInstance() |
||
| 113 | */ |
||
| 114 | static public function getInstance($xref, Tree $tree, $gedcom = null, CertificateProviderInterface $provider = null) { |
||
| 115 | try{ |
||
| 116 | $certfile = Functions::decryptFromSafeBase64($xref); |
||
| 117 | |||
| 118 | //NEED TO CHECK THAT !!! |
||
| 119 | if(Functions::isValidPath($certfile, true)) { |
||
| 120 | return new Certificate($certfile, $tree, $provider); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | catch (\Exception $ex) { |
||
| 124 | Log::addErrorLog('Certificate module error : > '.$ex->getMessage().' < with data > '.$xref.' <'); |
||
| 125 | } |
||
| 126 | |||
| 127 | return null; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inhericDoc} |
||
| 132 | * @see \Fisharebest\Webtrees\Media::canShowByType() |
||
| 133 | */ |
||
| 134 | protected function canShowByType($access_level) { |
||
| 135 | $linked_indis = $this->linkedIndividuals('_ACT'); |
||
| 136 | foreach ($linked_indis as $linked_indi) { |
||
| 137 | if ($linked_indi && !$linked_indi->canShow($access_level)) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $linked_fams = $this->linkedFamilies('_ACT'); |
||
| 142 | foreach ($linked_fams as $linked_fam) { |
||
| 143 | if ($linked_fam && !$linked_fam->canShow($access_level)) { |
||
| 144 | return false; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return count($linked_indis) + count($linked_fams) > 0; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Define a source associated with the certificate |
||
| 153 | * |
||
| 154 | * @param string|Source $xref |
||
| 155 | */ |
||
| 156 | public function setSource($xref){ |
||
| 157 | if($xref instanceof Source){ |
||
| 158 | $this->source = $xref; |
||
| 159 | } else { |
||
| 160 | $this->source = Source::getInstance($xref, $this->tree); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritDoc} |
||
| 166 | * @see \Fisharebest\Webtrees\Media::getTitle() |
||
| 167 | */ |
||
| 168 | public function getTitle() { |
||
| 169 | return $this->title; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the certificate date |
||
| 174 | * |
||
| 175 | * @return Date Certificate date |
||
| 176 | */ |
||
| 177 | public function getCertificateDate(){ |
||
| 178 | return $this->certDate; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the type of certificate |
||
| 183 | * |
||
| 184 | * @return string Certificate date |
||
| 185 | */ |
||
| 186 | public function getCertificateType(){ |
||
| 187 | return $this->certType; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the details of the certificate (basename without the date and type) |
||
| 192 | * |
||
| 193 | * @return string Certificate details |
||
| 194 | */ |
||
| 195 | public function getCertificateDetails(){ |
||
| 196 | return $this->certDetails; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return the city the certificate comes from |
||
| 201 | * |
||
| 202 | * @return string|NULL Certificate city |
||
| 203 | */ |
||
| 204 | public function getCity(){ |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inhericDoc} |
||
| 212 | * @see \Fisharebest\Webtrees\Media::getServerFilename() |
||
| 213 | */ |
||
| 214 | public function getServerFilename($which='main') { |
||
| 215 | $filename = $this->provider->getRealCertificatesDirectory() . $this->getFilename(); |
||
| 216 | return Functions::encodeUtf8ToFileSystem($filename); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inhericDoc} |
||
| 221 | * @see \Fisharebest\Webtrees\Media::getHtmlUrlDirect() |
||
| 222 | */ |
||
| 223 | public function getHtmlUrlDirect($which = 'main', $download = false) { |
||
| 224 | $sidstr = ($this->source) ? '&sid='.$this->source->getXref() : ''; |
||
| 225 | return |
||
| 226 | 'module.php?mod='. \MyArtJaub\Webtrees\Constants::MODULE_MAJ_CERTIF_NAME . |
||
| 227 | '&mod_action=Certificate@image' . |
||
| 228 | '&ged='. $this->tree->getNameUrl() . |
||
| 229 | '&cid=' . $this->getXref() . $sidstr . |
||
| 230 | '&cb=' . $this->getEtag($which); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the watermark text to be displayed. |
||
| 235 | * If a source ID has been provided with the certificate, use this image, |
||
| 236 | * otherwise try to find a linked source within the GEDCOM (the first occurence found is used). |
||
| 237 | * Else a default text is used. |
||
| 238 | * |
||
| 239 | * @return string Watermark text |
||
| 240 | */ |
||
| 241 | public function getWatermarkText(){ |
||
| 242 | $module = Module::getModuleByName(Constants::MODULE_MAJ_CERTIF_NAME); |
||
| 243 | |||
| 244 | if($module) { |
||
| 245 | $wmtext = $module->getSetting('MAJ_WM_DEFAULT', I18N::translate('This image is protected under copyright law.')); |
||
| 246 | $sid= Filter::get('sid', WT_REGEX_XREF); |
||
| 247 | |||
| 248 | if($sid){ |
||
| 249 | $this->source = Source::getInstance($sid, $this->tree); |
||
| 250 | } |
||
| 251 | else{ |
||
| 252 | $this->fetchALinkedSource(); // the method already attach the source to the Certificate object; |
||
| 253 | } |
||
| 254 | |||
| 255 | if($this->source) { |
||
| 256 | $wmtext = '©'; |
||
| 257 | $repofact = $this->source->getFirstFact('REPO'); |
||
| 258 | if($repofact) { |
||
| 259 | $repo = $repofact->getTarget(); |
||
| 260 | if($repo && $repo instanceof Repository) $wmtext .= ' '.$repo->getFullName().' - '; |
||
| 261 | } |
||
| 262 | $wmtext .= $this->source->getFullName(); |
||
| 263 | } |
||
| 264 | return $wmtext; |
||
| 265 | } |
||
| 266 | return ''; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inhericDoc} |
||
| 271 | * @see \Fisharebest\Webtrees\Media::displayImage() |
||
| 272 | */ |
||
| 273 | public function displayImage($which = 'main') { |
||
| 274 | $js = ' if(isCertifColorboxActive == 0) { |
||
| 275 | activatecertifcolorbox(); |
||
| 276 | isCertifColorboxActive = 1; |
||
| 277 | } |
||
| 278 | '; |
||
| 279 | |||
| 280 | $script = ''; |
||
| 281 | $controller = Globals::getController(); |
||
| 282 | if($controller && !($controller instanceof IndividualController)){ |
||
| 283 | $controller->addInlineJavascript('$(document).ready(function() { '.$js.' });'); |
||
| 284 | } else { |
||
| 285 | $script = '<script>' . $js . '</script>'; |
||
| 286 | } |
||
| 287 | |||
| 288 | if ($which == 'icon' || !file_exists($this->getServerFilename())) { |
||
| 289 | // Use an icon |
||
| 290 | $image = |
||
| 291 | '<i dir="auto" class="icon-maj-certificate margin-h-2"' . |
||
| 292 | ' title="' . strip_tags($this->getFullName()) . '"' . |
||
| 293 | '></i>'; |
||
| 294 | } else { |
||
| 295 | $imgsize = getimagesize($this->getServerFilename()); |
||
| 296 | $image = |
||
| 297 | '<img' . |
||
| 298 | ' class ="'. 'certif_image' . '"' . |
||
| 299 | ' dir="' . 'auto' . '"' . // For the tool-tip |
||
| 300 | ' src="' . $this->getHtmlUrlDirect() . '"' . |
||
| 301 | ' alt="' . strip_tags($this->getFullName()) . '"' . |
||
| 302 | ' title="' . strip_tags($this->getFullName()) . '"' . |
||
| 303 | $imgsize[3] . // height="yyy" width="xxx" |
||
| 304 | '>'; |
||
| 305 | } |
||
| 306 | return |
||
| 307 | '<a' . |
||
| 308 | ' class="' . 'certgallery' . '"' . |
||
| 309 | ' href="' . $this->getHtmlUrlDirect() . '"' . |
||
| 310 | ' type="' . $this->mimeType() . '"' . |
||
| 311 | ' data-obje-url="' . $this->getHtmlUrl() . '"' . |
||
| 312 | ' data-title="' . strip_tags($this->getFullName()) . '"' . |
||
| 313 | '>' . $image . '</a>'.$script; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inhericDoc} |
||
| 318 | * @see \Fisharebest\Webtrees\GedcomRecord::linkedIndividuals() |
||
| 319 | */ |
||
| 320 | public function linkedIndividuals($link = '_ACT'){ |
||
| 321 | $rows = Database::prepare( |
||
| 322 | 'SELECT i_id AS xref, i_gedcom AS gedcom'. |
||
| 323 | ' FROM `##individuals`'. |
||
| 324 | ' WHERE i_file= :gedcom_id AND i_gedcom LIKE :gedcom') |
||
| 325 | ->execute(array( |
||
| 326 | 'gedcom_id' => $this->tree->getTreeId(), |
||
| 327 | 'gedcom' => '%_ACT '.$this->getFilename().'%' |
||
| 328 | ))->fetchAll(); |
||
| 329 | |||
| 330 | $list = array(); |
||
| 331 | foreach ($rows as $row) { |
||
| 332 | $record = Individual::getInstance($row->xref, $this->tree, $row->gedcom); |
||
| 333 | if ($record->canShowName()) { |
||
| 334 | $list[] = $record; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | return $list; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inhericDoc} |
||
| 342 | * @see \Fisharebest\Webtrees\GedcomRecord::linkedFamilies() |
||
| 343 | */ |
||
| 344 | public function linkedFamilies($link = '_ACT'){ |
||
| 345 | $rows = Database::prepare( |
||
| 346 | 'SELECT f_id AS xref, f_gedcom AS gedcom'. |
||
| 347 | ' FROM `##families`'. |
||
| 348 | ' WHERE f_file= :gedcom_id AND f_gedcom LIKE :gedcom') |
||
| 349 | ->execute(array( |
||
| 350 | 'gedcom_id' => $this->tree->getTreeId(), |
||
| 351 | 'gedcom' => '%_ACT '.$this->getFilename().'%' |
||
| 352 | ))->fetchAll(); |
||
| 353 | |||
| 354 | $list = array(); |
||
| 355 | foreach ($rows as $row) { |
||
| 356 | $record = Family::getInstance($row->xref, $this->tree, $row->gedcom); |
||
| 357 | if ($record->canShowName()) { |
||
| 358 | $list[] = $record; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | return $list; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a unique source linked to the certificate |
||
| 366 | * |
||
| 367 | * @return Source|NULL Linked source |
||
| 368 | */ |
||
| 369 | public function fetchALinkedSource(){ |
||
| 435 | } |
||
| 436 | |||
| 437 | } |
||
| 438 | |||
| 439 | ?> |
||
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths