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

ContentList::createTemplateData()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 25
rs 9.2222
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\ContentListCache as Cache;
13
use AbterPhp\Website\Domain\Entities\ContentList as Entity;
14
use AbterPhp\Website\Orm\ContentListItemRepo as ItemRepo;
15
use AbterPhp\Website\Orm\ContentListRepo as Repo;
16
17
class ContentList implements ILoader
18
{
19
    /**
20
     * @var Repo
21
     */
22
    protected $repo;
23
24
    /**
25
     * @var ItemRepo
26
     */
27
    protected $itemRepo;
28
29
    /**
30
     * @var Cache
31
     */
32
    protected $cache;
33
34
    /**
35
     * @var IBuilder[]
36
     */
37
    protected $builders;
38
39
    /**
40
     * ContentList constructor.
41
     *
42
     * @param Repo       $repo
43
     * @param ItemRepo   $itemRepo
44
     * @param Cache      $cache
45
     * @param IBuilder[] $builders
46
     */
47
    public function __construct(Repo $repo, ItemRepo $itemRepo, Cache $cache, array $builders)
48
    {
49
        $this->repo     = $repo;
50
        $this->itemRepo = $itemRepo;
51
        $this->cache    = $cache;
52
        $this->builders = $builders;
53
    }
54
55
    /**
56
     * @param string   $name
57
     * @param IBuilder $builder
58
     *
59
     * @return $this
60
     */
61
    public function addBuilder(string $name, IBuilder $builder): self
62
    {
63
        $this->builders[$name] = $builder;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param array<string,ParsedTemplate[]> $parsedTemplates
70
     *
71
     * @return IData[]
72
     * @throws \Opulence\Orm\OrmException
73
     */
74
    public function load(array $parsedTemplates): array
75
    {
76
        $identifiers = array_keys($parsedTemplates);
77
78
        $list = $this->loadWithItems($identifiers);
79
80
        $templateData = $this->createTemplateData($parsedTemplates, $list);
81
82
        return $templateData;
83
    }
84
85
    /**
86
     * @param string[] $identifiers
87
     *
88
     * @return <string,Entity>
0 ignored issues
show
Documentation Bug introduced by
The doc comment <string,Entity> at position 0 could not be parsed: Unknown type name '<' at position 0 in <string,Entity>.
Loading history...
89
     * @throws \Opulence\Orm\OrmException
90
     */
91
    protected function loadWithItems(array $identifiers): array
92
    {
93
        $lists = $this->repo->getByIdentifiers($identifiers);
94
95
        /** @var <string,Entity> $list */
96
        $listsById = [];
97
        foreach ($lists as $list) {
98
            $listsById[$list->getId()] = $list;
99
        }
100
101
        $items = $this->itemRepo->getByListIds(array_keys($listsById));
102
        foreach ($items as $item) {
103
            $listsById[$list->getId()]->addItem($item);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $list seems to be defined by a foreach iteration on line 97. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
104
        }
105
106
        $lists = [];
107
        foreach ($listsById as $list) {
108
            $lists[$list->getIdentifier()] = $list;
109
        }
110
111
        return $lists;
112
    }
113
114
    /**
115
     * @param array<string,ParsedTemplate[]> $parsedTemplates
116
     * @param array<string,Entity>           $lists
117
     *
118
     * @return IData[]
119
     */
120
    protected function createTemplateData(array $parsedTemplates, array $lists): array
121
    {
122
        $templateData = [];
123
124
        foreach ($parsedTemplates as $identifier => $identifierTemplates) {
125
            foreach ($identifierTemplates as $parsedTemplate) {
126
                if (!array_key_exists($identifier, $lists)) {
127
                    continue;
128
                }
129
130
                /** @var Entity $list */
131
                $list = $lists[$identifier];
132
133
                $typeName = $list->getType()->getName();
134
                if ($typeName && array_key_exists($typeName, $this->builders)) {
135
                    $templateData[] = $this->builders[$typeName]->build($list);
0 ignored issues
show
Bug introduced by
$list of type AbterPhp\Website\Domain\Entities\ContentList is incompatible with the type array expected by parameter $entities of AbterPhp\Framework\Template\IBuilder::build(). ( Ignorable by Annotation )

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

135
                    $templateData[] = $this->builders[$typeName]->build(/** @scrutinizer ignore-type */ $list);
Loading history...
136
137
                    continue;
138
                }
139
140
                throw new Config(__CLASS__);
141
            }
142
        }
143
144
        return $templateData;
145
    }
146
147
    /**
148
     * @param string[] $identifiers
149
     * @param string   $cacheTime
150
     *
151
     * @return bool
152
     */
153
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
154
    {
155
        return $this->cache->hasAnyChangedSince($identifiers, $cacheTime);
156
    }
157
}
158