Passed
Push — fallback-intl ( 2f4c17...1f6a03 )
by Arnaud
03:32
created

Site   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 74.36%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 45
c 2
b 1
f 0
dl 0
loc 149
ccs 29
cts 39
cp 0.7436
rs 10
wmc 26

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A offsetSet() 0 3 1
A getPage() 0 11 5
A getAllPages() 0 3 1
B offsetGet() 0 22 9
A offsetExists() 0 11 3
A getPages() 0 9 3
A getTime() 0 3 1
A offsetUnset() 0 3 1
A getPagesIntl() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Renderer;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page as CollectionPage;
18
19
/**
20
 * Class Site.
21
 */
22
class Site implements \ArrayAccess
23
{
24
    /** @var Builder Builder object. */
25
    protected $builder;
26
27
    /** @var \Cecil\Config */
28
    protected $config;
29
30 1
    /** @var string Current language. */
31
    protected $language;
32 1
33 1
    public function __construct(Builder $builder, string $language)
34 1
    {
35 1
        $this->builder = $builder;
36
        $this->config = $this->builder->getConfig();
37
        $this->language = $language;
38
    }
39
40
    /**
41
     * Implement ArrayAccess.
42
     *
43
     * @param mixed $offset
44 1
     *
45
     * @return bool
46
     */
47
    #[\ReturnTypeWillChange]
48 1
    public function offsetExists($offset): bool
49 1
    {
50 1
        // special cases
51
        switch ($offset) {
52
            case 'menus':
53 1
            case 'home':
54
                return true;
55
        }
56
57
        return $this->config->has($offset);
58
    }
59
60
    /**
61
     * Implements \ArrayAccess.
62
     *
63 1
     * @param mixed $offset
64
     *
65
     * @return mixed|null
66
     */
67 1
    #[\ReturnTypeWillChange]
68 1
    public function offsetGet($offset)
69 1
    {
70 1
        // Featch data from builder instead of config raw data
71 1
        switch ($offset) {
72 1
            case 'pages':
73 1
                return $this->getPages();
74 1
            case 'menus':
75 1
                return $this->builder->getMenus($this->language);
76 1
            case 'taxonomies':
77 1
                return $this->builder->getTaxonomies();
78
            case 'data':
79
                return $this->builder->getData();
80
            case 'static':
81 1
                return $this->builder->getStatic();
82
            case 'language':
83
                return new Language($this->config, $this->language);
84
            case 'home':
85
                return $this->language != $this->config->getLanguageDefault() ? \sprintf('index.%s', $this->language) : 'index';
86
        }
87
88
        return $this->config->get($offset, $this->language);
89
    }
90
91
    /**
92
     * Implements \ArrayAccess.
93
     *
94
     * @param mixed $offset
95
     * @param mixed $value
96
     *
97
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
98
     */
99
    #[\ReturnTypeWillChange]
100
    public function offsetSet($offset, $value): void
101
    {
102
    }
103
104
    /**
105
     * Implements \ArrayAccess.
106 1
     *
107
     * @param mixed $offset
108
     *
109
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
110 1
     */
111
    #[\ReturnTypeWillChange]
112
    public function offsetUnset($offset): void
113
    {
114 1
    }
115 1
116
    /**
117
     * Returns a page in the current language or the one provided.
118
     *
119
     * @throws \DomainException
120
     */
121
    public function getPage(string $id, string $language = null): ?CollectionPage
122
    {
123
        $pageId = $id;
124
        if ($language === null && $this->language != $this->config->getLanguageDefault()) {
125
            $pageId = \sprintf('%s.%s', $id, $this->language);
126
        }
127
        if ($language !== null && $language != $this->config->getLanguageDefault()) {
128
            $pageId = \sprintf('%s.%s', $id, $language);
129
        }
130
131
        return $this->builder->getPages()->get($pageId);
132
    }
133
134
    /**
135
     * Returns all pages, in the current language.
136
     */
137 1
    public function getPages(): \Cecil\Collection\Page\Collection
138
    {
139 1
        return $this->builder->getPages()->filter(function (CollectionPage $page) {
140
            // We should fix case of virtual pages without language
141
            if ($page->getLanguage() === null && $this->language == $this->config->getLanguageDefault()) {
0 ignored issues
show
introduced by
The condition $page->getLanguage() === null is always false.
Loading history...
142
                return true;
143
            }
144
145
            return $page->getLanguage() == $this->language;
146
        });
147
    }
148
149
    /**
150
     * Returns all pages, regardless of their translation.
151
     */
152
    public function getAllPages(): \Cecil\Collection\Page\Collection
153
    {
154
        return $this->builder->getPages();
155
    }
156
157
    /**
158
     * Alias of getAllPages().
159
     */
160
    public function getPagesIntl(): \Cecil\Collection\Page\Collection
161
    {
162
        return $this->getAllPages();
163
    }
164
165
    /**
166
     * Return current time.
167
     */
168
    public function getTime(): int
169
    {
170
        return time();
171
    }
172
}
173