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

AbstractEvent::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 \Exception;
8
use \InvalidArgumentException;
9
10
use \Psr\Http\Message\RequestInterface;
11
use \Psr\Http\Message\ResponseInterface;
12
13
// Module `charcoal-base` dependencies
14
use \Charcoal\Object\Content;
15
use \Charcoal\Object\CategorizableInterface;
16
use \Charcoal\Object\CategorizableTrait;
17
use \Charcoal\Object\PublishableInterface;
18
use \Charcoal\Object\PublishableTrait;
19
20
// Dependencies from `charcoal-app`
21
use \Charcoal\App\Routable\RoutableInterface;
22
use \Charcoal\App\Routable\RoutableTrait;
23
24
// Module `charcoal-translation` depdencies
25
use \Charcoal\Translation\TranslationString;
26
27
// Intra-module (`charcoal-cms`) dependencies
28
use \Charcoal\Cms\MetatagInterface;
29
use \Charcoal\Cms\SearchableInterface;
30
31
/**
32
 *
33
 */
34
abstract class AbstractEvent extends Content implements
35
    CategorizableInterface,
36
    MetatagInterface,
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 $startDate
69
     */
70
    private $startDate;
71
    /**
72
     * @var DateTime $startDate
73
     */
74
    private $endDate;
75
76
    /**
77
     * @param mixed $title The event title (localized).
78
     * @return TranslationString
79
     */
80
    public function setTitle($title)
81
    {
82
        $this->title = new TranslationString($title);
83
        return $this;
84
    }
85
86
    /**
87
     * @return TranslationString
88
     */
89
    public function title()
90
    {
91
        return $this->title;
92
    }
93
94
    /**
95
     * @param mixed $subtitle The event subtitle (localized).
96
     * @return Event Chainable
97
     */
98
    public function setSubtitle($subtitle)
99
    {
100
        $this->subtitle = new TranslationString($subtitle);
101
        return $this;
102
    }
103
104
    /**
105
     * @return TranslationString
106
     */
107
    public function subtitle()
108
    {
109
        return $this->subtitle;
110
    }
111
112
    /**
113
     * @param mixed $content The event content (localized).
114
     * @return Event Chainable
115
     */
116
    public function setContent($content)
117
    {
118
        $this->content = new TranslationString($content);
119
        return $this;
120
    }
121
122
    /**
123
     * @return TranslationString
124
     */
125
    public function content()
126
    {
127
        return $this->content;
128
    }
129
130
    /**
131
     * @param mixed $image The section main image (localized).
132
     * @return Section Chainable
133
     */
134
    public function setImage($image)
135
    {
136
        $this->image = new TranslationString($image);
137
        return $this;
138
    }
139
140
    /**
141
     * @return TranslationString
142
     */
143
    public function image()
144
    {
145
        return $this->image;
146
    }
147
148
    /**
149
     * @param string|DateTime $startDate Event starting date.
150
     * @throws InvalidArgumentException If the timestamp is invalid.
151
     * @return EventInterface Chainable
152
     */
153
    public function setStartDate($startDate)
154
    {
155
        if ($startDate === null) {
156
            $this->startDate = null;
157
            return $this;
158
        }
159
        if (is_string($startDate)) {
160
            $startDate = new DateTime($startDate);
161
        }
162
        if (!($startDate instanceof DateTimeInterface)) {
163
            throw new InvalidArgumentException(
164
                'Invalid "Start Date" value. Must be a date/time string or a DateTime object.'
165
            );
166
        }
167
        $this->startDate = $startDate;
168
        return $this;
169
    }
170
171
    /**
172
     * @return DateTime|null
173
     */
174
    public function startDate()
175
    {
176
        return $this->startDate;
177
    }
178
179
    /**
180
     * @param string|DateTime $endDate Event end date.
181
     * @throws InvalidArgumentException If the timestamp is invalid.
182
     * @return EventInterface Chainable
183
     */
184 View Code Duplication
    public function setEndDate($endDate)
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...
185
    {
186
        if ($endDate === null) {
187
            $this->endDate = null;
188
            return $this;
189
        }
190
        if (is_string($endDate)) {
191
            $endDate = new DateTime($endDate);
192
        }
193
        if (!($endDate instanceof DateTimeInterface)) {
194
            throw new InvalidArgumentException(
195
                'Invalid "End Date" value. Must be a date/time string or a DateTime object.'
196
            );
197
        }
198
        $this->endDate = $endDate;
199
        return $this;
200
    }
201
202
    /**
203
     * @return DateTime|null
204
     */
205
    public function endDate()
206
    {
207
        return $this->endDate;
208
    }
209
210
    /**
211
     * RoutableInterface > handle_route()
212
     *
213
     * @param string            $path     The request path.
214
     * @param RequestInterface  $request  PSR-7 (http) request.
215
     * @param ResponseInterface $response PSR-7 (http) response.
216
     * @throws InvalidArgumentException If the path is not a string.
217
     * @return callable|null Route dispatcher
218
     */
219 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...
220
    {
221
        if (!is_string($path)) {
222
            throw new InvalidArgumentException(
223
                'Route path must be a string'
224
            );
225
        }
226
        $match_path = $path == 'xxx';
227
228
        // Insert logic here...
229
        if ($match_path) {
230
            return function(RequestInterface $request, ResponseInterface $response) use ($path) {
231
                $response->write($path);
232
            };
233
        } else {
234
            return null;
235
        }
236
    }
237
238
239
    /**
240
     * @return TranslationString
241
     */
242
    public function defaultMetaTitle()
243
    {
244
        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...
245
    }
246
247
    /**
248
     * @return TranslationString
249
     */
250
    public function defaultMetaDescription()
251
    {
252
        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...
253
    }
254
255
    /**
256
     * @return TranslationString
257
     */
258
    public function defaultMetaImage()
259
    {
260
        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...
261
    }
262
}
263