| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 107 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 84 | public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string |
||
| 85 | { |
||
| 86 | $request = app(ServerRequestInterface::class); |
||
| 87 | $default_start = $this->getBlockSetting($block_id, 'start'); |
||
| 88 | $filter_links = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); |
||
| 89 | $controls = $this->getBlockSetting($block_id, 'controls', '1'); |
||
| 90 | $start = (bool) ($request->getQueryParams()['start'] ?? $default_start); |
||
| 91 | |||
| 92 | $filter_types = [ |
||
| 93 | $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, |
||
| 94 | $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, |
||
| 95 | $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, |
||
| 96 | $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, |
||
| 97 | $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, |
||
| 98 | $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, |
||
| 99 | $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, |
||
| 100 | $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, |
||
| 101 | $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, |
||
| 102 | $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, |
||
| 103 | $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, |
||
| 104 | $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, |
||
| 105 | $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, |
||
| 106 | $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, |
||
| 107 | $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, |
||
| 108 | $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, |
||
| 109 | $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, |
||
| 110 | $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, |
||
| 111 | ]; |
||
| 112 | |||
| 113 | $filter_types = array_filter($filter_types); |
||
| 114 | |||
| 115 | // The type "other" includes media without a type. |
||
| 116 | if (in_array('other', $filter_types, true)) { |
||
| 117 | $filter_types[] = ''; |
||
| 118 | } |
||
| 119 | |||
| 120 | // We can apply the filters using SQL, but it is more efficient to shuffle in PHP. |
||
| 121 | $random_row = DB::table('media') |
||
| 122 | ->join('media_file', static function (JoinClause $join): void { |
||
| 123 | $join |
||
| 124 | ->on('media_file.m_file', '=', 'media.m_file') |
||
| 125 | ->on('media_file.m_id', '=', 'media.m_id'); |
||
| 126 | }) |
||
| 127 | ->where('media.m_file', '=', $tree->id()) |
||
| 128 | ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp']) |
||
| 129 | ->whereIn('media_file.source_media_type', $filter_types) |
||
| 130 | ->select('media.*') |
||
| 131 | ->get() |
||
| 132 | ->shuffle() |
||
| 133 | ->first(function (object $row) use ($filter_links, $tree): bool { |
||
| 134 | $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom); |
||
| 135 | assert($media instanceof Media); |
||
| 136 | |||
| 137 | if (!$media->canShow() || $media->firstImageFile() === null) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach ($this->linked_record_service->linkedIndividuals($media) as $individual) { |
||
| 142 | switch ($filter_links) { |
||
| 143 | case self::LINK_ALL: |
||
| 144 | return true; |
||
| 145 | |||
| 146 | case self::LINK_INDIVIDUAL: |
||
| 147 | return str_contains($individual->gedcom(), "\n1 OBJE @" . $media->xref() . '@'); |
||
| 148 | |||
| 149 | case self::LINK_EVENT: |
||
| 150 | return str_contains($individual->gedcom(), "\n2 OBJE @" . $media->xref() . '@'); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return false; |
||
| 155 | }); |
||
| 156 | |||
| 157 | $random_media = null; |
||
| 158 | |||
| 159 | if ($random_row !== null) { |
||
| 160 | $random_media = Registry::mediaFactory()->make($random_row->m_id, $tree, $random_row->m_gedcom); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($random_media instanceof Media) { |
||
| 164 | $content = view('modules/random_media/slide-show', [ |
||
| 165 | 'block_id' => $block_id, |
||
| 166 | 'delay' => self::DELAY, |
||
| 167 | 'linked_families' => $this->linked_record_service->linkedFamilies($random_media), |
||
| 168 | 'linked_individuals' => $this->linked_record_service->linkedIndividuals($random_media), |
||
| 169 | 'linked_sources' => $this->linked_record_service->linkedSources($random_media), |
||
| 170 | 'media' => $random_media, |
||
| 171 | 'media_file' => $random_media->firstImageFile(), |
||
| 172 | 'show_controls' => $controls, |
||
| 173 | 'start_automatically' => $start, |
||
| 174 | 'tree' => $tree, |
||
| 175 | ]); |
||
| 176 | } else { |
||
| 177 | $content = I18N::translate('This family tree has no images to display.'); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($context !== self::CONTEXT_EMBED) { |
||
| 181 | return view('modules/block-template', [ |
||
| 182 | 'block' => Str::kebab($this->name()), |
||
| 183 | 'id' => $block_id, |
||
| 184 | 'config_url' => $this->configUrl($tree, $context, $block_id), |
||
| 185 | 'title' => $this->title(), |
||
| 186 | 'content' => $content, |
||
| 187 | ]); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $content; |
||
| 191 | } |
||
| 317 |