Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

serviceAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.6
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Kunstmaan\ArticleBundle\Controller;
4
5
use Pagerfanta\Adapter\ArrayAdapter;
6
use Pagerfanta\Pagerfanta;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * Class AbstractArticleOverviewPageController
12
 */
13
class AbstractArticleOverviewPageController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
14
{
15
    /**
16
     * @param Request $request
17
     */
18
    public function serviceAction(Request $request)
19
    {
20
        $em = $this->getDoctrine()->getManager();
21
        $entity = $request->attributes->get('_entity');
22
        $repository = $entity->getArticleRepository($em);
23
        $pages = $repository->getArticles($request->getLocale());
24
25
        $adapter = new ArrayAdapter($pages);
26
        $pagerfanta = new Pagerfanta($adapter);
27
        $pagerfanta->setMaxPerPage(5);
28
29
        $pagenumber = $request->get('page');
30
        if (!$pagenumber || $pagenumber < 1) {
31
            $pagenumber = 1;
32
        }
33
        $pagerfanta->setCurrentPage($pagenumber);
34
        $context['pagerfanta'] = $pagerfanta;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$context was never initialized. Although not strictly required by PHP, it is generally a good practice to add $context = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
35
36
        $request->attributes->set('_renderContext', $context);
37
    }
38
}
39