Completed
Push — 3.x-dev-kit ( 21be5a )
by
unknown
03:10
created

CollectionPermalink   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 0
cbo 2
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 20 4
A generate() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Permalink;
13
14
use Sonata\NewsBundle\Model\PostInterface;
15
16
class CollectionPermalink implements PermalinkInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function generate(PostInterface $post)
22
    {
23
        return null == $post->getCollection()
24
            ? $post->getSlug()
25
            : sprintf('%s/%s', $post->getCollection()->getSlug(), $post->getSlug());
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getParameters($permalink)
32
    {
33
        $parameters = explode('/', $permalink);
34
35
        if (count($parameters) > 2 || count($parameters) == 0) {
36
            throw new \InvalidArgumentException('wrong permalink format');
37
        }
38
39
        if (false === strpos($permalink, '/')) {
40
            $collection = null;
41
            $slug = $permalink;
42
        } else {
43
            list($collection, $slug) = $parameters;
44
        }
45
46
        return array(
47
            'collection' => $collection,
48
            'slug' => $slug,
49
        );
50
    }
51
}
52