File   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 122
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 9 1
A getFilesByCategory() 0 8 2
A __construct() 0 4 1
A getTemplateData() 0 12 2
A hasAnyChangedSince() 0 3 1
A getCategoryHtml() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Template\Loader;
6
7
use AbterPhp\Files\Constant\Route;
8
use AbterPhp\Framework\Html\Helper\StringHelper;
9
use AbterPhp\Framework\Template\IData;
10
use AbterPhp\Framework\Template\ILoader;
11
use AbterPhp\Framework\Template\Data;
12
use AbterPhp\Files\Domain\Entities\File as Entity;
13
use AbterPhp\Files\Orm\FileRepo;
14
use AbterPhp\Framework\Template\ParsedTemplate;
15
use Opulence\Routing\Urls\UrlGenerator;
16
17
class File implements ILoader
18
{
19
    const TAG_A  = 'a';
20
    const TAG_LI = 'li';
21
    const TAG_UL = 'ul';
22
23
    const ATTRIBUTE_CLASS = 'class';
24
    const ATTRIBUTE_HREF  = 'href';
25
26
    const FILE_CATEGORY_CLASS = 'file-category';
27
28
    /** @var FileRepo */
29
    protected $fileRepo;
30
31
    /** @var UrlGenerator */
32
    protected $urlGenerator;
33
34
    /**
35
     * File constructor.
36
     *
37
     * @param FileRepo     $fileRepo
38
     * @param UrlGenerator $urlGenerator
39
     */
40
    public function __construct(FileRepo $fileRepo, UrlGenerator $urlGenerator)
41
    {
42
        $this->fileRepo     = $fileRepo;
43
        $this->urlGenerator = $urlGenerator;
44
    }
45
46
    /**
47
     * @param ParsedTemplate[] $parsedTemplates
48
     *
49
     * @return IData[]
50
     */
51
    public function load(array $parsedTemplates): array
52
    {
53
        $identifiers = array_keys($parsedTemplates);
54
55
        $files = $this->fileRepo->getPublicByCategoryIdentifiers($identifiers);
56
57
        $filesByCategories = $this->getFilesByCategory($files);
58
59
        return $this->getTemplateData($filesByCategories);
60
    }
61
62
    /**
63
     * @param Entity[] $files
64
     *
65
     * @return Entity[][]
66
     */
67
    protected function getFilesByCategory(array $files): array
68
    {
69
        $return = [];
70
        foreach ($files as $file) {
71
            $return[$file->getCategory()->getIdentifier()][] = $file;
72
        }
73
74
        return $return;
75
    }
76
77
    /**
78
     * @param Entity[][] $files
79
     *
80
     * @return IData[]
81
     */
82
    protected function getTemplateData(array $files): array
83
    {
84
        $templateData = [];
85
        foreach ($files as $categoryIdentifier => $categoryFiles) {
86
            $templateData[] = new Data(
87
                $categoryIdentifier,
88
                [],
89
                ['body' => $this->getCategoryHtml($categoryFiles, $categoryIdentifier)]
90
            );
91
        }
92
93
        return $templateData;
94
    }
95
96
    /**
97
     * @param Entity[] $files
98
     * @param string   $categoryIdentifier
99
     *
100
     * @return string
101
     */
102
    protected function getCategoryHtml(array $files, string $categoryIdentifier): string
103
    {
104
        $html = [];
105
        foreach ($files as $file) {
106
            // @phan-suppress-next-line PhanTypeMismatchArgument
107
            $url  = $this->urlGenerator->createFromName(Route::PUBLIC_FILE, $file->getFilesystemName());
108
            $link = StringHelper::wrapInTag(
109
                $file->getPublicName(),
110
                static::TAG_A,
111
                [
112
                    static::ATTRIBUTE_HREF => $url,
113
                ]
114
            );
115
116
            $html[] = StringHelper::wrapInTag($link, static::TAG_LI);
117
        }
118
119
        return StringHelper::wrapInTag(
120
            implode('', $html),
121
            static::TAG_UL,
122
            [
123
                static::ATTRIBUTE_CLASS => [static::FILE_CATEGORY_CLASS, $categoryIdentifier],
124
            ]
125
        );
126
    }
127
128
    /**
129
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
130
     *
131
     * @param string[] $identifiers
132
     * @param string   $cacheTime
133
     *
134
     * @return bool
135
     */
136
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
137
    {
138
        return true;
139
    }
140
}
141