Completed
Push — master ( 3a3229...8072c5 )
by David
13s
created

CmsPageExtension::getCmsBlocksByTag()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 5
nop 5
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\StaticRegistry\Twig;
5
6
use Barclays\Dao\BaseBlockDao;
0 ignored issues
show
Bug introduced by
The type Barclays\Dao\BaseBlockDao was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Barclays\Dao\StaticBlockDao;
0 ignored issues
show
Bug introduced by
The type Barclays\Dao\StaticBlockDao was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Barclays\Model\BaseBlock;
0 ignored issues
show
Bug introduced by
The type Barclays\Model\BaseBlock was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Barclays\Model\PageVersion;
0 ignored issues
show
Bug introduced by
The type Barclays\Model\PageVersion was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Barclays\Services\SerializationContext;
0 ignored issues
show
Bug introduced by
The type Barclays\Services\SerializationContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use TheCodingMachine\CMS\CMSException;
12
use TheCodingMachine\CMS\StaticRegistry\Loaders\Block;
13
use TheCodingMachine\CMS\StaticRegistry\Loaders\Page;
14
use TheCodingMachine\CMS\StaticRegistry\Registry\BlockRegistry;
15
use TheCodingMachine\CMS\StaticRegistry\Registry\PageRegistry;
16
use TheCodingMachine\TDBM\UncheckedOrderBy;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\UncheckedOrderBy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
class CmsPageExtension extends \Twig_Extension
19
{
20
    /**
21
     * @var PageRegistry
22
     */
23
    private $pageRegistry;
24
    /**
25
     * @var BlockRegistry
26
     */
27
    private $blockRegistry;
28
29
    public function __construct(PageRegistry $pageRegistry, BlockRegistry $blockRegistry)
30
    {
31
        $this->pageRegistry = $pageRegistry;
32
        $this->blockRegistry = $blockRegistry;
33
    }
34
35
    /**
36
     * Returns a list of functions to add to the existing list.
37
     *
38
     * @return \Twig_Function[]
39
     */
40
    public function getFunctions()
41
    {
42
        return [
43
            new \Twig_Function('cmsBlocksById', [$this, 'getCmsBlocksById']),
44
            new \Twig_Function('cmsPagesByTag', [$this, 'getCmsPagesByTag']),
45
            new \Twig_Function('cmsBlocksByTag', [$this, 'getCmsBlocksByTag']),
46
        ];
47
    }
48
49
    /**
50
     * @return Block[]
51
     */
52
    public function getCmsBlocksById(string $blockId): array
53
    {
54
        $blocks = $this->blockRegistry->getBlocks($blockId);
55
56
        return $blocks;
57
    }
58
59
    /**
60
     * @param string $tag
61
     * @param null|string $domain
62
     * @param string $orderBy One of the properties of the context. For instance, if you set this to 'date', it means you should add a "date" key to your context it you want to sort upon it.
63
     * @param string $direction
64
     * @param int|null $limit
65
     * @param int|null $offset
66
     * @return Page[]
67
     * @throws CMSException
68
     */
69
    public function getCmsPagesByTag(string $tag, ?string $domain = null, ?string $orderBy = null, string $direction = 'desc', int $limit = null, int $offset = null): array
70
    {
71
        if (!\in_array($direction, ['asc', 'desc'], true)) {
72
            throw new CMSException("Error while using getCmsPagesByTag. The fourth parameter (direction) must be either 'asc' or 'desc'.");
73
        }
74
75
        $pages = $this->pageRegistry->findPagesByTag($tag, $domain);
76
77
        if ($orderBy !== null) {
78
            usort($pages, function(Page $page1, Page $page2) use ($orderBy, $direction) {
79
                if ($direction === 'asc') {
80
                    return ($page1->getContext()[$orderBy] ?? null) <=> ($page2->getContext()[$orderBy] ?? null);
81
                } else {
82
                    return ($page2->getContext()[$orderBy] ?? null) <=> ($page1->getContext()[$orderBy] ?? null);
83
                }
84
            });
85
        }
86
87
        if ($limit !== null || $offset !== null) {
88
            $pages = \array_slice($pages, $offset, $limit);
89
        }
90
91
        return $pages;
92
    }
93
94
    /**
95
     * @param string $tag
96
     * @param string $orderBy One of the properties of the context. For instance, if you set this to 'date', it means you should add a "date" key to your context it you want to sort upon it.
97
     * @param string $direction
98
     * @param int|null $limit
99
     * @param int|null $offset
100
     * @return Block[]
101
     * @throws CMSException
102
     */
103
    public function getCmsBlocksByTag(string $tag, ?string $orderBy = null, string $direction = 'desc', int $limit = null, int $offset = null): array
104
    {
105
        if (!\in_array($direction, ['asc', 'desc'], true)) {
106
            throw new CMSException("Error while using getCmsBlocksByTag. The third parameter (direction) must be either 'asc' or 'desc'.");
107
        }
108
109
        $blocks = $this->blockRegistry->findBlocksByTag($tag);
110
111
        if ($orderBy !== null) {
112
            usort($blocks, function(Block $block1, Block $block2) use ($orderBy, $direction) {
113
                if ($direction === 'asc') {
114
                    return ($block1->getContext()[$orderBy] ?? null) <=> ($block2->getContext()[$orderBy] ?? null);
115
                } else {
116
                    return ($block2->getContext()[$orderBy] ?? null) <=> ($block1->getContext()[$orderBy] ?? null);
117
                }
118
            });
119
        }
120
121
        if ($limit !== null || $offset !== null) {
122
            $blocks = \array_slice($blocks, $offset, $limit);
123
        }
124
125
        return $blocks;
126
    }
127
}
128