Completed
Push — master ( 4f82af...913b4d )
by
unknown
03:04
created

AbstractNews::preUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
use \Charcoal\Object\RoutableInterface;
22
use \Charcoal\Object\RoutableTrait;
23
24
// Intra-module (`charcoal-cms`) dependencies
25
use \Charcoal\Cms\MetatagInterface;
26
use \Charcoal\Cms\NewsInterface;
27
use \Charcoal\Cms\SearchableInterface;
28
use \Charcoal\Cms\SearchableTrait;
29
30
/**
31
 * News
32
 */
33
abstract class AbstractNews extends Content implements
34
    CategorizableInterface,
35
    MetatagInterface,
36
    NewsInterface,
37
    PublishableInterface,
38
    RoutableInterface,
39
    SearchableInterface
40
{
41
    use CategorizableTrait;
42
    use PublishableTrait;
43
    use MetatagTrait;
44
    use RoutableTrait;
45
    use SearchableTrait;
46
47
    /**
48
     * @var TranslationString $title
49
     */
50
    private $title;
51
52
    /**
53
     * @var TranslationString $title
54
     */
55
    private $subtitle;
56
57
    /**
58
     * @var TranslationString $content
59
     */
60
    private $content;
61
62
    /**
63
     * @var TranslationString $image
64
     */
65
    private $image;
66
67
    /**
68
     * @var DateTime $newsDate
69
     */
70
    private $newsDate;
71
72
    /**
73
     * @var TranslationString $infoUrl
74
     */
75
    private $infoUrl;
76
77
    /**
78
     * Generate a slug on preSave
79
     * @see RoutableTrait
80
     * @return parent::preSave
0 ignored issues
show
Documentation introduced by
The doc-type parent::preSave could not be parsed: Unknown type name "parent::preSave" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
81
     */
82
    public function preSave()
83
    {
84
        $this->setSlug($this->generateSlug());
85
86
        return parent::preSave();
87
    }
88
89
    /**
90
     * Generate a slug on preUpdate
91
     * @see RoutableTrait
92
     * @param array $properties Properties to update.
93
     * @return parent::preUpdate
0 ignored issues
show
Documentation introduced by
The doc-type parent::preUpdate could not be parsed: Unknown type name "parent::preUpdate" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
94
     */
95
    public function preUpdate(array $properties = null)
96
    {
97
        $this->setSlug($this->generateSlug());
98
99
        return parent::preUpdate($properties);
100
    }
101
102
103
    /**
104
     * @param mixed $title The news title (localized).
105
     * @return TranslationString
106
     */
107
    public function setTitle($title)
108
    {
109
        $this->title = new TranslationString($title);
110
        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...
111
    }
112
113
    /**
114
     * @return TranslationString
115
     */
116
    public function title()
117
    {
118
        return $this->title;
119
    }
120
121
    /**
122
     * @param mixed $subtitle The news subtitle (localized).
123
     * @return Event Chainable
124
     */
125
    public function setSubtitle($subtitle)
126
    {
127
        $this->subtitle = new TranslationString($subtitle);
128
        return $this;
129
    }
130
131
    /**
132
     * @return TranslationString
133
     */
134
    public function subtitle()
135
    {
136
        return $this->subtitle;
137
    }
138
139
    /**
140
     * @param mixed $content The news content (localized).
141
     * @return Event Chainable
142
     */
143
    public function setContent($content)
144
    {
145
        $this->content = new TranslationString($content);
146
        return $this;
147
    }
148
149
    /**
150
     * @return TranslationString
151
     */
152
    public function content()
153
    {
154
        return $this->content;
155
    }
156
157
    /**
158
     * @param mixed $image The section main image (localized).
159
     * @return Section Chainable
160
     */
161
    public function setImage($image)
162
    {
163
        $this->image = new TranslationString($image);
164
        return $this;
165
    }
166
167
    /**
168
     * @return TranslationString
169
     */
170
    public function image()
171
    {
172
        return $this->image;
173
    }
174
175
    /**
176
     * @param mixed $template The section template (ident).
177
     * @return SectionInterface Chainable
178
     */
179
    public function setTemplateIdent($template)
180
    {
181
        $this->templateIdent = $template;
0 ignored issues
show
Bug introduced by
The property templateIdent cannot be accessed from this context as it is declared private in class Charcoal\View\ViewableTrait.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
182
        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 of the parent method Charcoal\Model\AbstractModel::setTemplateIdent of type Charcoal\View\ViewableTrait.

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...
183
    }
184
185
    /**
186
     * @return mixed
187
     */
188
    public function templateIdent()
189
    {
190
        if (!$this->templateIdent) {
0 ignored issues
show
Bug introduced by
The property templateIdent cannot be accessed from this context as it is declared private in class Charcoal\View\ViewableTrait.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
191
            $metadata = $this->metadata();
192
            return $metadata['template_ident'];
193
        }
194
        return $this->templateIdent;
0 ignored issues
show
Bug introduced by
The property templateIdent cannot be accessed from this context as it is declared private in class Charcoal\View\ViewableTrait.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
195
    }
196
197
    /**
198
     * @param array|string $templateOptions Extra template options, if any.
199
     * @return SectionInterface Chainable
200
     */
201
    public function setTemplateOptions($templateOptions)
202
    {
203
        $this->templateOptions = $templateOptions;
0 ignored issues
show
Bug introduced by
The property templateOptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
204
        return $this;
205
    }
206
207
    /**
208
     * @return array
209
     */
210
    public function templateOptions()
211
    {
212
        if (!$this->templateOptions) {
213
            $metadata = $this->metadata();
214
            return $metadata['template_options'];
215
        }
216
        return $this->templateOptions;
217
    }
218
219
    /**
220
     * @param mixed $newsDate The news date.
221
     * @throws InvalidArgumentException If the timestamp is invalid.
222
     * @return ObjectRevision Chainable
223
     */
224
    public function setNewsDate($newsDate)
225
    {
226
        if ($newsDate === null) {
227
            $this->newsDate = null;
228
            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...
229
        }
230
        if (is_string($newsDate)) {
231
            $newsDate = new DateTime($newsDate);
232
        }
233
        if (!($newsDate instanceof DateTimeInterface)) {
234
            throw new InvalidArgumentException(
235
                'Invalid "Revision Date" value. Must be a date/time string or a DateTimeInterface object.'
236
            );
237
        }
238
        $this->newsDate = $newsDate;
239
        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...
240
    }
241
242
    /**
243
     * @return DateTime|null
244
     */
245
    public function newsDate()
246
    {
247
        return $this->newsDate;
248
    }
249
250
    /**
251
     * @param mixed $url The info URL (news source or where to find more information; localized).
252
     * @return NewsInterface Chainable
253
     */
254
    public function setInfoUrl($url)
255
    {
256
        $this->infoUrl = new TranslationString($url);
257
        return $this;
258
    }
259
260
    /**
261
     * @return TranslationString
262
     */
263
    public function infoUrl()
264
    {
265
        return $this->infoUrl;
266
    }
267
268
    /**
269
     * MetatagTrait > canonical_url
270
     *
271
     * @return string
272
     * @todo
273
     */
274
    public function canonicalUrl()
275
    {
276
        return '';
277
    }
278
279
    /**
280
     * RoutableInterface > handle_route()
281
     *
282
     * @param string            $path     The request path.
283
     * @param RequestInterface  $request  PSR-7 (http) request.
284
     * @param ResponseInterface $response PSR-7 (http) response.
285
     * @throws InvalidArgumentException If the path is not a string.
286
     * @return callable|null Route dispatcher
287
     */
288
    public function routeHandler($path, RequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
289
    {
290
        if (!is_string($path)) {
291
            throw new InvalidArgumentException(
292
                'Route path must be a string'
293
            );
294
        }
295
        $match_path = $path == 'xxx';
296
297
        // Insert logic here...
298
        if ($match_path) {
299
            return function(RequestInterface $request, ResponseInterface $response) use ($path) {
300
                $response->write($path);
301
            };
302
        } else {
303
            return null;
304
        }
305
    }
306
307
    /**
308
     * @return TranslationString
309
     */
310
    public function defaultMetaTitle()
311
    {
312
        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...
313
    }
314
315
    /**
316
     * @return TranslationString
317
     */
318
    public function defaultMetaDescription()
319
    {
320
        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...
321
    }
322
323
    /**
324
     * @return TranslationString
325
     */
326
    public function defaultMetaImage()
327
    {
328
        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...
329
    }
330
}
331