Completed
Pull Request — master (#9)
by ARCANEDEV
03:16
created

Url::setChangeFreq()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php namespace Arcanedev\LaravelSitemap\Entities;
2
3
use DateTime;
4
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract;
5
/**
6
 * Class     Url
7
 *
8
 * @package  Arcanedev\LaravelSitemap\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Url implements UrlContract
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /** @var  string  */
19
    protected $url;
20
21
    /** @var  string|null */
22
    protected $title;
23
24
    /** @var  \Carbon\Carbon */
25
    protected $lastModDate;
26
27
    /** @var  string */
28
    protected $changeFrequency;
29
30
    /** @var  float */
31
    protected $priority;
32
33
    /* -----------------------------------------------------------------
34
     |  Constructor
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Url constructor.
40
     *
41
     * @param  string  $loc
42
     */
43 46
    public function __construct($loc)
44
    {
45 46
        $this->setLoc($loc);
46 46
        $this->setLastMod(new DateTime);
47 46
        $this->setChangeFreq(static::CHANGE_FREQUENCY_DAILY);
48 46
        $this->setPriority(0.8);
49 46
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Getters & Setters
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Get the url.
58
     *
59
     * @return string
60
     */
61 36
    public function getLoc()
62
    {
63 36
        return $this->url;
64
    }
65
66
    /**
67
     * Set the url.
68
     *
69
     * @param  string  $url
70
     *
71
     * @return self
72
     */
73 46
    public function setLoc($url)
74
    {
75 46
        $this->url = $url;
76
77 46
        return $this;
78
    }
79
80
    /**
81
     * Get the last modification date.
82
     *
83
     * @return DateTime
84
     */
85 24
    public function getLastMod()
86
    {
87 24
        return $this->lastModDate;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->lastModDate; (Carbon\Carbon) is incompatible with the return type declared by the interface Arcanedev\LaravelSitemap...ntities\Url::getLastMod of type DateTimeInterface.

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...
88
    }
89
90
    /**
91
     * Set the last modification date.
92
     *
93
     * @param  string|DateTime  $lastModDate
94
     * @param  string           $format
95
     *
96
     * @return self
97
     */
98 46
    public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s')
99
    {
100 46
        $this->lastModDate = gettype($lastModDate) === 'string'
0 ignored issues
show
Documentation Bug introduced by
It seems like gettype($lastModDate) ==...ModDate) : $lastModDate of type object<DateTime> is incompatible with the declared type object<Carbon\Carbon> of property $lastModDate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101 32
            ? DateTime::createFromFormat($format, $lastModDate)
102 46
            : $lastModDate;
103
104 46
        return $this;
105
    }
106
107
    /**
108
     * Get the change frequency.
109
     *
110
     * @return string
111
     */
112 22
    public function getChangeFreq()
113
    {
114 22
        return $this->changeFrequency;
115
    }
116
117
    /**
118
     * Set the change frequency.
119
     *
120
     * @param  string  $changeFreq
121
     *
122
     * @return self
123
     */
124 46
    public function setChangeFreq($changeFreq)
125
    {
126 46
        $this->changeFrequency = $changeFreq;
127
128 46
        return $this;
129
    }
130
131
    /**
132
     * Get the priority.
133
     *
134
     * @return float
135
     */
136 10
    public function getPriority()
137
    {
138 10
        return $this->priority;
139
    }
140
141
    /**
142
     * Set the priority.
143
     *
144
     * @param  float  $priority
145
     *
146
     * @return self
147
     */
148 46
    public function setPriority($priority)
149
    {
150 46
        $this->priority = (float) $priority;
151
152 46
        return $this;
153
    }
154
155
    /**
156
     * Get the title.
157
     *
158
     * @return string|null
159
     */
160 4
    public function getTitle()
161
    {
162 4
        return $this->title;
163
    }
164
165
    /**
166
     * Get the title.
167
     *
168
     * @param  string  $title
169
     *
170
     * @return self
171
     */
172 26
    public function setTitle($title)
173
    {
174 26
        $this->title = $title;
175
176 26
        return $this;
177
    }
178
179
    /* -----------------------------------------------------------------
180
     |  Main Methods
181
     | -----------------------------------------------------------------
182
     */
183
184
    /**
185
     * Create a sitemap url instance.
186
     *
187
     * @param  string  $loc
188
     *
189
     * @return \Arcanedev\LaravelSitemap\Entities\Url
190
     */
191 30
    public static function create($loc)
192
    {
193 30
        return new static($loc);
194
    }
195
196
    /**
197
     * Get the collection of items as a plain array.
198
     *
199
     * @return array
200
     */
201 12
    public function toArray()
202
    {
203
        return [
204 12
            'loc'        => $this->getLoc(),
205 12
            'lastmod'    => $this->getLastMod()->format(DateTime::ATOM),
206 12
            'changefreq' => $this->getChangeFreq(),
207 12
            'priority'   => $this->priority,
208
        ];
209
    }
210
211
    /**
212
     * Get the sitemap url as JSON.
213
     *
214
     * @param  int  $options
215
     *
216
     * @return string
217
     */
218 2
    public function toJson($options = 0)
219
    {
220 2
        return json_encode($this->jsonSerialize(), $options);
221
    }
222
223
    /**
224
     * Convert the object into something JSON serializable.
225
     *
226
     * @return array
227
     */
228 2
    public function jsonSerialize()
229
    {
230 2
        return $this->toArray();
231
    }
232
}
233