Completed
Pull Request — master (#77)
by Thibaud
07:33
created

Story::getRawData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
use PhraseanetSDK\Annotation\ApiField as ApiField;
16
use PhraseanetSDK\Annotation\ApiRelation as ApiRelation;
17
use PhraseanetSDK\EntityManager;
18
19
class Story
20
{
21
22 1
    public static function fromList(EntityManager $entityManager, array $values)
23
    {
24 1
        $stories = array();
25
26 1
        foreach ($values as $value) {
27 1
            $stories[] = self::fromValue($entityManager, $value);
28 1
        }
29
30 1
        return $stories;
31
    }
32
33 2
    public static function fromValue(EntityManager $entityManager, \stdClass $value)
34
    {
35 2
        return new self($entityManager, $value);
36
    }
37
38
    /**
39
     * @var EntityManager
40
     */
41
    protected $entityManager;
42
43
    /**
44
     * @var \stdClass
45
     */
46
    protected $source;
47
48
    /**
49
     * @var \DateTime
50
     */
51
    protected $updatedOn;
52
53
    /**
54
     * @var \DateTime
55
     */
56
    protected $createdOn;
57
58
    /**
59
     * @var Subdef|null
60
     */
61
    protected $thumbnail;
62
63
    /**
64
     * @var ArrayCollection|Record[]
65
     */
66
    protected $records;
67
68
    /**
69
     * @var ArrayCollection|Metadata[]
70
     */
71
    protected $metadata;
72
73
    /**
74
     * @var ArrayCollection|RecordStatus[]
75
     */
76
    protected $status;
77
78
    /**
79
     * @var ArrayCollection|RecordCaption[]
80
     */
81
    protected $caption;
82
83
    /**
84
     * @var int
85
     */
86
    protected $recordCount;
87
88
    /**
89
     * @param EntityManager $entityManager
90
     * @param \stdClass $source
91
     */
92 2
    public function __construct(EntityManager $entityManager, \stdClass $source)
93
    {
94 2
        $this->entityManager = $entityManager;
95 2
        $this->source = $source;
96 2
    }
97
98
    /**
99
     * @return \stdClass
100
     */
101
    public function getRawData()
102
    {
103
        return $this->source;
104
    }
105
106
    /**
107
     * Get unique id
108
     *
109
     * @return string
110
     */
111 2
    public function getId()
112
    {
113 2
        return $this->getDataboxId().'_'.$this->getStoryId();
114
    }
115
116
    /**
117
     * Get the record id
118
     *
119
     * @return integer
120
     */
121 2
    public function getStoryId()
122
    {
123 2
        return $this->source->story_id;
124
    }
125
126
    /**
127
     * Get the databox id
128
     *
129
     * @return integer
130
     */
131 2
    public function getDataboxId()
132
    {
133 2
        return $this->source->databox_id;
134
    }
135
136
    /**
137
     * @return null|Subdef
138
     */
139 2
    public function getThumbnail()
140
    {
141 2
        if (! isset($this->source->thumbnail)) {
142 1
            return null;
143
        }
144
145 2
        return $this->thumbnail ?: $this->thumbnail = Subdef::fromValue($this->source->thumbnail);
146
    }
147
148
    /**
149
     * Last updated date
150
     *
151
     * @return \DateTime
152
     */
153 2
    public function getUpdatedOn()
154
    {
155 2
        return $this->updatedOn ?: $this->updatedOn = new \DateTime($this->source->updated_on);
156
    }
157
158
    /**
159
     * Creation date
160
     *
161
     * @return \DateTime
162
     */
163 2
    public function getCreatedOn()
164
    {
165 2
        return $this->createdOn ?: $this->createdOn = new \DateTime($this->source->created_on);
166
    }
167
168
    /**
169
     * Get the record collection id
170
     *
171
     * @return integer
172
     */
173 2
    public function getCollectionId()
174
    {
175 2
        return $this->source->collection_id;
176
    }
177
178
    /**
179
     * Get the record UUID
180
     *
181
     * @return string
182
     */
183 2
    public function getUuid()
184
    {
185 2
        return $this->source->uuid;
186
    }
187
188
    /**
189
     * @return int
190
     */
191
    public function getRecordCount()
192
    {
193
        return $this->recordCount !== null ?
194
            $this->recordCount :
195
            $this->recordCount =
196
                (isset($this->source->record_count) ? $this->source->record_count : count($this->getRecords()));
197
    }
198
199
    /**
200
     * @return Record[]|ArrayCollection
201
     */
202
    public function getRecords()
203
    {
204
        if (! isset($this->source->records)) {
205
            $this->records = new ArrayCollection();
206
        }
207
208
        return $this->records ?: $this->records = new ArrayCollection(
209
            Record::fromList((array) $this->source->records)
210
        );
211
    }
212
213 2
    /**
214
     * @return Metadata[]|ArrayCollection
215 2
     */
216 2
    public function getMetadata()
217 2
    {
218
        if (! isset($this->source->metadata)) {
219 2
            $this->metadata = new ArrayCollection();
220
        }
221
222
        return $this->metadata ?: $this->metadata = new ArrayCollection(
223
            Metadata::fromList((array) $this->source->metadata)
224
        );
225
    }
226
227
    /**
228
     * @return RecordStatus[]|ArrayCollection
229
     */
230
    public function getStatus()
231
    {
232
        if (! isset($this->status)) {
233
            $this->status = $this->entityManager->getRepository('recordStatus')->findByRecord(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhraseanetSDK\AbstractRepository as the method findByRecord() does only exist in the following sub-classes of PhraseanetSDK\AbstractRepository: PhraseanetSDK\Repository\Basket, PhraseanetSDK\Repository\Caption, PhraseanetSDK\Repository\Metadata, PhraseanetSDK\Repository\RecordStatus, PhraseanetSDK\Repository\Subdef. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
234
                $this->getDataboxId(),
235
                $this->getStoryId()
236
            );
237
        }
238
239
        return $this->status;
240
    }
241
242
    /**
243
     * @return RecordCaption[]|ArrayCollection
244
     */
245
    public function getCaption()
246
    {
247
        if (! isset($this->caption) && isset($this->source->caption)) {
248
            $this->caption = RecordCaption::fromList((array) $this->source->caption);
249
        }
250
251
        if (! isset($this->caption)) {
252
            $this->caption = $this->entityManager->getRepository('caption')->findByRecord(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhraseanetSDK\AbstractRepository as the method findByRecord() does only exist in the following sub-classes of PhraseanetSDK\AbstractRepository: PhraseanetSDK\Repository\Basket, PhraseanetSDK\Repository\Caption, PhraseanetSDK\Repository\Metadata, PhraseanetSDK\Repository\RecordStatus, PhraseanetSDK\Repository\Subdef. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
253
                $this->getDataboxId(),
254
                $this->getStoryId()
255
            );
256
        }
257
258
        return $this->caption;
259
    }
260
}
261