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