-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathEAVReaderTask.php
168 lines (149 loc) · 5.16 KB
/
EAVReaderTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of the CleverAge/EAVManager package.
*
* Copyright (c) 2015-2018 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\EAVManager\ProcessBundle\Task;
use CleverAge\ProcessBundle\Model\IterableTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Sidus\EAVModelBundle\Doctrine\EAVFinder;
use Sidus\EAVModelBundle\Registry\FamilyRegistry;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Allows to iterate over a paged resultset of EAV data
*/
class EAVReaderTask extends AbstractEAVQueryTask implements IterableTaskInterface
{
/** @var LoggerInterface */
protected $logger;
/** @var \Iterator */
protected $iterator;
/** @var bool */
protected $closed = false;
/**
* @param LoggerInterface $logger
* @param EntityManagerInterface $entityManager
* @param FamilyRegistry $familyRegistry
* @param EAVFinder $eavFinder
*/
public function __construct(
LoggerInterface $logger,
EntityManagerInterface $entityManager,
FamilyRegistry $familyRegistry,
EAVFinder $eavFinder
) {
$this->logger = $logger;
parent::__construct($entityManager, $familyRegistry, $eavFinder);
}
/**
* {@inheritDoc}
*
* @throws \InvalidArgumentException
* @throws \Pagerfanta\Exception\NotIntegerCurrentPageException
* @throws \Pagerfanta\Exception\OutOfRangeCurrentPageException
* @throws \Pagerfanta\Exception\LessThan1CurrentPageException
* @throws \UnexpectedValueException
* @throws \Sidus\EAVModelBundle\Exception\MissingAttributeException
* @throws \LogicException
* @throws \Symfony\Component\OptionsResolver\Exception\ExceptionInterface
*/
public function execute(ProcessState $state)
{
$options = $this->getOptions($state);
if ($this->closed) {
$logContext = $this->getLogContext($state);
if ($options['allow_reset']) {
$this->closed = false;
$this->iterator = null;
$this->logger->error('Reader was closed previously, restarting it', $logContext);
} else {
$this->logger->error('Reader was closed previously, stopping the process', $logContext);
$state->setStopped(true);
return;
}
}
if (null === $this->iterator) {
$paginator = $this->getPaginator($state);
$this->iterator = $paginator->getIterator();
// Log the data count
if ($this->getOption($state, 'log_count')) {
$count = \count($paginator);
$logContext = $this->getLogContext($state);
$this->logger->info("{$count} items found with current query", $logContext);
}
}
// Handle empty results
if (0 === $this->iterator->count()) {
if ($this->getOption($state, 'log_count')) {
$logContext = $this->getLogContext($state);
$this->logger->notice('Empty resultset for query, stopping the process', $logContext);
}
$state->setStopped(true);
return;
}
$state->setOutput($this->iterator->current());
}
/**
* Moves the internal pointer to the next element,
* return true if the task has a next element
* return false if the task has terminated it's iteration
*
* @param ProcessState $state
*
* @throws \LogicException
*
* @return bool
*/
public function next(ProcessState $state)
{
if (!$this->iterator instanceof \Iterator) {
throw new \LogicException('No iterator initialized');
}
$this->iterator->next();
$valid = $this->iterator->valid();
if (!$valid) {
$this->closed = true;
}
return $valid;
}
/**
* {@inheritDoc}
* @throws \Sidus\EAVModelBundle\Exception\MissingFamilyException
*/
protected function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults(
[
'allow_reset' => false, // Allow the reader to reset it's iterator
'log_count' => false, // Log in state history the result count
]
);
}
/**
* @param \CleverAge\ProcessBundle\Model\ProcessState $state
*
* @throws \Symfony\Component\OptionsResolver\Exception\ExceptionInterface
*
* @return array
*/
protected function getLogContext(ProcessState $state)
{
$logContext = [];
$options = $this->getOptions($state);
if (array_key_exists('family', $options)) {
$options['family'] = $options['family']->getCode();
}
if (array_key_exists('repository', $options)) {
$options['repository'] = \get_class($options['repository']);
}
$logContext['options'] = $options;
return $logContext;
}
}