Passed
Pull Request — master (#2)
by Igor
08:27
created

ShipmentViewRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 16
c 0
b 0
f 0
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getAllPaginated() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusLagersystemPlugin\ViewRepository\Shipment;
6
7
use Pagerfanta\Adapter\DoctrineORMAdapter;
8
use Pagerfanta\Pagerfanta;
9
use Setono\SyliusLagersystemPlugin\Factory\PageViewFactoryInterface;
10
use Setono\SyliusLagersystemPlugin\Factory\Shipping\ShipmentViewFactoryInterface;
11
use Setono\SyliusLagersystemPlugin\Model\PaginatorDetails;
12
use Setono\SyliusLagersystemPlugin\Repository\ShipmentRepositoryInterface;
13
use Setono\SyliusLagersystemPlugin\View\PageView;
14
use Webmozart\Assert\Assert;
15
16
class ShipmentViewRepository implements ShipmentViewRepositoryInterface
17
{
18
    /** @var ShipmentRepositoryInterface */
19
    protected $shipmentRepository;
20
21
    /** @var PageViewFactoryInterface */
22
    protected $pageViewFactory;
23
24
    /** @var ShipmentViewFactoryInterface */
25
    protected $shipmentViewFactory;
26
27
    public function __construct(
28
        ShipmentRepositoryInterface $shipmentRepository,
29
        PageViewFactoryInterface $pageViewFactory,
30
        ShipmentViewFactoryInterface $shipmentViewFactory
31
    ) {
32
        $this->shipmentRepository = $shipmentRepository;
33
        $this->pageViewFactory = $pageViewFactory;
34
        $this->shipmentViewFactory = $shipmentViewFactory;
35
    }
36
37
    public function getAllPaginated(PaginatorDetails $paginatorDetails, ?string $localeCode): PageView
38
    {
39
        Assert::notNull($localeCode);
40
        $queryBuilder = $this->shipmentRepository->createLagersystemListQueryBuilder();
41
42
        $pagerfanta = new Pagerfanta(new DoctrineORMAdapter($queryBuilder));
43
        $pagerfanta->setMaxPerPage($paginatorDetails->limit());
44
        $pagerfanta->setCurrentPage($paginatorDetails->page());
45
46
        $pageView = $this->pageViewFactory->create($pagerfanta, $paginatorDetails->route(), $paginatorDetails->parameters());
47
        foreach ($pagerfanta->getCurrentPageResults() as $currentPageResult) {
48
            $pageView->items[] = $this->shipmentViewFactory->create($currentPageResult, $localeCode);
0 ignored issues
show
Bug introduced by
It seems like $localeCode can also be of type null; however, parameter $locale of Setono\SyliusLagersystem...toryInterface::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            $pageView->items[] = $this->shipmentViewFactory->create($currentPageResult, /** @scrutinizer ignore-type */ $localeCode);
Loading history...
49
        }
50
51
        return $pageView;
52
    }
53
}
54