RelationPaginatorFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 19.44 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 6
dl 14
loc 72
rs 10
ccs 19
cts 19
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 9 1
A getFirstLink() 7 7 1
A getSelfLink() 0 4 1
A getLastLink() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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