Completed
Pull Request — master (#1640)
by Giovanni
05:07
created

AsyncPagerPersister::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace FOS\ElasticaBundle\Persister;
5
6
use FOS\ElasticaBundle\Message\AsyncPersistPage;
7
use FOS\ElasticaBundle\Provider\PagerInterface;
8
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
9
use Symfony\Component\Messenger\MessageBusInterface;
10
11
final class AsyncPagerPersister implements PagerPersisterInterface
12
{
13
    const NAME = 'async';
14
    const DEFAULT_PAGE_SIZE = 100;
15
16
    /**
17
     * @var PagerPersisterRegistry
18
     */
19
    private $pagerPersisterRegistry;
20
21
    /**
22
     * @var PagerProviderRegistry
23
     */
24
    private $pagerProviderRegistry;
25
26
    /**
27
     * @var MessageBusInterface
28
     */
29
    private $messageBus;
30
31
    public function __construct(
32
        PagerPersisterRegistry $pagerPersisterRegistry,
33
        PagerProviderRegistry $pagerProviderRegistry,
34
        MessageBusInterface $messageBus
35
    ) {
36
        $this->pagerPersisterRegistry = $pagerPersisterRegistry;
37
        $this->pagerProviderRegistry = $pagerProviderRegistry;
38
        $this->messageBus = $messageBus;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function insert(PagerInterface $pager, array $options = array()): void
45
    {
46
        $pager->setMaxPerPage(empty($options['max_per_page']) ? self::DEFAULT_PAGE_SIZE : $options['max_per_page']);
47
48
        $options = array_replace([
49
            'max_per_page' => $pager->getMaxPerPage(),
50
            'first_page' => $pager->getCurrentPage(),
51
            'last_page' => $pager->getNbPages(),
52
        ], $options);
53
54
        $pager->setCurrentPage($options['first_page']);
55
56
        $lastPage = min($options['last_page'], $pager->getNbPages());
57
        $page = $pager->getCurrentPage();
58
        do {
59
            $this->messageBus->dispatch(new AsyncPersistPage($page, $options));
60
61
            $page++;
62
        } while ($page <= $lastPage);
63
    }
64
65
    public function insertPage(int $page, array $options = []): int
66
    {
67
        if (!isset($options['indexName'])) {
68
            throw new \RuntimeException('Invalid call. $options is missing the indexName key.');
69
        }
70
        if (!isset($options['max_per_page'])) {
71
            throw new \RuntimeException('Invalid call. $options is missing the max_per_page key.');
72
        }
73
74
        $options['first_page'] = $page;
75
        $options['last_page'] = $page;
76
77
        $provider = $this->pagerProviderRegistry->getProvider($options['indexName']);
78
        $pager = $provider->provide($options);
79
        $pager->setMaxPerPage($options['max_per_page']);
80
        $pager->setCurrentPage($options['first_page']);
81
82
        $objectCount = count($pager->getCurrentPageResults());
83
84
        $pagerPersister = $this->pagerPersisterRegistry->getPagerPersister(InPlacePagerPersister::NAME);
85
        $pagerPersister->insert($pager, $options);
86
87
        return $objectCount;
88
    }
89
}
90