Completed
Pull Request — master (#12)
by Tomáš
04:06 queued 01:21
created

ProxySourceItem::setNextItem()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
crap 30
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\PostsBundle\ProxySourceCollection;
13
14
use Symplify\PHP7_Sculpin\Source\ProxySource;
15
16
class ProxySourceItem extends ProxySource implements \ArrayAccess
17
{
18
    /**
19
     * @var
20
     */
21
    private $previousItem;
22
23
    /**
24
     * @var
25
     */
26
    private $nextItem;
27
28
    public function id()
29
    {
30
        return $this->sourceId();
31
    }
32
33
    public function meta()
34
    {
35
        return $this->data()->export();
36
    }
37
38
    public function url()
39
    {
40
        return $this->permalink()->relativeUrlPath();
41
    }
42
43
    public function blocks()
44
    {
45
        return $this->data()->get('blocks');
46
    }
47
48
    public function setBlocks(array $blocks = null)
49
    {
50
        $this->data()->set('blocks', $blocks ?: []);
51
    }
52
53
    public function previousItem()
54
    {
55
        return $this->previousItem;
56
    }
57
58
    public function setPreviousItem(ProxySourceItem $item = null)
59
    {
60
        $lastPreviousItem = $this->previousItem;
61
        $this->previousItem = $item;
62
        if ($lastPreviousItem) {
63
            // We did have a item before...
64
            if (!$item || $item->id() !== $lastPreviousItem->id()) {
65
                // But we no longer have a item or the item we
66
                // were given does not have the same ID as the
67
                // last one we had...
68
                $this->reprocess();
69
            }
70
        } elseif ($item) {
71
            // We didn't have a item before but we do now...
72
            $this->reprocess();
73
        }
74
    }
75
76
    public function nextItem()
77
    {
78
        return $this->nextItem;
79
    }
80
81
    public function setNextItem(ProxySourceItem $item = null)
82
    {
83
        $lastNextItem = $this->nextItem;
84
        $this->nextItem = $item;
85
        if ($lastNextItem) {
86
            // We did have a item before...
87
            if (!$item || $item->id() !== $lastNextItem->id()) {
88
                // But we no longer have a item or the item we
89
                // were given does not have the same ID as the
90
                // last one we had...
91
                $this->reprocess();
92
            }
93
        } elseif ($item) {
94
            // We didn't have a item before but we do now...
95
            $this->reprocess();
96
        }
97
    }
98
99
    public function reprocess()
100
    {
101
        $this->setHasChanged();
102
    }
103
104
    public function offsetSet($offset, $value)
105
    {
106
        if (is_null($offset)) {
107
            throw new \InvalidArgumentException('Proxy source items cannot have values pushed onto them');
108
        } else {
109
            if (method_exists($this, $offset)) {
110
                return call_user_func([$this, $offset, $value]);
111
            }
112
113
            $setMethod = 'set'.ucfirst($offset);
114
            if (method_exists($this, $setMethod)) {
115
                return call_user_func([$this, $setMethod, $value]);
116
            }
117
118
            $this->data()->set($offset, $value);
119
        }
120
    }
121
122
    public function offsetExists($offset)
123
    {
124
        return !method_exists($this, $offset) && null !== $this->data()->get($offset);
125
    }
126
127
    public function offsetUnset($offset)
128
    {
129
        if (!method_exists($this, $offset)) {
130
            $this->data()->remove($offset);
131
        }
132
    }
133
134
    public function offsetGet($offset)
135
    {
136
        if (method_exists($this, $offset)) {
137
            return call_user_func([$this, $offset]);
138
        }
139
140
        return $this->data()->get($offset);
141
    }
142
}
143