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

Feed::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
use PhraseanetSDK\EntityManager;
16
17
class Feed
18
{
19
20 1
    public static function fromList(EntityManager $entityManager, array $values)
21
    {
22 1
        $feeds = array();
23
24 1
        foreach ($values as $value) {
25 1
            $feeds[$value->id] = self::fromValue($entityManager, $value);
26 1
        }
27
28 1
        return $feeds;
29
    }
30
31 2
    public static function fromValue(EntityManager $entityManager, \stdClass $value)
32
    {
33 2
        return new self($entityManager, $value);
34
    }
35
36
    /**
37
     * @var EntityManager
38
     */
39
    protected $entityManager;
40
41
    /**
42
     * @var \stdClass
43
     */
44
    protected $source;
45
46
    /**
47
     * @var \DateTime|null
48
     */
49
    protected $createdOn;
50
51
    /**
52
     * @var \DateTime|null
53
     */
54
    protected $updatedOn;
55
56
    /**
57
     * @param EntityManager $entityManager
58
     * @param \stdClass $source
59
     */
60 2
    public function __construct(EntityManager $entityManager, \stdClass $source)
61
    {
62 2
        $this->entityManager = $entityManager;
63 2
        $this->source = $source;
64 2
    }
65
66
    /**
67
     * @return \stdClass
68
     */
69
    public function getRawData()
70
    {
71 2
        return $this->source;
72
    }
73 2
74
    /**
75
     * The feed id
76
     *
77
     * @return integer
78
     */
79
    public function getId()
80
    {
81 2
        return $this->source->id;
82
    }
83 2
84
    /**
85
     * The feed title
86
     *
87
     * @return string
88
     */
89
    public function getTitle()
90
    {
91 2
        return $this->source->title;
92
    }
93 2
94
    /**
95
     * The feed icon
96
     *
97
     * @return string
98
     */
99
    public function getIcon()
100
    {
101 2
        return $this->source->icon;
102
    }
103 2
104
    /**
105
     * The feed subtitle
106
     *
107
     * @return string
108
     */
109
    public function getSubTitle()
110
    {
111 2
        return $this->source->subtitle;
112
    }
113 2
114
    /**
115
     * Get the total entries of the feed
116
     *
117
     * @return integer
118
     */
119
    public function getTotalEntries()
120
    {
121 2
        return $this->source->total_entries;
122
    }
123 2
124
    /**
125
     * Creation date
126
     *
127
     * @return \DateTime
128
     */
129
    public function getCreatedOn()
130
    {
131 2
        return $this->createdOn ?: $this->createdOn = new \DateTime($this->source->created_on);
132
    }
133 2
134
    /**
135
     * Last updated date
136
     *
137
     * @return \DateTime
138
     */
139
    public function getUpdatedOn()
140
    {
141 2
        return $this->updatedOn ?: $this->updatedOn = new \DateTime($this->source->updated_on);
142
    }
143 2
144
    /**
145
     * Tell whether the feed is public or not
146
     *
147
     * @return Boolean
148
     */
149
    public function isPublic()
150
    {
151 2
        return $this->source->public;
152
    }
153 2
154
    /**
155
     * Tell whether the feed is a read only feed
156
     *
157
     * @return Boolean
158
     */
159
    public function isReadonly()
160
    {
161 2
        return $this->source->readonly;
162
    }
163 2
164
    /**
165
     * Tell whether the feed is deletable
166
     *
167
     * @return Boolean
168
     */
169
    public function isDeletable()
170
    {
171
        return $this->source->deletable;
172
    }
173
174
    /**
175
     * @param int $offset
176
     * @param int $perPage
177
     * @return FeedEntry[]|ArrayCollection
178
     */
179
    public function getEntries($offset = 0, $perPage = 0)
180
    {
181
        return $this->entityManager
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 findByFeed() does only exist in the following sub-classes of PhraseanetSDK\AbstractRepository: PhraseanetSDK\Repository\Entry. 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...
182
            ->getRepository('entry')
183
            ->findByFeed($this->getId(), $offset, $perPage);
184
    }
185
}
186