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

SitemapManager::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\LaravelSitemap;
2
3
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap as SitemapContract;
4
use Arcanedev\LaravelSitemap\Contracts\SitemapManager as SitemapManagerContract;
5
use Arcanedev\LaravelSitemap\Entities\Sitemap;
6
use DOMDocument;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class     SitemapManager
11
 *
12
 * @package  Arcanedev\LaravelSitemap
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class SitemapManager implements SitemapManagerContract
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /** @var  \Illuminate\Support\Collection */
23
    protected $sitemaps;
24
25
    /** @var  string */
26
    protected $format = 'xml';
27
28
    /* -----------------------------------------------------------------
29
     |  Constructor
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * SitemapManager constructor.
35
     */
36 34
    public function __construct()
37
    {
38 34
        $this->sitemaps = new Collection;
39 34
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Getters & Setters
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Set the format.
48
     *
49
     * @param  string  $format
50
     *
51
     * @return self
52
     */
53 8
    public function format($format)
54
    {
55 8
        $this->format = $format;
56
57 8
        return $this;
58
    }
59
60
    /* -----------------------------------------------------------------
61
     |  Main Methods
62
     | -----------------------------------------------------------------
63
     */
64
65
    /**
66
     * Create and add a sitemap to the collection.
67
     *
68
     * @param  string    $name
69
     * @param  callable  $callback
70
     *
71
     * @return self
72
     */
73 2
    public function create($name, callable $callback)
74
    {
75 2
        return $this->add($name, tap((new Sitemap)->setPath($name), $callback));
76
    }
77
78
    /**
79
     * Add a sitemap to the collection.
80
     *
81
     * @param  string                                                $name
82
     * @param  \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap  $sitemap
83
     *
84
     * @return self
85
     */
86 28
    public function add($name, SitemapContract $sitemap)
87
    {
88 28
        $this->sitemaps->put($name, $sitemap);
89
90 28
        return $this;
91
    }
92
93
    /**
94
     * Get the sitemaps collection.
95
     *
96
     * @return \Illuminate\Support\Collection
97
     */
98 10
    public function all()
99
    {
100 10
        return $this->sitemaps;
101
    }
102
103
    /**
104
     * Get a sitemap instance.
105
     *
106
     * @param  string      $name
107
     * @param  mixed|null  $default
108
     *
109
     * @return mixed
110
     */
111 4
    public function get($name, $default = null)
112
    {
113 4
        return $this->sitemaps->get($name, $default);
114
    }
115
116
    /**
117
     * Check if a sitemap exists.
118
     *
119
     * @param  string  $name
120
     *
121
     * @return bool
122
     */
123 2
    public function has($name)
124
    {
125 2
        return $this->sitemaps->has($name);
126
    }
127
128
    /**
129
     * Remove a sitemap from the collection by key.
130
     *
131
     * @param  string|array  $names
132
     *
133
     * @return self
134
     */
135 2
    public function forget($names)
136
    {
137 2
        $this->sitemaps->forget($names);
138
139 2
        return $this;
140
    }
141
142
    /**
143
     * Get the sitemaps count.
144
     *
145
     * @return int
146
     */
147 8
    public function count()
148
    {
149 8
        return $this->sitemaps->count();
150
    }
151
152
    /**
153
     * Render the sitemaps.
154
     *
155
     * @param  string  $name
156
     *
157
     * @return string|null
158
     */
159 18
    public function render($name = null)
160
    {
161 18
        return SitemapBuilder::make()->build($name, $this->sitemaps, $this->format);
162
    }
163
164
    /**
165
     * Save the sitemaps.
166
     *
167
     * @param  string       $path
168
     * @param  string|null  $name
169
     *
170
     * @return self
171
     */
172 6
    public function save($path, $name = null)
173
    {
174 6
        if ($this->sitemaps->isNotEmpty()) {
175 6
            file_put_contents($path, $this->render($name));
176
        }
177
178 6
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Arcanedev\LaravelSitemap\SitemapManager) is incompatible with the return type declared by the interface Arcanedev\LaravelSitemap...ts\SitemapManager::save of type boolean.

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...
179
    }
180
181
    /**
182
     * Get the collection of items as a plain array.
183
     *
184
     * @return array
185
     */
186 4
    public function toArray()
187
    {
188 4
        return $this->all()->toArray();
189
    }
190
191
    /**
192
     * Get the collection of sitemaps as JSON.
193
     *
194
     * @param  int  $options
195
     *
196
     * @return string
197
     */
198 2
    public function toJson($options = 0)
199
    {
200 2
        return json_encode($this->jsonSerialize(), $options);
201
    }
202
203
    /**
204
     * Convert the object into something JSON serializable.
205
     *
206
     * @return array
207
     */
208 2
    public function jsonSerialize()
209
    {
210 2
        return $this->toArray();
211
    }
212
}
213