DatePermalink   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generate() 0 10 1
A getParameters() 0 17 2
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 DatePermalink implements PermalinkInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $pattern;
24
25
    /**
26
     * @param $pattern
27
     */
28
    public function __construct($pattern = '%1$04d/%2$d/%3$d/%4$s')
29
    {
30
        $this->pattern = $pattern;
31
    }
32
33
    public function generate(PostInterface $post)
34
    {
35
        return sprintf(
36
            $this->pattern,
37
            $post->getYear(),
38
            $post->getMonth(),
39
            $post->getDay(),
40
            $post->getSlug()
41
        );
42
    }
43
44
    public function getParameters($permalink)
45
    {
46
        $parameters = explode('/', $permalink);
47
48
        if (4 !== \count($parameters)) {
49
            throw new \InvalidArgumentException('wrong permalink format');
50
        }
51
52
        list($year, $month, $day, $slug) = $parameters;
53
54
        return [
55
            'year' => (int) $year,
56
            'month' => (int) $month,
57
            'day' => (int) $day,
58
            'slug' => $slug,
59
        ];
60
    }
61
}
62