RelationPaginatorFactory::getSelfLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Pgs\RestfonyBundle\Service;
4
5
use Doctrine\Common\Util\Inflector;
6
use Knp\Component\Pager\Pagination\AbstractPagination;
7
use Pgs\RestfonyBundle\Model\RelationPaginator;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\RouterInterface;
10
11
/**
12
 * @author Michał Sikora
13
 */
14
class RelationPaginatorFactory
15
{
16
    /**
17
     * @var RouterInterface
18
     */
19
    private $router;
20
21
    /**
22
     * @param RouterInterface $router
23
     */
24 1
    public function __construct(RouterInterface $router)
25
    {
26 1
        $this->router = $router;
27 1
    }
28
29
    /**
30
     * @param Request            $request
31
     * @param AbstractPagination $paginationView
32
     * @param string             $moduleName
33
     *
34
     * @return RelationPaginator
35
     */
36 1
    public function create(Request $request, AbstractPagination $paginationView, $moduleName)
37
    {
38 1
        $first = $this->getFirstLink($request, $moduleName);
39 1
        $self = $this->getSelfLink($request, $moduleName);
40 1
        $lastPage = ceil($paginationView->getTotalItemCount() / $paginationView->getItemNumberPerPage());
41 1
        $last = $this->getLastLink($request, $moduleName, $lastPage);
42
43 1
        return new RelationPaginator($first, $last, $self);
44
    }
45
46
    /**
47
     * @param Request $request
48
     * @param string  $moduleName
49
     *
50
     * @return string
51
     */
52 1 View Code Duplication
    protected function getFirstLink(Request $request, $moduleName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 1
        $parameters = $request->query->all();
55 1
        $parameters['page'] = 1;
56
57 1
        return $this->router->generate(sprintf('get_%s', Inflector::pluralize($moduleName)), $parameters);
58
    }
59
60
    /**
61
     * @param Request $request
62
     * @param string  $moduleName
63
     *
64
     * @return string
65
     */
66 1
    protected function getSelfLink(Request $request, $moduleName)
67
    {
68 1
        return $this->router->generate(sprintf('get_%s', Inflector::pluralize($moduleName)), $request->query->all());
69
    }
70
71
    /**
72
     * @param Request $request
73
     * @param string  $moduleName
74
     * @param int     $lastPage
75
     *
76
     * @return string
77
     */
78 1 View Code Duplication
    protected function getLastLink(Request $request, $moduleName, $lastPage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 1
        $parameters = $request->query->all();
81 1
        $parameters['page'] = $lastPage;
82
83 1
        return $this->router->generate(sprintf('get_%s', Inflector::pluralize($moduleName)), $parameters);
84
    }
85
}
86