Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.67% covered (danger)
41.67%
5 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GalleryHelper
41.67% covered (danger)
41.67%
5 / 12
50.00% covered (danger)
50.00%
1 / 2
9.96
0.00% covered (danger)
0.00%
0 / 1
 processGalleryPlaceholders
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 renderGalleryPlaceholder
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare(strict_types=1);
3
4namespace App\View\Helper;
5
6use Cake\View\Helper;
7use Exception;
8
9/**
10 * Gallery Helper
11 *
12 * Handles processing of gallery placeholders in article content using the Cell pattern.
13 * This helper follows CakePHP best practices by delegating data fetching to a Cell
14 * and focusing purely on placeholder processing.
15 */
16class GalleryHelper extends Helper
17{
18    /**
19     * Process gallery placeholders in content
20     *
21     * Replaces [gallery:id:theme:title] placeholders with rendered gallery cells.
22     * This method follows CakePHP conventions by delegating rendering to a Cell
23     * instead of fetching data directly in the view layer.
24     *
25     * @param string $content The content containing gallery placeholders
26     * @return string The processed content with rendered galleries
27     */
28    public function processGalleryPlaceholders(string $content): string
29    {
30        return preg_replace_callback(
31            '/\[gallery:([a-f0-9-]+):([^:]*):([^\]]*)\]/',
32            [$this, 'renderGalleryPlaceholder'],
33            $content,
34        );
35    }
36
37    /**
38     * Render a single gallery placeholder using Cell pattern
39     *
40     * @param array $matches Regex matches [full_match, gallery_id, theme, title]
41     * @return string HTML for the gallery or empty string if error
42     */
43    private function renderGalleryPlaceholder(array $matches): string
44    {
45        $galleryId = $matches[1];
46        $theme = $matches[2] ?: 'default';
47        $title = $matches[3] ?: '';
48
49        try {
50            // Use Cell pattern for proper MVC separation
51            // Cell handles data fetching, caching, and error handling
52            return (string)$this->getView()->cell('Gallery::display', [$galleryId, $theme, $title]);
53        } catch (Exception $e) {
54            // Log error and return empty string for graceful degradation
55            error_log("GalleryHelper: Error rendering gallery cell {$galleryId}" . $e->getMessage());
56
57            return '';
58        }
59    }
60}