Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.00% covered (danger)
35.00%
7 / 20
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
VideoHelper
35.00% covered (danger)
35.00%
7 / 20
33.33% covered (danger)
33.33%
1 / 3
5.47
0.00% covered (danger)
0.00%
0 / 1
 processVideoPlaceholders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 processYouTubePlaceholders
54.55% covered (warning)
54.55%
6 / 11
0.00% covered (danger)
0.00%
0 / 1
1.09
 generateGdprCompliantEmbed
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2// src/View/Helper/VideoHelper.php
3declare(strict_types=1);
4
5namespace App\View\Helper;
6
7use Cake\View\Helper;
8
9/**
10 * Video helper
11 *
12 * Handles processing of YouTube videos in article content
13 */
14class VideoHelper extends Helper
15{
16    /**
17     * List of helpers used by this helper
18     *
19     * @var array<string>
20     */
21    protected array $helpers = ['Html'];
22
23    /**
24     * Process video placeholders in content
25     *
26     * @param string $content The content containing video placeholders
27     * @param array $options Processing options
28     * @return string The processed content
29     */
30    public function processVideoPlaceholders(string $content, array $options = []): string
31    {
32        return $this->processYouTubePlaceholders($content);
33    }
34
35    /**
36     * Replace YouTube video placeholders with GDPR-compliant embed code
37     *
38     * @param string $content The content containing video placeholders
39     * @return string The processed content
40     */
41    public function processYouTubePlaceholders(string $content): string
42    {
43        return preg_replace_callback(
44            '/\[youtube:([a-zA-Z0-9_-]+):(\d+):(\d+):([^\]]*)\]/',
45            function ($matches) {
46                $videoId = $matches[1];
47                $width = $matches[2];
48                $height = $matches[3];
49                $title = $matches[4];
50
51                return $this->generateGdprCompliantEmbed($videoId, $width, $height, $title);
52            },
53            $content,
54        );
55    }
56
57    /**
58     * Generate GDPR-compliant embed code for YouTube videos
59     *
60     * @param string $videoId YouTube video ID
61     * @param string $width Video width
62     * @param string $height Video height
63     * @param string $title Video title
64     * @return string HTML for the video embed
65     */
66    protected function generateGdprCompliantEmbed(
67        string $videoId,
68        string $width,
69        string $height,
70        string $title,
71    ): string {
72        $thumbnailUrl = "https://img.youtube.com/vi/{$videoId}/maxresdefault.jpg";
73
74        return $this->getView()->element('site/youtube_embed', [
75            'videoId' => $videoId,
76            'width' => $width,
77            'height' => $height,
78            'title' => $title,
79            'thumbnailUrl' => $thumbnailUrl,
80        ]);
81    }
82}