Completed
Push — develop ( 4303fc...d099e7 )
by Tino
06:36
created

ACP3/Files/Controller/Widget/Index/Index.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Copyright (c) by the ACP3 Developers.
5
 * See the LICENSE file at the top-level module directory for licensing details.
6
 */
7
8
namespace ACP3\Modules\ACP3\Files\Controller\Widget\Index;
9
10
use ACP3\Core;
11
use ACP3\Modules\ACP3\Categories\Model\Repository\CategoryRepository;
12
use ACP3\Modules\ACP3\Files;
13
use ACP3\Modules\ACP3\System\Installer\Schema;
14
15
class Index extends Core\Controller\AbstractWidgetAction
16
{
17
    use Core\Cache\CacheResponseTrait;
18
19
    /**
20
     * @var \ACP3\Core\Date
21
     */
22
    protected $date;
23
    /**
24
     * @var \ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository
25
     */
26
    protected $filesRepository;
27
    /**
28
     * @var \ACP3\Modules\ACP3\Categories\Model\Repository\CategoryRepository
29
     */
30
    private $categoryRepository;
31
32
    public function __construct(
33
        Core\Controller\Context\WidgetContext $context,
34
        Core\Date $date,
35
        Files\Model\Repository\FilesRepository $filesRepository,
36
        CategoryRepository $categoryRepository
37
    ) {
38
        parent::__construct($context);
39
40
        $this->date = $date;
41
        $this->filesRepository = $filesRepository;
42
        $this->categoryRepository = $categoryRepository;
43
    }
44
45
    /**
46
     * @param int|null $limit
47
     * @param int      $categoryId
48
     * @param string   $template
49
     *
50
     * @return array
51
     *
52
     * @throws \Doctrine\DBAL\DBALException
53
     */
54
    public function execute(?int $limit = null, ?int $categoryId = null, string $template = '')
55
    {
56
        $this->setCacheResponseCacheable($this->config->getSettings(Schema::MODULE_NAME)['cache_lifetime']);
57
58
        $this->setTemplate(\urldecode($template));
59
60
        return [
61
            'category' => $this->categoryRepository->getOneById($categoryId),
0 ignored issues
show
It seems like $categoryId can also be of type null; however, parameter $entryId of ACP3\Modules\ACP3\Catego...epository::getOneById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            'category' => $this->categoryRepository->getOneById(/** @scrutinizer ignore-type */ $categoryId),
Loading history...
62
            'sidebar_files' => $this->fetchFiles($categoryId, $limit),
63
        ];
64
    }
65
66
    /**
67
     * @param int|null $categoryId
68
     * @param int|null $limit
69
     *
70
     * @return array
71
     *
72
     * @throws \Doctrine\DBAL\DBALException
73
     */
74
    private function fetchFiles(?int $categoryId, ?int $limit): array
75
    {
76
        $settings = $this->config->getSettings(Files\Installer\Schema::MODULE_NAME);
77
78
        if (!empty($categoryId)) {
79
            return $this->filesRepository->getAllByCategoryId(
80
                $categoryId,
81
                $this->date->getCurrentDateTime(),
82
                $limit ?? $settings['sidebar']
83
            );
84
        }
85
86
        return $this->filesRepository->getAll(
87
            $this->date->getCurrentDateTime(),
88
            $limit ?? $settings['sidebar']
89
        );
90
    }
91
}
92