AsyncPagerPersister   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 7
dl 0
loc 79
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A insertPage() 0 24 3
A insert() 0 20 3
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://friendsofsymfony.github.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
namespace FOS\ElasticaBundle\Persister;
13
14
use FOS\ElasticaBundle\Message\AsyncPersistPage;
15
use FOS\ElasticaBundle\Provider\PagerInterface;
16
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
17
use Symfony\Component\Messenger\MessageBusInterface;
18
19
final class AsyncPagerPersister implements PagerPersisterInterface
20
{
21
    public const NAME = 'async';
22
    private const DEFAULT_PAGE_SIZE = 100;
23
24
    /**
25
     * @var PagerPersisterRegistry
26
     */
27
    private $pagerPersisterRegistry;
28
29
    /**
30
     * @var PagerProviderRegistry
31
     */
32
    private $pagerProviderRegistry;
33
34
    /**
35
     * @var MessageBusInterface
36
     */
37
    private $messageBus;
38
39 2
    public function __construct(
40
        PagerPersisterRegistry $pagerPersisterRegistry,
41
        PagerProviderRegistry $pagerProviderRegistry,
42
        MessageBusInterface $messageBus
43
    ) {
44 2
        $this->pagerPersisterRegistry = $pagerPersisterRegistry;
45 2
        $this->pagerProviderRegistry = $pagerProviderRegistry;
46 2
        $this->messageBus = $messageBus;
47 2
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function insert(PagerInterface $pager, array $options = []): void
53
    {
54 1
        $pager->setMaxPerPage(empty($options['max_per_page']) ? self::DEFAULT_PAGE_SIZE : $options['max_per_page']);
55
56 1
        $options = \array_replace([
57 1
            'max_per_page' => $pager->getMaxPerPage(),
58 1
            'first_page' => $pager->getCurrentPage(),
59 1
            'last_page' => $pager->getNbPages(),
60 1
        ], $options);
61
62 1
        $pager->setCurrentPage($options['first_page']);
63
64 1
        $lastPage = \min($options['last_page'], $pager->getNbPages());
65 1
        $page = $pager->getCurrentPage();
66
        do {
67 1
            $this->messageBus->dispatch(new AsyncPersistPage($page, $options));
68
69 1
            ++$page;
70 1
        } while ($page <= $lastPage);
71 1
    }
72
73 1
    public function insertPage(int $page, array $options = []): int
74
    {
75 1
        if (!isset($options['indexName'])) {
76
            throw new \RuntimeException('Invalid call. $options is missing the indexName key.');
77
        }
78 1
        if (!isset($options['max_per_page'])) {
79
            throw new \RuntimeException('Invalid call. $options is missing the max_per_page key.');
80
        }
81
82 1
        $options['first_page'] = $page;
83 1
        $options['last_page'] = $page;
84
85 1
        $provider = $this->pagerProviderRegistry->getProvider($options['indexName']);
86 1
        $pager = $provider->provide($options);
87 1
        $pager->setMaxPerPage($options['max_per_page']);
88 1
        $pager->setCurrentPage($options['first_page']);
89
90 1
        $objectCount = \count($pager->getCurrentPageResults());
91
92 1
        $pagerPersister = $this->pagerPersisterRegistry->getPagerPersister(InPlacePagerPersister::NAME);
93 1
        $pagerPersister->insert($pager, $options);
94
95 1
        return $objectCount;
96
    }
97
}
98