CollectionPermalink::getParameters()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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