Completed
Push — master ( 874d6a...5decb7 )
by Thibaud
10s
created

Basket::getRawData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[email protected]>
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 PhraseanetSDK\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
16
class Basket
17
{
18
19 2
    public static function fromList(array $values)
20
    {
21 2
        $baskets = array();
22
23 2
        foreach ($values as $value) {
24 2
            $baskets[$value->basket_id] = self::fromValue($value);
25 2
        }
26
27 2
        return $baskets;
28
    }
29
30 2
    public static function fromValue(\stdClass $value)
31
    {
32 2
        return new self($value);
33
    }
34
35
    /**
36
     * @var \stdClass
37
     */
38
    protected $source;
39
40
    /**
41
     * @var User|null
42
     */
43
    protected $owner;
44
45
    /**
46
     * @var User|null
47
     */
48
    protected $pusher;
49
50
    /**
51
     * @var \DateTime
52
     */
53
    protected $createdOn;
54
55
    /**
56
     * @var \DateTime
57
     */
58
    protected $updatedOn;
59
60
    /**
61
     * ArrayCollection|User[]
62
     */
63
    protected $validationUsers;
64
65
    /**
66
     * @var \DateTime
67
     */
68
    protected $expiresOn;
69
70
    /**
71
     * @var User|null
72
     */
73
    protected $validationInitiatorUser;
74
75 2
    public function __construct(\stdClass $source)
76
    {
77 2
        $this->source = $source;
78 2
    }
79
80
    /**
81
     * @return \stdClass
82
     */
83
    public function getRawData()
84
    {
85 2
        return $this->source;
86
    }
87 2
88
    /**
89
     * The basket id
90
     *
91
     * @return integer
92
     */
93
    public function getId()
94
    {
95 2
        return $this->source->basket_id;
96
    }
97 2
98
    /**
99
     * The basket name
100
     *
101
     * @return string
102
     */
103
    public function getName()
104
    {
105 2
        return $this->source->name;
106
    }
107 2
108
    /**
109
     * The basket description
110
     *
111
     * @return string
112
     */
113
    public function getDescription()
114
    {
115
        return $this->source->description;
116
    }
117
118
    /**
119
     * The user who created the basket when the current basket
120
     * is a validation basket
121
     *
122
     * @return integer|null
123
     */
124
    public function getPusher()
125
    {
126 2
        return $this->pusher ?: $this->pusher = User::fromValue($this->source->pusher);
127
    }
128 2
129
    /**
130
     * Tell whether the basket has been read or not
131
     *
132
     * @return Boolean
133
     */
134
    public function isUnread()
135
    {
136 2
        return $this->source->unread;
137
    }
138 2
139
    /**
140
     * Creation date
141
     *
142
     * @return \DateTime
143
     */
144
    public function getCreatedOn()
145
    {
146 2
        return $this->createdOn ?: $this->createdOn = new \DateTime($this->source->created_on);
147
    }
148 2
149
    /**
150
     * Last update date
151
     *
152
     * @return \DateTime
153
     */
154
    public function getUpdatedOn()
155
    {
156 2
        return $this->updatedOn ?: $this->updatedOn = new \DateTime($this->source->updated_on);
157
    }
158 2
159
    /**
160
     * Tell whether the basket is a validation basket
161
     *
162
     * @return Boolean
163
     */
164
    public function isValidationBasket()
165
    {
166
        return $this->source->validation_basket;
167 2
    }
168
169 2
    /**
170
     * Return a collection of PhraseanetSDK\entity\BasketValidationParticipant object
171
     * if the basket is a validation basket otherwise it returns null
172
     *
173 2
     * @return ArrayCollection|null
174 2
     */
175 2
    public function getValidationUsers()
176
    {
177
        if (! $this->isValidationBasket()) {
178
            return null;
179
        }
180
181
        return $this->validationUsers ?: $this->validationUsers = new ArrayCollection(
182
            BasketValidationParticipant::fromList($this->source->validation_users)
183 2
        );
184
    }
185 2
186
    /**
187
     * The expiration validation date, if the basket is a validation basket
188
     *
189
     * @return \DateTime|null
190
     */
191
    public function getExpiresOn()
192
    {
193
        return $this->expiresOn ?: $this->expiresOn = new \DateTime($this->source->expires_on);
194 2
    }
195
196 2
    /**
197
     * Get some information about the validation, if the basket is a validation
198
     * basket
199
     *
200
     * @return string|null
201
     */
202
    public function getValidationInfo()
203
    {
204 2
        return $this->source->validation_infos;
205
    }
206 2
207
    /**
208
     * Tell whether the validation is confirmed
209
     *
210
     * @return bool
211
     */
212
    public function isValidationConfirmed()
213
    {
214 2
        return (bool) $this->source->validation_confirmed;
215
    }
216 2
217
    /**
218
     * Tell whether the current authenticated user initiates the validation process
219
     *
220
     * @return bool
221
     */
222
    public function isValidationInitiator()
223
    {
224
        return (bool) $this->source->validation_initiator;
225
    }
226
227
    /**
228
     * @return User|null
229
     */
230
    public function getValidationInitiatorUser()
231
    {
232
        return $this->validationInitiatorUser ?: $this->validationInitiatorUser = User::fromValue(
233
            $this->source->validation_initiator_user
234
        );
235
    }
236
237
    /**
238
     * @return User
239
     */
240
    public function getOwner()
241
    {
242
        return $this->owner ?: $this->owner = User::fromValue($this->source->owner);
243
    }
244
}
245