Completed
Push — master ( d35089...49036b )
by Thibaud
05:48 queued 02:58
created

Story   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 58.33%

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 29
c 12
b 0
f 0
lcom 1
cbo 5
dl 0
loc 225
ccs 35
cts 60
cp 0.5833
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A fromList() 0 10 2
A fromValue() 0 4 1
A __construct() 0 5 1
A getId() 0 4 1
A getStoryId() 0 4 1
A getDataboxId() 0 4 1
A getThumbnail() 0 8 3
A getUpdatedOn() 0 4 2
A getCreatedOn() 0 4 2
A getCollectionId() 0 4 1
A getUuid() 0 4 1
A getRecordCount() 0 6 3
A getRecords() 0 8 3
A getMetadata() 0 8 3
A getStatus() 0 11 2
A getCaption() 0 11 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
     * Get unique id
100
     *
101
     * @return string
102
     */
103 2
    public function getId()
104
    {
105 2
        return $this->getDataboxId().'_'.$this->getStoryId();
106
    }
107
108
    /**
109
     * Get the record id
110
     *
111
     * @return integer
112
     */
113 2
    public function getStoryId()
114
    {
115 2
        return $this->source->story_id;
116
    }
117
118
    /**
119
     * Get the databox id
120
     *
121
     * @return integer
122
     */
123 2
    public function getDataboxId()
124
    {
125 2
        return $this->source->databox_id;
126
    }
127
128
    /**
129
     * @return null|Subdef
130
     */
131 2
    public function getThumbnail()
132
    {
133 2
        if (! isset($this->source->thumbnail)) {
134 1
            return null;
135
        }
136
137 2
        return $this->thumbnail ?: $this->thumbnail = Subdef::fromValue($this->source->thumbnail);
138
    }
139
140
    /**
141
     * Last updated date
142
     *
143
     * @return \DateTime
144
     */
145 2
    public function getUpdatedOn()
146
    {
147 2
        return $this->updatedOn ?: $this->updatedOn = new \DateTime($this->source->updated_on);
148
    }
149
150
    /**
151
     * Creation date
152
     *
153
     * @return \DateTime
154
     */
155 2
    public function getCreatedOn()
156
    {
157 2
        return $this->createdOn ?: $this->createdOn = new \DateTime($this->source->created_on);
158
    }
159
160
    /**
161
     * Get the record collection id
162
     *
163
     * @return integer
164
     */
165 2
    public function getCollectionId()
166
    {
167 2
        return $this->source->collection_id;
168
    }
169
170
    /**
171
     * Get the record UUID
172
     *
173
     * @return string
174
     */
175 2
    public function getUuid()
176
    {
177 2
        return $this->source->uuid;
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    public function getRecordCount()
184
    {
185
        return $this->recordCount !== null ?
186
            $this->recordCount :
187
            $this->recordCount = (isset($this->source->record_count) ? $this->source->record_count : count($this->getRecords()));
188
    }
189
190
    /**
191
     * @return Record[]|ArrayCollection
192
     */
193
    public function getRecords()
194
    {
195
        if (! isset($this->source->records)) {
196
            $this->records = new ArrayCollection();
197
        }
198
199
        return $this->records ?: $this->records = new ArrayCollection(Record::fromList((array) $this->source->records));
200
    }
201
202
    /**
203
     * @return Metadata[]|ArrayCollection
204
     */
205 2
    public function getMetadata()
206
    {
207 2
        if (! isset($this->source->metadata)) {
208 2
            $this->metadata = new ArrayCollection();
209 2
        }
210
211 2
        return $this->metadata ?: $this->metadata = new ArrayCollection(Metadata::fromList((array) $this->source->metadata));
212
    }
213
214
    /**
215
     * @return RecordStatus[]|ArrayCollection
216
     */
217
    public function getStatus()
218
    {
219
        if (! isset($this->status)) {
220
            $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...
221
                $this->getDataboxId(),
222
                $this->getStoryId()
223
            );
224
        }
225
226
        return $this->status;
227
    }
228
229
    /**
230
     * @return RecordCaption[]|ArrayCollection
231
     */
232
    public function getCaption()
233
    {
234
        if (! isset($this->caption)) {
235
            $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...
236
                $this->getDataboxId(),
237
                $this->getStoryId()
238
            );
239
        }
240
241
        return $this->caption;
242
    }
243
}
244