1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilBuddySystemRelation
6 * @author Michael Jansen <mjansen@databay.de>
7 */
8class ilBuddySystemRelation
9{
10    /** @var bool */
11    protected $isOwnedByActor = false;
12
13    /** @var int */
14    protected $usrId;
15
16    /** @var int */
17    protected $buddyUsrId;
18
19    /** @var int */
20    protected $timestamp;
21
22    /** @var ilBuddySystemRelationState */
23    protected $state;
24
25    /** @var ilBuddySystemRelationState|null */
26    protected $priorState;
27
28    /**
29     * ilBuddySystemRelation constructor.
30     * @param ilBuddySystemRelationState $state
31     */
32    public function __construct(ilBuddySystemRelationState $state)
33    {
34        $this->setState($state, false);
35    }
36
37    /**
38     * @param ilBuddySystemRelationState $state
39     * @param $rememberPriorState boolean
40     * @return self
41     */
42    public function setState(ilBuddySystemRelationState $state, bool $rememberPriorState = true) : self
43    {
44        if ($rememberPriorState) {
45            $this->setPriorState($this->getState());
46        }
47
48        $this->state = $state;
49        return $this;
50    }
51
52    /**
53     * @return ilBuddySystemRelationState
54     */
55    public function getState() : ilBuddySystemRelationState
56    {
57        return $this->state;
58    }
59
60    /**
61     * @return ilBuddySystemRelationState|null
62     */
63    public function getPriorState() : ?ilBuddySystemRelationState
64    {
65        return $this->priorState;
66    }
67
68    /**
69     * @param ilBuddySystemRelationState $prior_state
70     * @return ilBuddySystemRelation
71     */
72    private function setPriorState(ilBuddySystemRelationState $prior_state) : self
73    {
74        $this->priorState = $prior_state;
75        return $this;
76    }
77
78    /**
79     * @return bool
80     */
81    public function isOwnedByActor() : bool
82    {
83        return $this->isOwnedByActor;
84    }
85
86    /**
87     * @param bool $isOwnedByActor
88     * @return ilBuddySystemRelation
89     */
90    public function setIsOwnedByActor(bool $isOwnedByActor) : self
91    {
92        $this->isOwnedByActor = $isOwnedByActor;
93        return $this;
94    }
95
96    /**
97     * @return int
98     */
99    public function getBuddyUsrId() : int
100    {
101        return $this->buddyUsrId;
102    }
103
104    /**
105     * @param int $buddyUsrId
106     * @return self
107     */
108    public function setBuddyUsrId(int $buddyUsrId) : self
109    {
110        $this->buddyUsrId = $buddyUsrId;
111        return $this;
112    }
113
114    /**
115     * @return int
116     */
117    public function getUsrId() : int
118    {
119        return $this->usrId;
120    }
121
122    /**
123     * @param int $usrId
124     * @return self
125     */
126    public function setUsrId(int $usrId) : self
127    {
128        $this->usrId = $usrId;
129        return $this;
130    }
131
132    /**
133     * @return int
134     */
135    public function getTimestamp() : int
136    {
137        return $this->timestamp;
138    }
139
140    /**
141     * @param int $timestamp
142     * @return self
143     */
144    public function setTimestamp(int $timestamp) : self
145    {
146        $this->timestamp = $timestamp;
147        return $this;
148    }
149
150    /**
151     * @return ilBuddySystemCollection
152     */
153    public function getCurrentPossibleTargetStates() : ilBuddySystemCollection
154    {
155        return ilBuddySystemRelationStateFilterRuleFactory::getInstance()->getFilterRuleByRelation($this)->getStates();
156    }
157
158    /**
159     * @return self
160     * @throws ilBuddySystemRelationStateException
161     */
162    public function link() : self
163    {
164        if ($this->getUsrId() === $this->getBuddyUsrId()) {
165            throw new ilBuddySystemRelationStateException("Can't change a state when the requester equals the requestee.");
166        }
167
168        $this->getState()->link($this);
169        return $this;
170    }
171
172    /**
173     * @return self
174     * @throws ilBuddySystemRelationStateException
175     */
176    public function unlink() : self
177    {
178        if ($this->getUsrId() === $this->getBuddyUsrId()) {
179            throw new ilBuddySystemRelationStateException("Can't change a state when the requester equals the requestee.");
180        }
181
182        $this->getState()->unlink($this);
183        return $this;
184    }
185
186    /**
187     * @return self
188     * @throws ilBuddySystemRelationStateException
189     */
190    public function request() : self
191    {
192        if ($this->getUsrId() === $this->getBuddyUsrId()) {
193            throw new ilBuddySystemRelationStateException("Can't change a state when the requester equals the requestee.");
194        }
195
196        $this->getState()->request($this);
197        return $this;
198    }
199
200    /**
201     * @return self
202     * @throws ilBuddySystemRelationStateException
203     */
204    public function ignore() : self
205    {
206        if ($this->getUsrId() === $this->getBuddyUsrId()) {
207            throw new ilBuddySystemRelationStateException("Can't change a state when the requester equals the requestee.");
208        }
209
210        $this->getState()->ignore($this);
211        return $this;
212    }
213
214    /**
215     * @return bool
216     */
217    public function isLinked() : bool
218    {
219        return $this->getState() instanceof ilBuddySystemLinkedRelationState;
220    }
221
222    /**
223     * @return bool
224     */
225    public function isUnlinked() : bool
226    {
227        return $this->getState() instanceof ilBuddySystemUnlinkedRelationState;
228    }
229
230    /**
231     * @return bool
232     */
233    public function isRequested() : bool
234    {
235        return $this->getState() instanceof ilBuddySystemRequestedRelationState;
236    }
237
238    /**
239     * @return bool
240     */
241    public function isIgnored() : bool
242    {
243        return $this->getState() instanceof ilBuddySystemIgnoredRequestRelationState;
244    }
245
246    /**
247     * @return bool
248     */
249    public function wasLinked() : bool
250    {
251        return $this->getPriorState() instanceof ilBuddySystemLinkedRelationState;
252    }
253
254    /**
255     * @return bool
256     */
257    public function wasUnlinked() : bool
258    {
259        return $this->getPriorState() instanceof ilBuddySystemUnlinkedRelationState;
260    }
261
262    /**
263     * @return bool
264     */
265    public function wasRequested() : bool
266    {
267        return $this->getPriorState() instanceof ilBuddySystemRequestedRelationState;
268    }
269
270    /**
271     * @return bool
272     */
273    public function wasIgnored() : bool
274    {
275        return $this->getPriorState() instanceof ilBuddySystemIgnoredRequestRelationState;
276    }
277}
278