Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
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 / 43
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 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 newsDetail
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 all
0.00% covered (danger)
0.00%
0 / 37
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        $httpUtils->countRequest($request, ['filter', 'von']);
28        $out = $olzNewsList->getHtml([]);
29        return new Response($out);
30    }
31
32    #[Route('/news/{id}', requirements: ['id' => '\d+'])]
33    public function newsDetail(
34        Request $request,
35        LoggerInterface $logger,
36        HttpUtils $httpUtils,
37        OlzNewsDetail $olzNewsDetail,
38        int $id,
39    ): Response {
40        $httpUtils->countRequest($request, ['von']);
41        $out = $olzNewsDetail->getHtml(['id' => $id]);
42        return new Response($out);
43    }
44
45    #[Route('/news/{id}/all.zip', requirements: ['id' => '\d+'])]
46    public function all(
47        Request $request,
48        LoggerInterface $logger,
49        HttpUtils $httpUtils,
50        int $id,
51    ): Response {
52        $httpUtils->countRequest($request);
53        if (!$this->authUtils()->hasPermission('any')) {
54            throw new NotFoundHttpException();
55        }
56
57        $news_repo = $this->entityManager()->getRepository(NewsEntry::class);
58        $news_entry = $news_repo->findOneBy(['id' => $id]);
59        if (!$news_entry) {
60            throw new NotFoundHttpException();
61        }
62        $image_ids = $news_entry->getImageIds();
63
64        $data_path = $this->envUtils()->getDataPath();
65        $imgdir = "{$data_path}img/news/{$id}/img/";
66        if (!is_dir($imgdir)) {
67            throw new NotFoundHttpException("No such image directory: {$imgdir}");
68        }
69
70        $this->log()->info("Downloading all images zip file for news/{$id}");
71        $zip_id = $this->uploadUtils()->getRandomUploadId('.zip');
72        $zip_path = "{$data_path}temp/{$zip_id}";
73        $zip = new \ZipArchive();
74        if ($zip->open($zip_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
75            $this->log()->error("Could not create ZIP file: {$zip_path}");
76            throw new \Exception("Could not create ZIP file: {$zip_path}");
77        }
78        $num_images = count($image_ids);
79        for ($i = 0; $i < $num_images; $i++) {
80            $image_id = $image_ids[$i];
81            $file_path = "{$imgdir}{$image_id}";
82            if (is_file($file_path)) {
83                $index = $i + 1;
84                $pad_len = intval(ceil(log10($num_images)));
85                $name = str_pad("{$index}", $pad_len, '0', STR_PAD_LEFT);
86                $zip->addFile($file_path, "{$name}.jpg");
87            } else {
88                $this->log()->warning("Missing image in news/{$id}{$file_path}");
89            }
90        }
91        if ($zip->status != \ZipArchive::ER_OK) {
92            $this->log()->error("Could write files to ZIP: {$imgdir}*.jpg");
93            throw new \Exception("Could write files to ZIP: {$imgdir}*.jpg");
94        }
95        $zip->close();
96
97        $response = new Response(file_get_contents($zip_path) ?: null);
98        $response->headers->set('Content-Type', 'application/zip');
99        $response->headers->set('Content-Disposition', "attachment;filename=Fotos {$news_entry->getTitle()}.zip");
100        return $response;
101    }
102}