SitemapGenerator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Sitemap\Lib;
14
15
use Cake\Controller\Controller;
16
17
abstract class SitemapGenerator
18
{
19
20
    /**
21
     * Number of URLs per sitemap file.
22
     *
23
     * Mostly bound by memory available to PHP on server
24
     *
25
     * @var int
26
     */
27
    protected $_divider = 20000;
28
29
    protected $_Controller;
30
31
    protected $_type = null;
32
33
    /**
34
     * @param Controller $Controller controller
35
     * @throws \Exception
36
     */
37
    public function __construct(Controller $Controller)
38
    {
39
        $this->_Controller = $Controller;
40
        if ($this->_type === null) {
41
            throw new \Exception('SitemapGenerator type not set.', 1559477829);
42
        }
43
    }
44
45
    /**
46
     * Returns sitemap file
47
     *
48
     * @return array keys: 'url'
49
     */
50
    abstract public function files();
51
52
    /**
53
     * @param string $file filename
54
     * @return mixed
55
     */
56
    public function content($file)
57
    {
58
        list($type, $params) = $this->_parseFilename($file);
59
        if ($type !== $this->_type) {
60
            return false;
61
        }
62
63
        return $this->_content($this->_parseParams($params));
64
    }
65
66
    /**
67
     *
68
     * @param string $name filename to parse
69
     * @return array
70
     * @throws \InvalidArgumentException
71
     */
72
    protected function _parseFilename($name)
73
    {
74
        preg_match(
75
            '/sitemap-(?P<type>\w*)(-(?P<params>.*))?(\.(\w*))?$/',
76
            $name,
77
            $matches
78
        );
79
        if (empty($matches['type'])) {
80
            throw new \InvalidArgumentException(
81
                sprintf('File not found for: %s', $name),
82
                1559477721
83
            );
84
        }
85
86
        return [$matches['type'], explode('-', $matches['params'])];
87
    }
88
89
    /**
90
     * Parse and validate params. Should throw exception if params are not valid.
91
     *
92
     * @param array $params additional parameters
93
     * @return mixed processed params
94
     */
95
    abstract protected function _parseParams($params);
96
97
    /**
98
     * Generate urls
99
     *
100
     * @param array $params additional paramters
101
     * @return array urls
102
     */
103
    abstract protected function _content(array $params): array;
104
105
    /**
106
     * Creates name for sitemap-file
107
     *
108
     * @param array $params additional name parameters
109
     * @return string
110
     */
111
    protected function _filename($params = [])
112
    {
113
        $filename = "sitemap-{$this->_type}";
114
        if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115
            $filename .= '-' . implode('-', $params);
116
        }
117
118
        return $filename;
119
    }
120
}
121