ApiHandler::convertRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Handler;
4
5
use App\Entity\Product;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\QueryBuilder;
8
use FOS\RestBundle\View\View;
9
use FOS\RestBundle\View\ViewHandlerInterface;
10
use Pagerfanta\Pagerfanta;
11
use Symfony\Component\HttpFoundation\Request;
12
use Pagerfanta\Adapter\DoctrineORMAdapter;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
15
/**
16
 * Class ApiHandler
17
 *
18
 * @package App\Handler
19
 */
20
class ApiHandler
21
{
22
    /**
23
     *
24
     */
25
    private const MAX_RESULTS = 100;
26
27
    /**
28
     * @var ViewHandlerInterface
29
     */
30
    public $viewhandler;
31
32
    /**
33
     * @var EntityManagerInterface
34
     */
35
    public $em;
36
37
    /**
38
     * @var Request
39
     */
40
    public $request;
41
42
    /**
43
     * @var
44
     */
45
    public $repository;
46
47
    /**
48
     * ApiHandler constructor.
49
     *
50
     * @param RequestStack $requestStack
51
     * @param EntityManagerInterface $em
52
     * @param ViewHandlerInterface $viewHandler
53
     */
54
    public function __construct(RequestStack $requestStack, EntityManagerInterface $em, ViewHandlerInterface $viewHandler)
55
    {
56
        $this->viewhandler = $viewHandler;
57
        $this->em = $em;
58
        $this->request = $requestStack->getCurrentRequest();
59
    }
60
61
    /**
62
     * @param string $entity
63
     * 
64
     * @return $this
65
     */
66
    public function init(string $entity)
67
    {
68
        $this->qb = $this->em->getRepository($entity)->createQueryBuilder('c');
0 ignored issues
show
Bug introduced by
The property qb does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
        $this->repository = $this->em->getRepository($entity);
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param array $groups
76
     * @param array $routeParams
77
     *
78
     * @return \Symfony\Component\HttpFoundation\Response
79
     */
80
    public function collect(array $groups, array $routeParams = array())
0 ignored issues
show
Unused Code introduced by
The parameter $routeParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $view = $this->createView(
83
            $this->transformIterator($this->getPaginatedResult()),
84
            $groups
85
        );
86
87
        return $this->viewhandler->createResponse($view, $this->request,'json');
88
    }
89
90
    /**
91
     * @param $data
92
     * @param array $groups
93
     *
94
     * @return static
95
     */
96
    public function createView($data, array $groups)
97
    {
98
        $view = View::create($data);
99
100
        if ($groups) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groups of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
            $context = $view->getContext();
102
            $context->setGroups($groups);
103
        }
104
105
        return $view;
106
    }
107
108
109
    /**
110
     * @return Pagerfanta
111
     */
112
    private function getPaginatedResult(): Pagerfanta
113
    {
114
        $adapter = new DoctrineORMAdapter($this->getQueryBuilder());
115
        
116
        $pagerfanta = new Pagerfanta($adapter);
117
118
        $pagerfanta
119
            ->setMaxPerPage($this->request->query->get('limit', self::MAX_RESULTS))
120
            ->setCurrentPage($this->request->query->get('page', 1));
121
122
        if ($this->request->query->get('noLimit') !== null) {
123
            $pagerfanta->setMaxPerPage(999999);
124
        }
125
126
        return $pagerfanta;
127
    }
128
129
130
    /**
131
     * Create final return array with data needed + items per page etc.
132
     *
133
     * @param Pagerfanta $pagerfanta
134
     *
135
     * @return array
136
     */
137
    public function transformIterator(Pagerfanta $pagerfanta): array
0 ignored issues
show
Unused Code introduced by
The parameter $pagerfanta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        $pagerfanta = $this->getPaginatedResult();
140
141
        return [
142
            'data' => iterator_to_array($pagerfanta->getCurrentPageResults()),
143
            'page' => $pagerfanta->getCurrentPage(),
144
            'pages' => $pagerfanta->getNbPages(),
145
            'max' => $pagerfanta->getNbResults(),
146
            'offes' => $pagerfanta->getMaxPerPage(),
147
        ];
148
    }
149
150
    /**
151
     * @return QueryBuilder
152
     */
153
    public function convertRequest(): QueryBuilder
154
    {
155
        return $this->qb;
156
    }
157
158
    /**
159
     * @return QueryBuilder
160
     */
161
    public function getQueryBuilder(): QueryBuilder
162
    {
163
        $qb = $this->convertRequest();
164
165
        return $qb;
166
    }
167
}
168