Completed
Push — master ( 124fed...6caa0a )
by Tomáš
12s
created

ProxySourceItem::offsetSet()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
crap 20
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 date()
49
    {
50
        return $this->data()->get('date');
51
    }
52
53
    public function setBlocks(array $blocks = null)
54
    {
55
        $this->data()->set('blocks', $blocks ?: []);
56
    }
57
58
    public function previousItem()
59
    {
60
        return $this->previousItem;
61
    }
62
63
    public function setPreviousItem(ProxySourceItem $item = null)
64
    {
65
        $lastPreviousItem = $this->previousItem;
66
        $this->previousItem = $item;
67
        if ($lastPreviousItem) {
68
            // We did have a item before...
69
            if (!$item || $item->id() !== $lastPreviousItem->id()) {
70
                // But we no longer have a item or the item we
71
                // were given does not have the same ID as the
72
                // last one we had...
73
                $this->reprocess();
74
            }
75
        } elseif ($item) {
76
            // We didn't have a item before but we do now...
77
            $this->reprocess();
78
        }
79
    }
80
81
    public function nextItem()
82
    {
83
        return $this->nextItem;
84
    }
85
86
    public function setNextItem(ProxySourceItem $item = null)
87
    {
88
        $lastNextItem = $this->nextItem;
89
        $this->nextItem = $item;
90
        if ($lastNextItem) {
91
            // We did have a item before...
92
            if (!$item || $item->id() !== $lastNextItem->id()) {
93
                // But we no longer have a item or the item we
94
                // were given does not have the same ID as the
95
                // last one we had...
96
                $this->reprocess();
97
            }
98
        } elseif ($item) {
99
            // We didn't have a item before but we do now...
100
            $this->reprocess();
101
        }
102
    }
103
104
    public function reprocess()
105
    {
106
        $this->setHasChanged();
107
    }
108
109
    public function offsetSet($offset, $value)
110
    {
111
        if (is_null($offset)) {
112
            throw new \InvalidArgumentException('Proxy source items cannot have values pushed onto them');
113
        } else {
114
            if (method_exists($this, $offset)) {
115
                return call_user_func([$this, $offset, $value]);
116
            }
117
118
            $setMethod = 'set'.ucfirst($offset);
119
            if (method_exists($this, $setMethod)) {
120
                return call_user_func([$this, $setMethod, $value]);
121
            }
122
123
            $this->data()->set($offset, $value);
124
        }
125
    }
126
127
    public function offsetExists($offset)
128
    {
129
        return !method_exists($this, $offset) && null !== $this->data()->get($offset);
130
    }
131
132
    public function offsetUnset($offset)
133
    {
134
        if (!method_exists($this, $offset)) {
135
            $this->data()->remove($offset);
136
        }
137
    }
138
139
    public function offsetGet($offset)
140
    {
141
        if (method_exists($this, $offset)) {
142
            return call_user_func([$this, $offset]);
143
        }
144
145
        return $this->data()->get($offset);
146
    }
147
}
148