Completed
Push — master ( 0141d3...4c89e9 )
by Mathieu
02:46
created

AbstractNews::image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Cms;
4
5
use \DateTime;
6
use \DateTimeInterface;
7
use \InvalidArgumentException;
8
9
use \Psr\Http\Message\RequestInterface;
10
use \Psr\Http\Message\ResponseInterface;
11
12
// Dependencies from `charcoal-translation`
13
use \Charcoal\Translation\TranslationString;
14
15
// Dependencies from `charcoal-base`
16
use \Charcoal\Object\Content;
17
use \Charcoal\Object\CategorizableInterface;
18
use \Charcoal\Object\CategorizableTrait;
19
use \Charcoal\Object\PublishableInterface;
20
use \Charcoal\Object\PublishableTrait;
21
22
// Dependencies from `charcoal-app`
23
use \Charcoal\App\Routable\RoutableInterface;
24
use \Charcoal\App\Routable\RoutableTrait;
25
26
// Intra-module (`charcoal-cms`) dependencies
27
use \Charcoal\Cms\MetatagInterface;
28
use \Charcoal\Cms\NewsInterface;
29
use \Charcoal\Cms\SearchableInterface;
30
use \Charcoal\Cms\SearchableTrait;
31
32
/**
33
 * News
34
 */
35
abstract class AbstractNews extends Content implements
36
    CategorizableInterface,
37
    MetatagInterface,
38
    NewsInterface,
39
    PublishableInterface,
40
    RoutableInterface,
41
    SearchableInterface
42
{
43
    use CategorizableTrait;
44
    use PublishableTrait;
45
    use MetatagTrait;
46
    use RoutableTrait;
47
    use SearchableTrait;
48
49
    /**
50
     * @var TranslationString $title
51
     */
52
    private $title;
53
54
    /**
55
     * @var TranslationString $title
56
     */
57
    private $subtitle;
58
59
    /**
60
     * @var TranslationString $content
61
     */
62
    private $content;
63
64
    /**
65
     * @var TranslationString $image
66
     */
67
    private $image;
68
69
    /**
70
     * @var DateTime $newsDate
71
     */
72
    private $newsDate;
73
74
    /**
75
     * @var Collection $documents
76
     */
77
    public $documents;
78
79
    /**
80
     * @var TranslationString $infoUrl
81
     */
82
    private $infoUrl;
83
84
    /**
85
     * @param mixed $title The news title (localized).
86
     * @return TranslationString
87
     */
88
    public function setTitle($title)
89
    {
90
        $this->title = new TranslationString($title);
91
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Charcoal\Cms\AbstractNews) is incompatible with the return type declared by the interface Charcoal\Cms\NewsInterface::setTitle of type Charcoal\Translation\TranslationString.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
92
    }
93
94
    /**
95
     * @return TranslationString
96
     */
97
    public function title()
98
    {
99
        return $this->title;
100
    }
101
102
    /**
103
     * @param mixed $subtitle The news subtitle (localized).
104
     * @return Event Chainable
105
     */
106
    public function setSubtitle($subtitle)
107
    {
108
        $this->subtitle = new TranslationString($subtitle);
109
        return $this;
110
    }
111
112
    /**
113
     * @return TranslationString
114
     */
115
    public function subtitle()
116
    {
117
        return $this->subtitle;
118
    }
119
120
    /**
121
     * @param mixed $content The news content (localized).
122
     * @return Event Chainable
123
     */
124
    public function setContent($content)
125
    {
126
        $this->content = new TranslationString($content);
127
        return $this;
128
    }
129
130
    /**
131
     * @return TranslationString
132
     */
133
    public function content()
134
    {
135
        return $this->content;
136
    }
137
138
    /**
139
     * @param mixed $image The section main image (localized).
140
     * @return Section Chainable
141
     */
142
    public function setImage($image)
143
    {
144
        $this->image = new TranslationString($image);
145
        return $this;
146
    }
147
148
    /**
149
     * @return TranslationString
150
     */
151
    public function image()
152
    {
153
        return $this->image;
154
    }
155
156
    /**
157
     * @param mixed $newsDate The news date.
158
     * @throws InvalidArgumentException If the timestamp is invalid.
159
     * @return ObjectRevision Chainable
160
     */
161 View Code Duplication
    public function setNewsDate($newsDate)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        if ($newsDate === null) {
164
            $this->newsDate = null;
165
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Charcoal\Cms\AbstractNews) is incompatible with the return type declared by the interface Charcoal\Cms\NewsInterface::setNewsDate of type Charcoal\Cms\ObjectRevision.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
166
        }
167
        if (is_string($newsDate)) {
168
            $newsDate = new DateTime($newsDate);
169
        }
170
        if (!($newsDate instanceof DateTimeInterface)) {
171
            throw new InvalidArgumentException(
172
                'Invalid "Revision Date" value. Must be a date/time string or a DateTimeInterface object.'
173
            );
174
        }
175
        $this->newsDate = $newsDate;
176
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Charcoal\Cms\AbstractNews) is incompatible with the return type declared by the interface Charcoal\Cms\NewsInterface::setNewsDate of type Charcoal\Cms\ObjectRevision.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
177
    }
178
179
    /**
180
     * @return DateTime|null
181
     */
182
    public function newsDate()
183
    {
184
        return $this->newsDate;
185
    }
186
187
    /**
188
     * @param mixed $url The info URL (news source or where to find more information; localized).
189
     * @return NewsInterface Chainable
190
     */
191
    public function setInfoUrl($url)
192
    {
193
        $this->infoUrl = new TranslationString($url);
194
        return $this;
195
    }
196
197
    /**
198
     * @return TranslationString
199
     */
200
    public function infoUrl()
201
    {
202
        return $this->infoUrl;
203
    }
204
205
    /**
206
     * MetatagTrait > canonical_url
207
     *
208
     * @return string
209
     * @todo
210
     */
211
    public function canonicalUrl()
212
    {
213
        return '';
214
    }
215
216
    /**
217
     * RoutableInterface > handle_route()
218
     *
219
     * @param string            $path     The request path.
220
     * @param RequestInterface  $request  PSR-7 (http) request.
221
     * @param ResponseInterface $response PSR-7 (http) response.
222
     * @throws InvalidArgumentException If the path is not a string.
223
     * @return callable|null Route dispatcher
224
     */
225 View Code Duplication
    public function routeHandler($path, RequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
    {
227
        if (!is_string($path)) {
228
            throw new InvalidArgumentException(
229
                'Route path must be a string'
230
            );
231
        }
232
        $match_path = $path == 'xxx';
233
234
        // Insert logic here...
235
        if ($match_path) {
236
            return function(RequestInterface $request, ResponseInterface $response) use ($path) {
237
                $response->write($path);
238
            };
239
        } else {
240
            return null;
241
        }
242
    }
243
244
    /**
245
     * @return TranslationString
246
     */
247
    public function defaultMetaTitle()
248
    {
249
        return $this->title();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->title(); (Charcoal\Translation\TranslationString) is incompatible with the return type declared by the interface Charcoal\Cms\MetatagInterface::defaultMetaTitle of type Charcoal\Cms\TranslationString.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
250
    }
251
252
    /**
253
     * @return TranslationString
254
     */
255
    public function defaultMetaDescription()
256
    {
257
        return $this->content();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->content(); (Charcoal\Translation\TranslationString) is incompatible with the return type declared by the interface Charcoal\Cms\MetatagInte...:defaultMetaDescription of type Charcoal\Cms\TranslationString.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
258
    }
259
260
    /**
261
     * @return TranslationString
262
     */
263
    public function defaultMetaImage()
264
    {
265
        return $this->image();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->image(); (Charcoal\Translation\TranslationString) is incompatible with the return type declared by the interface Charcoal\Cms\MetatagInterface::defaultMetaImage of type Charcoal\Cms\TranslationString.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
266
    }
267
}
268