Completed
Push — master ( f5068e...e4f693 )
by Petre
02:00
created

SitemapResourceBuilder::withRelativeLocation()   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
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace DL\SitemapBundle\Service;
5
6
use DL\SitemapBundle\Definition\SitemapResource;
7
8
/**
9
 * Handles the construction of sitemap resources.
10
 *
11
 * @package DL\SitemapBundle\Service
12
 * @author  Petre Pătrașc <[email protected]>
13
 */
14
class SitemapResourceBuilder
15
{
16
    /**
17
     * The title of the resource.
18
     *
19
     * @var string
20
     */
21
    protected $title;
22
23
    /**
24
     * The absolute URL of where the resource can be accessed.
25
     *
26
     * @var string
27
     */
28
    protected $location;
29
30
    /**
31
     * The date at which the resource was last modified.
32
     *
33
     * @var \DateTime
34
     */
35
    protected $lastModified;
36
37
    /**
38
     * The change frequency at which this resource is updated.
39
     *
40
     * @see ChangeFrequencyEnum
41
     * @var string
42
     */
43
    protected $changeFrequency;
44
45
    /**
46
     * The priority that this resource should be given within a sitemap.
47
     *
48
     * @var float
49
     */
50
    protected $priority;
51
52
    /**
53
     * @var SitemapResourceValidator
54
     */
55
    protected $validator;
56
57
    /**
58
     * The location prefix - used when creating resources
59
     * with relative locations.
60
     *
61
     * @var string
62
     */
63
    protected $locationPrefix;
64
65
    /**
66
     * SitemapResourceBuilder constructor.
67
     *
68
     * @param SitemapResourceValidator $validator
69
     * @param string                   $locationPrefix
70
     */
71 6
    public function __construct(SitemapResourceValidator $validator, string $locationPrefix)
72
    {
73 6
        $this->validator      = $validator;
74 6
        $this->locationPrefix = $locationPrefix;
75 6
        $this->clear();
76 6
    }
77
78
    /**
79
     * Clear all of the builder properties to ensure a default state
80
     * between multiple executions.
81
     */
82 6
    private function clear(): void
83
    {
84 6
        $this->title           = '';
85 6
        $this->location        = '';
86 6
        $this->lastModified    = null;
87 6
        $this->changeFrequency = '';
88 6
        $this->priority        = -1;
0 ignored issues
show
Documentation Bug introduced by
The property $priority was declared of type double, but -1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
89 6
    }
90
91
    /**
92
     * Build an instance of a sitemap resource and validate it.
93
     *
94
     * @return SitemapResource
95
     */
96 2
    public function build(): SitemapResource
97
    {
98 2
        $resource = new SitemapResource(
99 2
            $this->title,
100 2
            $this->location,
101 2
            $this->lastModified,
102 2
            $this->changeFrequency,
103 2
            $this->priority
104
        );
105
106 2
        $this->clear();
107 2
        $this->validator->validate($resource);
108
109 2
        return $resource;
110
    }
111
112
    /**
113
     * @param string $title
114
     *
115
     * @return SitemapResourceBuilder
116
     */
117 3
    public function withTitle(string $title): SitemapResourceBuilder
118
    {
119 3
        $this->title = $title;
120
121 3
        return $this;
122
    }
123
124
    /**
125
     * @param string $location
126
     *
127
     * @return SitemapResourceBuilder
128
     */
129 1
    public function withAbsoluteLocation(string $location): SitemapResourceBuilder
130
    {
131 1
        $this->location = $location;
132
133 1
        return $this;
134
    }
135
136
    /**
137
     * @param string $location
138
     *
139
     * @return SitemapResourceBuilder
140
     */
141 4
    public function withRelativeLocation(string $location): SitemapResourceBuilder
142
    {
143 4
        $this->location = $this->locationPrefix . $location;
144
145 4
        return $this;
146
    }
147
148
    /**
149
     * @param string $changeFrequency
150
     *
151
     * @return SitemapResourceBuilder
152
     */
153 3
    public function withChangeFrequency(string $changeFrequency): SitemapResourceBuilder
154
    {
155 3
        $this->changeFrequency = $changeFrequency;
156
157 3
        return $this;
158
    }
159
160
    /**
161
     * @param \DateTime $lastModified
162
     *
163
     * @return SitemapResourceBuilder
164
     */
165 3
    public function withLastModified(\DateTime $lastModified): SitemapResourceBuilder
166
    {
167 3
        $this->lastModified = $lastModified;
168
169 3
        return $this;
170
    }
171
172
    /**
173
     * @param float $priority
174
     *
175
     * @return SitemapResourceBuilder
176
     */
177 3
    public function withPriority(float $priority): SitemapResourceBuilder
178
    {
179 3
        $this->priority = $priority;
180
181 3
        return $this;
182
    }
183
}
184