Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewsController
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 newsList
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 newsDetail
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 all
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3namespace Olz\Controller;
4
5use Olz\Entity\News\NewsEntry;
6use Olz\News\Components\OlzNewsDetail\OlzNewsDetail;
7use Olz\News\Components\OlzNewsList\OlzNewsList;
8use Olz\Utils\HttpUtils;
9use Olz\Utils\WithUtilsTrait;
10use Psr\Log\LoggerInterface;
11use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12use Symfony\Component\HttpFoundation\Request;
13use Symfony\Component\HttpFoundation\Response;
14use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15use Symfony\Component\Routing\Annotation\Route;
16
17class NewsController extends AbstractController {
18    use WithUtilsTrait;
19
20    #[Route('/news')]
21    public function newsList(
22        Request $request,
23        LoggerInterface $logger,
24        HttpUtils $httpUtils,
25        OlzNewsList $olzNewsList,
26    ): Response {
27        return $httpUtils->measure($request, ['filter', 'von'], function () use ($httpUtils, $request, $olzNewsList) {
28            $httpUtils->stripParams($request, ['von']);
29            $out = $olzNewsList->getHtml([]);
30            return new Response($out);
31        });
32    }
33
34    #[Route('/news/{id}', requirements: ['id' => '\d+'])]
35    public function newsDetail(
36        Request $request,
37        LoggerInterface $logger,
38        HttpUtils $httpUtils,
39        OlzNewsDetail $olzNewsDetail,
40        int $id,
41    ): Response {
42        return $httpUtils->measure($request, ['von'], function () use ($httpUtils, $request, $olzNewsDetail, $id) {
43            $httpUtils->stripParams($request, ['von']);
44            $out = $olzNewsDetail->getHtml(['id' => $id]);
45            return new Response($out);
46        });
47    }
48
49    #[Route('/news/{id}/all.zip', requirements: ['id' => '\d+'])]
50    public function all(
51        Request $request,
52        LoggerInterface $logger,
53        HttpUtils $httpUtils,
54        int $id,
55    ): Response {
56        return $httpUtils->measure($request, [], function () use ($id) {
57            if (!$this->authUtils()->hasPermission('any')) {
58                throw new NotFoundHttpException();
59            }
60
61            $news_repo = $this->entityManager()->getRepository(NewsEntry::class);
62            $news_entry = $news_repo->findOneBy(['id' => $id]);
63            if (!$news_entry) {
64                throw new NotFoundHttpException();
65            }
66            $image_ids = $news_entry->getImageIds();
67
68            $data_path = $this->envUtils()->getDataPath();
69            $imgdir = "{$data_path}img/news/{$id}/img/";
70            if (!is_dir($imgdir)) {
71                throw new NotFoundHttpException("No such image directory: {$imgdir}");
72            }
73
74            $this->log()->info("Downloading all images zip file for news/{$id}");
75            $zip_id = $this->uploadUtils()->getRandomUploadId('.zip');
76            $zip_path = "{$data_path}temp/{$zip_id}";
77            $zip = new \ZipArchive();
78            if ($zip->open($zip_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
79                $this->log()->error("Could not create ZIP file: {$zip_path}");
80                throw new \Exception("Could not create ZIP file: {$zip_path}");
81            }
82            $num_images = count($image_ids);
83            for ($i = 0; $i < $num_images; $i++) {
84                $image_id = $image_ids[$i];
85                $file_path = "{$imgdir}{$image_id}";
86                if (is_file($file_path)) {
87                    $index = $i + 1;
88                    $pad_len = intval(ceil(log10($num_images)));
89                    $name = str_pad("{$index}", $pad_len, '0', STR_PAD_LEFT);
90                    $zip->addFile($file_path, "{$name}.jpg");
91                } else {
92                    $this->log()->warning("Missing image in news/{$id}{$file_path}");
93                }
94            }
95            if ($zip->status != \ZipArchive::ER_OK) {
96                $this->log()->error("Could write files to ZIP: {$imgdir}*.jpg");
97                throw new \Exception("Could write files to ZIP: {$imgdir}*.jpg");
98            }
99            $zip->close();
100
101            $response = new Response(file_get_contents($zip_path) ?: null);
102            $response->headers->set('Content-Type', 'application/zip');
103            $response->headers->set('Content-Disposition', "attachment;filename=Fotos {$news_entry->getTitle()}.zip");
104            return $response;
105        });
106    }
107}