Test Setup Failed
Branch master (0141d3)
by Mathieu
07:06 queued 04:12
created

AbstractEvent::setStartDate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.2
cc 4
eloc 11
nc 5
nop 1
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 Collection $blocks
64
     */
65
    private $blocks;
0 ignored issues
show
Unused Code introduced by
The property $blocks is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
66
67
    /**
68
     * @var DateTime $startDate
69
     */
70
    private $startDate;
71
    /**
72
     * @var DateTime $startDate
73
     */
74
    private $endDate;
75
76
    /**
77
     * @var TranslationString $thumbnail
78
     */
79
    private $thumbnail;
0 ignored issues
show
Unused Code introduced by
The property $thumbnail is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
80
    /**
81
     * @var TranslationString $image
82
     */
83
    private $image;
0 ignored issues
show
Unused Code introduced by
The property $image is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
84
85
    /**
86
     * @var array $attachments
87
     */
88
    private $attachments;
0 ignored issues
show
Unused Code introduced by
The property $attachments is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
89
90
    /**
91
     * @param mixed $title The event title (localized).
92
     * @return TranslationString
93
     */
94
    public function setTitle($title)
95
    {
96
        $this->title = new TranslationString($title);
97
        return $this;
98
    }
99
100
    /**
101
     * @return TranslationString
102
     */
103
    public function title()
104
    {
105
        return $this->title;
106
    }
107
108
    /**
109
     * @param mixed $subtitle The event subtitle (localized).
110
     * @return Event Chainable
111
     */
112
    public function setSubtitle($subtitle)
113
    {
114
        $this->subtitle = new TranslationString($subtitle);
115
        return $this;
116
    }
117
118
    /**
119
     * @return TranslationString
120
     */
121
    public function subtitle()
122
    {
123
        return $this->subtitle;
124
    }
125
126
    /**
127
     * @param mixed $content The event content (localized).
128
     * @return Event Chainable
129
     */
130
    public function setContent($content)
131
    {
132
        $this->content = new TranslationString($content);
133
        return $this;
134
    }
135
136
    /**
137
     * @return TranslationString
138
     */
139
    public function content()
140
    {
141
        return $this->content;
142
    }
143
144
    /**
145
     * @param string|DateTime $startDate Event starting date.
146
     * @throws InvalidArgumentException If the timestamp is invalid.
147
     * @return EventInterface Chainable
148
     */
149 View Code Duplication
    public function setStartDate($startDate)
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...
150
    {
151
        if ($startDate === null) {
152
            $this->startDate = null;
153
            return $this;
154
        }
155
        if (is_string($startDate)) {
156
            $startDate = new DateTime($startDate);
157
        }
158
        if (!($startDate instanceof DateTimeInterface)) {
159
            throw new InvalidArgumentException(
160
                'Invalid "Start Date" value. Must be a date/time string or a DateTime object.'
161
            );
162
        }
163
        $this->startDate = $startDate;
164
        return $this;
165
    }
166
167
    /**
168
     * @return DateTime|null
169
     */
170
    public function startDate()
171
    {
172
        return $this->startDate;
173
    }
174
175
    /**
176
     * @param string|DateTime $endDate Event end date.
177
     * @throws InvalidArgumentException If the timestamp is invalid.
178
     * @return EventInterface Chainable
179
     */
180 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...
181
    {
182
        if ($endDate === null) {
183
            $this->endDate = null;
184
            return $this;
185
        }
186
        if (is_string($endDate)) {
187
            $endDate = new DateTime($endDate);
188
        }
189
        if (!($endDate instanceof DateTimeInterface)) {
190
            throw new InvalidArgumentException(
191
                'Invalid "End Date" value. Must be a date/time string or a DateTime object.'
192
            );
193
        }
194
        $this->endDate = $endDate;
195
        return $this;
196
    }
197
198
    /**
199
     * @return DateTime|null
200
     */
201
    public function endDate()
202
    {
203
        return $this->endDate;
204
    }
205
206
    /**
207
     * RoutableInterface > handle_route()
208
     *
209
     * @param string            $path     The request path.
210
     * @param RequestInterface  $request  PSR-7 (http) request.
211
     * @param ResponseInterface $response PSR-7 (http) response.
212
     * @throws InvalidArgumentException If the path is not a string.
213
     * @return callable|null Route dispatcher
214
     */
215 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...
216
    {
217
        if (!is_string($path)) {
218
            throw new InvalidArgumentException(
219
                'Route path must be a string'
220
            );
221
        }
222
        $match_path = $path == 'xxx';
223
224
        // Insert logic here...
225
        if ($match_path) {
226
            return function(RequestInterface $request, ResponseInterface $response) use ($path) {
227
                $response->write($path);
228
            };
229
        } else {
230
            return null;
231
        }
232
    }
233
234
235
    /**
236
     * @return TranslationString
237
     */
238
    public function defaultMetaTitle()
239
    {
240
        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...
241
    }
242
243
    /**
244
     * @return TranslationString
245
     */
246
    public function defaultMetaDescription()
247
    {
248
        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...
249
    }
250
251
    /**
252
     * @return TranslationString
253
     */
254
    public function defaultMetaImage()
255
    {
256
        return new TranslationString('');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Charcoal\Tra...\TranslationString(''); (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...
257
    }
258
}
259