Sitemap::addResource()   A
last analyzed

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\Definition;
5
6
use DL\SitemapBundle\Exception\SitemapException;
7
8
/**
9
 * The representation of an entire sitemap collection.
10
 *
11
 * @package DL\SitemapBundle\Definition
12
 * @author  Petre Pătrașc <[email protected]>
13
 */
14
class Sitemap
15
{
16
    /**
17
     * The resources that make up the sitemap collection.
18
     *
19
     * @see SitemapResource
20
     * @var array
21
     */
22
    protected $resources;
23
24
    /**
25
     * Sitemap constructor.
26
     *
27
     * @param array $entries
28
     */
29 8
    public function __construct(array $entries = [])
30
    {
31 8
        $this->validateSitemapResources($entries);
32
33 7
        $this->resources = $entries;
34 7
    }
35
36
    /**
37
     * Add a new sitemap entry into the sitemap collection.
38
     *
39
     * @param SitemapResource $sitemapResource
40
     *
41
     * @return Sitemap
42
     */
43 1
    public function addResource(SitemapResource $sitemapResource): Sitemap
44
    {
45 1
        $this->resources[] = $sitemapResource;
46
47 1
        return $this;
48
    }
49
50
    /**
51
     * Retrieve all of the sitemap resources that have
52
     * been associated to the collection.
53
     *
54
     * @see SitemapResource
55
     * @return array
56
     */
57 3
    public function getResources(): array
58
    {
59 3
        return $this->resources;
60
    }
61
62
    /**
63
     * Validate that an array of sitemap resources is valid.
64
     *
65
     * @param array $sitemapResources
66
     *
67
     * @throws SitemapException
68
     */
69 8
    protected function validateSitemapResources(array $sitemapResources)
70
    {
71 8
        foreach ($sitemapResources as $resource) {
72 2
            if (false === $resource instanceof SitemapResource) {
73 2
                throw new SitemapException('Invalid resource provided for sitemap collection instantiation');
74
            }
75
        }
76 7
    }
77
}
78