Completed
Push — master ( ea487b...367ab0 )
by Joachim
05:21
created

StockMovementProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addFilter() 0 3 1
A getStockMovements() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Provider;
6
7
use Doctrine\ORM\EntityRepository;
8
use Generator;
9
use Pagerfanta\Adapter\DoctrineORMAdapter;
10
use Pagerfanta\Pagerfanta;
11
use Setono\SyliusStockMovementPlugin\Filter\FilterInterface;
12
13
class StockMovementProvider implements StockMovementProviderInterface
14
{
15
    /** @var FilterInterface[] */
16
    protected $filters = [];
17
18
    /** @var EntityRepository */
19
    private $repository;
20
21
    public function __construct(EntityRepository $repository)
22
    {
23
        $this->repository = $repository;
24
    }
25
26
    public function addFilter(FilterInterface $filter): void
27
    {
28
        $this->filters[] = $filter;
29
    }
30
31
    public function getStockMovements(): Generator
32
    {
33
        $qb = $this->repository->createQueryBuilder('o');
34
35
        foreach ($this->filters as $filter) {
36
            $filter->filter($qb);
37
        }
38
39
        // Use output walkers option in DoctrineORMAdapter should be false as it affects performance greatly. (see https://github.com/Sylius/Sylius/issues/3775)
40
        $paginator = new Pagerfanta(new DoctrineORMAdapter($qb, false, false));
41
        $paginator->setNormalizeOutOfRangePages(true);
42
43
        $paginator->setMaxPerPage(100);
44
        $pages = $paginator->getNbPages();
45
46
        for ($page = 1; $page <= $pages; ++$page) {
47
            $paginator->setCurrentPage($page);
48
49
            yield from $paginator->getCurrentPageResults();
50
        }
51
    }
52
}
53