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