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

CollectionPermalink::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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