Completed
Push — master ( 3ae95d...2e8c19 )
by Rafael
11:16
created

AllNodesWithPagination::createPaginator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
c 1
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Query\Node;
12
13
use Doctrine\ORM\Query\Expr\Orx;
14
use Doctrine\ORM\QueryBuilder;
15
use GraphQL\Error\Error;
16
use Ynlo\GraphQLBundle\Definition\Extension\PaginationDefinitionExtension;
17
use Ynlo\GraphQLBundle\Model\ConnectionInterface;
18
use Ynlo\GraphQLBundle\Model\ID;
19
use Ynlo\GraphQLBundle\Model\NodeConnection;
20
use Ynlo\GraphQLBundle\Model\NodeInterface;
21
use Ynlo\GraphQLBundle\Pagination\DoctrineCursorPaginatorInterface;
22
use Ynlo\GraphQLBundle\Pagination\DoctrineOffsetCursorPaginator;
23
use Ynlo\GraphQLBundle\Pagination\PaginationRequest;
24
25
/**
26
 * Base class to fetch nodes
27
 */
28
class AllNodesWithPagination extends AllNodes
29
{
30
    /**
31
     * @param array[] $args
32
     *
33
     * @return mixed
34
     *
35
     * @throws Error
36
     */
37 10
    public function __invoke($args = [])
38
    {
39 10
        $orderBy = $args['orderBy'] ?? [];
40 10
        $first = $args['first'] ?? null;
41 10
        $last = $args['last'] ?? null;
42 10
        $before = $args['before'] ?? null;
43 10
        $after = $args['after'] ?? null;
44 10
        $search = $args['search'] ?? null;
45 10
        $filters = $args['filters'] ?? null;
46
47 10
        $this->initialize();
48
49 10
        $qb = $this->createQuery();
50 10
        $this->applyOrderBy($qb, $orderBy);
51
52 10
        if ($this->getContext()->getRoot()) {
53 3
            $this->applyFilterByParent($qb, $this->getContext()->getRoot());
54
        }
55
56 10
        if ($search) {
57
            $this->search($qb, $search);
58
        }
59
60 10
        if ($filters) {
61
            $this->applyFilters($qb, $filters);
62
        }
63
64 10
        $this->configureQuery($qb);
65 10
        foreach ($this->extensions as $extension) {
66 4
            $extension->configureQuery($qb, $this, $this->context);
67
        }
68
69 10
        if (!$first && !$last) {
70
            $error = sprintf('You must provide a `first` or `last` value to properly paginate records in "%s" connection.', $this->queryDefinition->getName());
71
            throw new Error($error);
72
        }
73
74 10
        if ($this->queryDefinition->hasMeta('pagination')) {
75 10
            $limitAllowed = $this->queryDefinition->getMeta('pagination')['limit'];
76
77 10
            if ($first > $limitAllowed || $last > $limitAllowed) {
78
                $current = $first ?? $last;
79
                $where = $first ? 'first' : 'last';
80
                $error = sprintf(
81
                    'Requesting %s records for `%s` exceeds the `%s` limit of %s records for "%s" connection',
82
                    $current,
83
                    $this->queryDefinition->getName(),
84
                    $where,
85
                    $limitAllowed,
86
                    $this->queryDefinition->getName()
87
                );
88
                throw new Error($error);
89
            }
90
        }
91
92 10
        $paginator = $this->createPaginator();
93
94 10
        $connection = $this->createConnection();
95 10
        $paginator->paginate($qb, new PaginationRequest($first, $last, $after, $before), $connection);
96
97 10
        return $connection;
98
    }
99
100
    /**
101
     * @return ConnectionInterface
102
     */
103 10
    protected function createConnection(): ConnectionInterface
104
    {
105 10
        return new NodeConnection();
106
    }
107
108
    /**
109
     * @return DoctrineCursorPaginatorInterface
110
     */
111 10
    protected function createPaginator(): DoctrineCursorPaginatorInterface
112
    {
113 10
        return new DoctrineOffsetCursorPaginator();
114
    }
115
116
    /**
117
     * Apply advanced filters
118
     *
119
     * @param QueryBuilder $qb
120
     * @param array        $filters string to search
121
     */
122
    protected function applyFilters(QueryBuilder $qb, $filters)
123
    {
124
        $definition = $this->objectDefinition;
125
        foreach ($filters as $field => $value) {
126
            if ($definition->hasField($field)) {
127
                $prop = $definition->getField($field)->getOriginName();
128
                if ($prop) {
129
                    $entityField = sprintf('%s.%s', $this->queryAlias, $prop);
130
                    if (is_array($value)) {
131
                        foreach ($value as &$val) {
132
                            if ($val instanceof ID) {
133
                                $val = (int) $val->getDatabaseId();
134
                            }
135
                        }
136
                        if (empty($value)) {
137
                            $qb->andWhere($qb->expr()->isNull($entityField));
138
                        } else {
139
                            $qb->andWhere($qb->expr()->in($entityField, $value));
140
                        }
141
                    } else {
142
                        if (null === $value) {
143
                            $qb->andWhere($qb->expr()->isNull($entityField));
144
                        } else {
145
                            $qb->andWhere($qb->expr()->eq($entityField, $value));
146
                        }
147
                    }
148
                }
149
            }
150
        }
151
    }
152
153
    /**
154
     * Filter some columns with simple string.
155
     *
156
     * @param QueryBuilder $qb
157
     * @param string       $search string to search
158
     */
159
    protected function search(QueryBuilder $qb, $search)
160
    {
161
        //search every word separate
162
        $searchArray = explode(' ', $search);
163
164
        $alias = $qb->getRootAliases()[0];
165
166
        //TODO: allow some config to customize search fields
167
        $em = $this->getManager();
168
        $metadata = $em->getClassMetadata($this->entity);
169
        $searchFields = $metadata->getFieldNames();
170
171
        if (count($searchFields) > 0) {
172
            $meta = $qb->getEntityManager()->getClassMetadata($qb->getRootEntities()[0]);
173
            foreach ($searchArray as $q) {
174
                $q = trim(rtrim($q));
175
                $id = md5($q);
176
                $orx = new Orx();
177
                foreach ($searchFields as $field) {
178
                    if (strpos($field, '.') !== false && !isset($meta->embeddedClasses[explode('.', $field)[0]])) {
179
                        $orx->add("$field LIKE :search_$id");
180
                    } else { //append current alias
181
                        $orx->add("$alias.$field LIKE :search_$id");
182
                    }
183
                }
184
                $qb->andWhere($orx);
185
                $qb->setParameter("search_$id", "%$q%");
186
            }
187
        }
188
    }
189
190
    /**
191
     * @param QueryBuilder  $qb
192
     * @param NodeInterface $root
193
     */
194 3
    protected function applyFilterByParent(QueryBuilder $qb, NodeInterface $root)
195
    {
196 3
        $parentField = null;
197 3
        if ($this->queryDefinition->hasMeta('pagination')) {
198 3
            $parentField = $this->queryDefinition->getMeta('pagination')['parent_field'] ?? null;
199
        }
200 3
        if (!$parentField) {
201
            throw new \RuntimeException(
202
                sprintf(
203
                    'Missing parent field to filter "%s" by given parent.
204
             The "parent_field" should be specified.',
205
                    $this->queryDefinition->getName()
206
                )
207
            );
208
        }
209
210 3
        if ($this->objectDefinition->hasField($parentField)) {
211 3
            $parentField = $this->objectDefinition->getField($parentField)->getOriginName();
212
        }
213
214 3
        $paramName = 'root'.mt_rand();
215 3
        if ($this->queryDefinition->getMeta('pagination')['parent_relation'] === PaginationDefinitionExtension::MANY_TO_MANY) {
216 2
            $qb->andWhere(sprintf(':%s MEMBER OF %s.%s', $paramName, $this->queryAlias, $parentField))
217 2
               ->setParameter($paramName, $root);
218
        } else {
219 1
            $qb->andWhere(sprintf('%s.%s = :%s', $this->queryAlias, $parentField, $paramName))
220 1
               ->setParameter($paramName, $root);
221
        }
222 3
    }
223
}
224