Passed
Push — master ( 8f0f73...d2306b )
by Peter
02:30
created

PageCategory::groupPages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Template\Loader;
6
7
use AbterPhp\Framework\Exception\Config;
8
use AbterPhp\Framework\Template\IBuilder;
9
use AbterPhp\Framework\Template\IData;
10
use AbterPhp\Framework\Template\ILoader;
11
use AbterPhp\Framework\Template\ParsedTemplate;
12
use AbterPhp\Website\Databases\Queries\PageCategoryCache;
13
use AbterPhp\Website\Domain\Entities\Page;
14
use AbterPhp\Website\Orm\PageRepo;
15
use Opulence\Orm\OrmException;
16
use Opulence\QueryBuilders\InvalidQueryException;
17
18
class PageCategory implements ILoader
19
{
20
    /**
21
     * @var PageRepo
22
     */
23
    protected $pageRepo;
24
25
    /**
26
     * @var PageCategoryCache
27
     */
28
    protected $cache;
29
30
    /**
31
     * @var IBuilder[]
32
     */
33
    protected $builders;
34
35
    /**
36
     * PageCategory constructor.
37
     *
38
     * @param PageRepo          $pageRepo
39
     * @param PageCategoryCache $pageCategoryCache
40
     * @param IBuilder[]        $builders
41
     */
42
    public function __construct(PageRepo $pageRepo, PageCategoryCache $pageCategoryCache, array $builders)
43
    {
44
        $this->pageRepo = $pageRepo;
45
        $this->cache    = $pageCategoryCache;
46
        $this->builders = $builders;
47
    }
48
49
    /**
50
     * @param string   $name
51
     * @param IBuilder $builder
52
     *
53
     * @return $this
54
     */
55
    public function addBuilder(string $name, IBuilder $builder): self
56
    {
57
        $this->builders[$name] = $builder;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param array<string,ParsedTemplate[]> $parsedTemplates
64
     *
65
     * @return IData[]
66
     * @throws OrmException
67
     */
68
    public function load(array $parsedTemplates): array
69
    {
70
        $identifiers = array_keys($parsedTemplates);
71
72
        $groupedPages = $this->loadPages($identifiers);
73
74
        $templateData = $this->createTemplateData($parsedTemplates, $groupedPages);
75
76
        return $templateData;
77
    }
78
79
    /**
80
     * @param string[] $identifiers
81
     *
82
     * @return array<string,Page[]>
83
     */
84
    protected function loadPages(array $identifiers): array
85
    {
86
        $pages = $this->pageRepo->getByCategoryIdentifiers($identifiers);
87
88
        $groupedPages = [];
89
        foreach ($pages as $page) {
90
            $groupedPages[$page->getCategory()->getIdentifier()][] = $page;
91
        }
92
93
        return $groupedPages;
94
    }
95
96
    /**
97
     * @param array<string,ParsedTemplate[]> $parsedTemplates
98
     * @param Page[][]                       $groupedPages
99
     *
100
     * @return IData[]
101
     */
102
    protected function createTemplateData(array $parsedTemplates, array $groupedPages): array
103
    {
104
        $templateData = [];
105
        foreach ($parsedTemplates as $identifier => $identifierTemplates) {
106
            foreach ($identifierTemplates as $parsedTemplate) {
107
                if (!array_key_exists($identifier, $groupedPages)) {
108
                    continue;
109
                }
110
111
                $pages = $groupedPages[$identifier];
112
113
                $builderName = $parsedTemplate->getAttribute('builder');
114
                if ($builderName && array_key_exists($builderName, $this->builders)) {
115
                    $templateData[] = $this->builders[$builderName]->build($pages);
116
117
                    continue;
118
                }
119
120
                throw new Config(__CLASS__);
121
            }
122
        }
123
124
        return $templateData;
125
    }
126
127
    /**
128
     * @param string[] $identifiers
129
     * @param string   $cacheTime
130
     *
131
     * @return bool
132
     * @throws InvalidQueryException
133
     */
134
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
135
    {
136
        return $this->cache->hasAnyChangedSince($identifiers, $cacheTime);
137
    }
138
}
139