Completed
Pull Request — master (#78)
by Vladimir
04:42 queued 01:10
created

RepeaterPageView   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 4
dl 0
loc 112
ccs 26
cts 34
cp 0.7647
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPermalinkWhere() 0 15 3
A getRepeaterPermalinks() 0 4 1
A getRepeaterRedirects() 0 4 1
A bumpPermalink() 0 5 1
A rewindPermalink() 0 4 1
A configurePermalinks() 0 10 1
A buildPermalink() 0 3 1
A createJail() 0 4 1
A jsonSerialize() 0 4 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use __\__;
11
use allejo\stakx\Filesystem\File;
12
use allejo\stakx\FrontMatter\ExpandedValue;
13
14
class RepeaterPageView extends BasePageView implements TemplateReadyDocument
15
{
16
    /** @var \ArrayIterator An iterator for the permalinks used in order for this entity to be treated as a static PageView. */
17
    private $permalinksIterator;
18
19
    /** @var ExpandedValue[] All of the expanded permalinks. */
20
    private $permalinks;
21
22
    /** @var ExpandedValue[][] All of expanded redirects that should point to the respective permalink; this is estimated by index. */
23
    private $redirectLinks;
24
25
    /**
26
     * RepeaterPageView constructor.
27
     */
28 3
    public function __construct(File $file)
29
    {
30 3
        parent::__construct($file);
31
32 3
        $this->type = BasePageView::REPEATER_TYPE;
33 3
    }
34
35
    public function getPermalinkWhere(array $where)
36
    {
37
        foreach ($this->permalinks as $expandedValue)
38
        {
39
            /** @var ExpandedValue[] $permalink */
40
            $permalink = __::where($expandedValue->getIterators(), $where);
41
42
            if (!empty($permalink))
43
            {
44
                return $permalink[0]->getEvaluated();
45
            }
46
        }
47
48
        return null;
49
    }
50
51
    /**
52
     * Get the expanded values for the permalinks to this PageView.
53
     *
54
     * @return ExpandedValue[]
55
     */
56 3
    public function getRepeaterPermalinks()
57
    {
58 3
        return $this->permalinks;
59
    }
60
61
    /**
62
     * Get the expanded values for the redirects pointing to this PageView.
63
     *
64
     * @return ExpandedValue[][]
65
     */
66 3
    public function getRepeaterRedirects()
67
    {
68 3
        return $this->redirectLinks;
69
    }
70
71
    /**
72
     * When looping through permalinks in a RepeaterPageView, the permalink needs to be updated each time so that it may
73
     * behave as a static PageView.
74
     */
75 3
    public function bumpPermalink()
76
    {
77 3
        $this->permalink = $this->permalinksIterator->current()->getEvaluated();
78 3
        $this->permalinksIterator->next();
79 3
    }
80
81
    /**
82
     * Rewind the permalink iterator to the beginning.
83
     */
84 3
    public function rewindPermalink()
85
    {
86 3
        $this->permalinksIterator->rewind();
87 3
    }
88
89
    /**
90
     * Configure permalinks from expanded values internally.
91
     */
92 3
    public function configurePermalinks()
93
    {
94 3
        $evaluated = $this->frontMatter['permalink'];
95
96 3
        $this->permalinks = $evaluated[0];
97 3
        array_shift($evaluated);
98 3
        $this->redirectLinks = $evaluated;
99
100 3
        $this->permalinksIterator = new \ArrayIterator($this->permalinks);
101 3
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 3
    public function buildPermalink($force = false)
107
    {
108 3
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 3
    public function createJail()
114
    {
115 3
        return new JailedDocument($this, self::$whiteListedFunctions);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function jsonSerialize()
122
    {
123
        return [];
124
    }
125
}
126