Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminListBundle/Traits/ChangeableLimitTrait.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Traits;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * Trait ChangeableLimitTrait
9
 */
10
trait ChangeableLimitTrait
11
{
12
    private $limit;
13
14
    /**
15
     * Bind current request.
16
     */
17
    public function bindRequest(Request $request)
18
    {
19
        $query = $request->query;
20
        $session = $request->getSession();
21
22
        $adminListName = 'listconfig_' . $request->get('_route');
23
24
        $this->page = $request->query->getInt('page', 1);
25
        $this->limit = $request->query->getInt('limit', $this->getLimitOptions()[0]);
26
27
        $adminListSessionData = $request->getSession()->get($adminListName);
28
        if (!$query->has('limit') && null !== $adminListSessionData && isset($adminListSessionData['limit'])) {
29
            $this->limit = $adminListSessionData['limit'];
30
        }
31
32
        if ($request->query->has('limit') && !$request->query->has('page')) {
33
            $this->page = 1;
34
        }
35
36
        // Allow alphanumeric, _ & . in order by parameter!
37
        $this->orderBy = preg_replace('/[^[a-zA-Z0-9\_\.]]/', '', $request->query->get('orderBy', ''));
38
        $this->orderDirection = $request->query->getAlpha('orderDirection');
39
40
        // there is a session and the filter param is not set
41 View Code Duplication
        if ($session->has($adminListName) && !$query->has('filter')) {
42
            if (!$query->has('page') && !$query->has('limit')) {
43
                $this->page = $adminListSessionData['page'];
44
            }
45
46
            if (!$query->has('orderBy')) {
47
                $this->orderBy = $adminListSessionData['orderBy'];
48
            }
49
50
            if (!$query->has('orderDirection')) {
51
                $this->orderDirection = $adminListSessionData['orderDirection'];
52
            }
53
        }
54
55
        // save current parameters
56
        $session->set(
57
            $adminListName,
58
            [
59
                'page' => $this->page,
60
                'limit' => $this->limit,
61
                'orderBy' => $this->orderBy,
62
                'orderDirection' => $this->orderDirection,
63
            ]
64
        );
65
66
        // Remove limit from query param so it doesn't affect the session of the filter builder
67
        $request->query->remove('limit');
68
        $this->getFilterBuilder()->bindRequest($request);
69
    }
70
71
    /**
72
     * @return int
73
     */
74
    public function getLimit()
75
    {
76
        return $this->limit;
77
    }
78
79
    /** @return array */
0 ignored issues
show
Consider making the return type a bit more specific; maybe use integer[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
80
    public function getLimitOptions()
81
    {
82
        return [
83
            10,
84
            20,
85
            50,
86
            100,
87
        ];
88
    }
89
}
90