Passed
Push — master ( 4a146d...3e1455 )
by Peter
02:17
created

PageCategoryLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 74
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAnyChangedSince() 0 3 1
A load() 0 12 2
A __construct() 0 5 1
A groupPages() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Template;
6
7
use AbterPhp\Framework\Template\IBuilder;
8
use AbterPhp\Framework\Template\IData;
9
use AbterPhp\Framework\Template\ILoader;
10
use AbterPhp\Website\Databases\Queries\PageCategoryCache;
11
use AbterPhp\Website\Domain\Entities\Page;
12
use AbterPhp\Website\Orm\PageRepo;
13
14
class PageCategoryLoader implements ILoader
15
{
16
    /**
17
     * @var PageRepo
18
     */
19
    protected $pageRepo;
20
21
    /**
22
     * @var PageCategoryCache
23
     */
24
    protected $pageCategoryCache;
25
26
    /**
27
     * @var IBuilder
28
     */
29
    protected $builder;
30
31
    /**
32
     * PageCategoryLoader constructor.
33
     *
34
     * @param PageRepo          $pageRepo
35
     * @param PageCategoryCache $blockCache
36
     * @param IBuilder          $builder
37
     */
38
    public function __construct(PageRepo $pageRepo, PageCategoryCache $pageCategoryCache, IBuilder $builder)
39
    {
40
        $this->pageRepo          = $pageRepo;
41
        $this->pageCategoryCache = $pageCategoryCache;
42
        $this->builder           = $builder;
43
    }
44
45
    /**
46
     * @param string[] $identifiers
47
     *
48
     * @return IData[]
49
     */
50
    public function load(array $identifiers): array
51
    {
52
        $pages = $this->pageRepo->getByCategoryIdentifiers($identifiers);
53
54
        $titlesByCategories = $this->groupPages($pages);
55
56
        $templateData = [];
57
        foreach ($titlesByCategories as $category => $pages) {
58
            $templateData[] = $this->builder->build($pages);
59
        }
60
61
        return $templateData;
62
    }
63
64
    /**
65
     * @param array $pages
66
     *
67
     * @return Page[][]
68
     */
69
    protected function groupPages(array $pages): array
70
    {
71
        $titlesByCategories = [];
72
        foreach ($pages as $page) {
73
            $titlesByCategories[$page->getCategory()->getIdentifier()][] = $page;
74
        }
75
76
        return $titlesByCategories;
77
    }
78
79
    /**
80
     * @param string[] $identifiers
81
     * @param string   $cacheTime
82
     *
83
     * @return bool
84
     */
85
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
86
    {
87
        return $this->pageCategoryCache->hasAnyChangedSince($identifiers, $cacheTime);
88
    }
89
}
90