Passed
Branch develop (56c45f)
by Jens
02:42
created

createSitemapItemFromPostValues()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 3
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 13-3-2017
5
 * Time: 17:04
6
 */
7
8
namespace CloudControl\Cms\storage\factories;
9
10
11
use CloudControl\Cms\cc\StringUtil;
12
13
class SitemapItemFactory
14
{
15
    /**
16
     * Create a sitemap item from post values
17
     *
18
     * @param $postValues
19
     *
20
     * @return \stdClass
21
     * @throws \Exception
22
     */
23
    public static function createSitemapItemFromPostValues($postValues)
24
    {
25
        if (isset($postValues['title'], $postValues['url'], $postValues['component'], $postValues['template'])) {
26
            $sitemapObject = new \stdClass();
27
            $sitemapObject->title = $postValues['title'];
28
            $sitemapObject->slug = StringUtil::slugify($postValues['title']);
29
            $sitemapObject->url = $postValues['url'];
30
            $sitemapObject->component = $postValues['component'];
31
            $sitemapObject->template = $postValues['template'];
32
            $sitemapObject->regex = isset($postValues['regex']);
33
            $sitemapObject->parameters = new \stdClass();
34
            if (isset($postValues['parameterNames'], $postValues['parameterValues'])) {
35
                foreach ($postValues['parameterNames'] as $key => $value) {
36
                    $sitemapObject->parameters->$value = $postValues['parameterValues'][$key];
37
                }
38
            }
39
40
            return $sitemapObject;
41
        } else {
42
            throw new \Exception('Trying to create sitemap item with invalid data.');
43
        }
44
    }
45
}