Passed
Push — master ( aab043...4b229d )
by Peter
02:22
created

PageCategory::addBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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