|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Setono\SyliusStockMovementPlugin\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Generator; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use Safe\Exceptions\StringsException; |
|
12
|
|
|
use function Safe\sprintf; |
|
13
|
|
|
use Setono\SyliusStockMovementPlugin\Filter\FilterInterface; |
|
14
|
|
|
use Setono\SyliusStockMovementPlugin\Model\ReportConfigurationInterface; |
|
15
|
|
|
use Setono\SyliusStockMovementPlugin\Model\StockMovementInterface; |
|
16
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
|
17
|
|
|
|
|
18
|
|
|
class StockMovementProvider implements StockMovementProviderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ServiceRegistryInterface */ |
|
21
|
|
|
private $filterRegistry; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ManagerRegistry */ |
|
24
|
|
|
private $managerRegistry; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $stockMovementClass; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ServiceRegistryInterface $filterRegistry, ManagerRegistry $managerRegistry, string $stockMovementClass) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->filterRegistry = $filterRegistry; |
|
32
|
|
|
$this->managerRegistry = $managerRegistry; |
|
33
|
|
|
$this->stockMovementClass = $stockMovementClass; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return StockMovementInterface[]|Generator |
|
38
|
|
|
* |
|
39
|
|
|
* @throws StringsException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getStockMovements(ReportConfigurationInterface $reportConfiguration): Generator |
|
42
|
|
|
{ |
|
43
|
|
|
$em = $this->managerRegistry->getManagerForClass($this->stockMovementClass); |
|
44
|
|
|
if (!$em instanceof EntityManagerInterface) { |
|
45
|
|
|
throw new InvalidArgumentException(sprintf('No manager for class %s', $this->stockMovementClass)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$qb = $em->createQueryBuilder(); |
|
49
|
|
|
$qb->select('o') |
|
50
|
|
|
->from($this->stockMovementClass, 'o') |
|
51
|
|
|
; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($reportConfiguration->getFilters() as $reportConfigurationFilter) { |
|
54
|
|
|
/** @var FilterInterface $filter */ |
|
55
|
|
|
$filter = $this->filterRegistry->get($reportConfigurationFilter->getType()); |
|
56
|
|
|
|
|
57
|
|
|
$filter->filter($qb, $reportConfiguration, $reportConfigurationFilter->getConfiguration()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$iterableResult = $qb->getQuery()->iterate(); |
|
61
|
|
|
foreach ($iterableResult as $row) { |
|
62
|
|
|
yield $row[0]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|