Passed
Push — analysis-vQjJ4v ( b84763 )
by Arnaud
12:53 queued 08:04
created

Site::offsetGet()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 17
c 1
b 0
f 1
nc 8
nop 1
dl 0
loc 25
rs 8.0555
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Renderer;
12
13
use Cecil\Builder;
14
15
/**
16
 * Class Site.
17
 */
18
class Site implements \ArrayAccess
19
{
20
    /** @var Builder Builder object. */
21
    protected $builder;
22
    /** @var string Current language. */
23
    protected $language;
24
25
    /**
26
     * @param Builder     $builder
27
     * @param string|null $language
28
     */
29
    public function __construct(Builder $builder, string $language = null)
30
    {
31
        $this->builder = $builder;
32
        $this->language = $language;
33
    }
34
35
    /**
36
     * Implement ArrayAccess.
37
     *
38
     * @param mixed $offset
39
     *
40
     * @return bool
41
     */
42
    public function offsetExists($offset)
43
    {
44
        // special cases
45
        switch ($offset) {
46
            case 'home':
47
            case 'menus':
48
                return true;
49
        }
50
51
        return $this->builder->getConfig()->has($offset);
52
    }
53
54
    /**
55
     * Implements ArrayAccess.
56
     *
57
     * @param mixed $offset
58
     *
59
     * @return mixed|null
60
     */
61
    public function offsetGet($offset)
62
    {
63
        // special cases
64
        switch ($offset) {
65
            case 'menus':
66
                return $this->builder->getMenus($this->language);
67
            case 'taxonomies':
68
                return $this->builder->getTaxonomies();
69
            case 'language':
70
                return new Language($this->builder->getConfig(), $this->language);
71
            case 'data':
72
                return $this->builder->getData();
73
            case 'static':
74
                return $this->builder->getStatic();
75
        }
76
        if ($offset == 'home') {
77
            if (count($this->builder->getConfig()->getLanguages()) > 1
78
                && $this->language != $this->builder->getConfig()->getLanguageDefault) {
0 ignored issues
show
Bug introduced by
The property getLanguageDefault does not seem to exist on Cecil\Config.
Loading history...
79
                return sprintf('index.%s', $this->language);
80
            }
81
82
            return 'index';
83
        }
84
85
        return $this->builder->getConfig()->get($offset, $this->language);
86
    }
87
88
    /**
89
     * Implements ArrayAccess.
90
     *
91
     * @param mixed $offset
92
     * @param mixed $value
93
     */
94
    public function offsetSet($offset, $value)
95
    {
96
    }
97
98
    /**
99
     * Implements ArrayAccess.
100
     *
101
     * @param mixed $offset
102
     */
103
    public function offsetUnset($offset)
104
    {
105
    }
106
107
    /**
108
     * Returns all pages.
109
     *
110
     * @return \Cecil\Collection\Page\Collection
111
     */
112
    public function getPages(): \Cecil\Collection\Page\Collection
113
    {
114
        return $this->builder->getPages();
115
    }
116
117
    /**
118
     * Return current time.
119
     *
120
     * @return int
121
     */
122
    public function getTime(): int
123
    {
124
        return time();
125
    }
126
}
127