• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/src/Doctrine/Odm/Paginator.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Doctrine\Odm;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\State\Pagination\HasNextPagePaginatorInterface;
18
use ApiPlatform\State\Pagination\PaginatorInterface;
19
use Doctrine\ODM\MongoDB\Iterator\Iterator;
20
use Doctrine\ODM\MongoDB\UnitOfWork;
21

22
/**
23
 * Decorates the Doctrine MongoDB ODM paginator.
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 * @author Alan Poulain <contact@alanpoulain.eu>
27
 */
28
final class Paginator implements \IteratorAggregate, PaginatorInterface, HasNextPagePaginatorInterface
29
{
30
    private readonly \ArrayIterator $iterator;
31

32
    private readonly int $firstResult;
33

34
    private readonly int $maxResults;
35

36
    private readonly int $totalItems;
37

38
    private readonly int $count;
39

40
    public function __construct(Iterator $mongoDbOdmIterator, UnitOfWork $unitOfWork, string $resourceClass)
41
    {
UNCOV
42
        $result = $mongoDbOdmIterator->toArray()[0];
269✔
43

UNCOV
44
        if (array_diff_key(['results' => 1, 'count' => 1, '__api_first_result__' => 1, '__api_max_results__' => 1], $result)) {
269✔
45
            throw new RuntimeException('The result of the query must contain only "__api_first_result__", "__api_max_results__", "results" and "count" fields.');
×
46
        }
47

48
        // The "count" facet contains the total number of documents,
49
        // it is not set when the query does not return any document
UNCOV
50
        $this->totalItems = $result['count'][0]['count'] ?? 0;
269✔
51

52
        // The "results" facet contains the returned documents
UNCOV
53
        if ([] === $result['results']) {
269✔
UNCOV
54
            $this->count = 0;
44✔
UNCOV
55
            $this->iterator = new \ArrayIterator();
44✔
56
        } else {
UNCOV
57
            $this->count = \count($result['results']);
230✔
UNCOV
58
            $this->iterator = new \ArrayIterator(array_map(
230✔
UNCOV
59
                static fn ($result): object => $unitOfWork->getOrCreateDocument($resourceClass, $result),
230✔
UNCOV
60
                $result['results'],
230✔
UNCOV
61
            ));
230✔
62
        }
63

UNCOV
64
        $this->firstResult = $result['__api_first_result__'];
269✔
UNCOV
65
        $this->maxResults = $result['__api_max_results__'];
269✔
66
    }
67

68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getCurrentPage(): float
72
    {
UNCOV
73
        if (0 >= $this->maxResults) {
202✔
UNCOV
74
            return 1.;
1✔
75
        }
76

UNCOV
77
        return floor($this->firstResult / $this->maxResults) + 1.;
201✔
78
    }
79

80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getLastPage(): float
84
    {
UNCOV
85
        if (0 >= $this->maxResults) {
201✔
UNCOV
86
            return 1.;
1✔
87
        }
88

UNCOV
89
        return ceil($this->totalItems / $this->maxResults) ?: 1.;
200✔
90
    }
91

92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getItemsPerPage(): float
96
    {
UNCOV
97
        return (float) $this->maxResults;
45✔
98
    }
99

100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getTotalItems(): float
104
    {
UNCOV
105
        return (float) $this->totalItems;
205✔
106
    }
107

108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getIterator(): \Traversable
112
    {
UNCOV
113
        return $this->iterator;
257✔
114
    }
115

116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function count(): int
120
    {
UNCOV
121
        return $this->count;
6✔
122
    }
123

124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function hasNextPage(): bool
128
    {
UNCOV
129
        return $this->getLastPage() > $this->getCurrentPage();
3✔
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc