These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace FOS\ElasticaBundle\Persister; |
||
4 | |||
5 | use FOS\ElasticaBundle\Persister\Event\Events; |
||
6 | use FOS\ElasticaBundle\Persister\Event\OnExceptionEvent; |
||
7 | use FOS\ElasticaBundle\Persister\Event\PostInsertObjectsEvent; |
||
8 | use FOS\ElasticaBundle\Persister\Event\PostPersistEvent; |
||
9 | use FOS\ElasticaBundle\Persister\Event\PreFetchObjectsEvent; |
||
10 | use FOS\ElasticaBundle\Persister\Event\PreInsertObjectsEvent; |
||
11 | use FOS\ElasticaBundle\Persister\Event\PrePersistEvent; |
||
12 | use FOS\ElasticaBundle\Provider\PagerInterface; |
||
13 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
14 | use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; |
||
15 | |||
16 | final class InPlacePagerPersister implements PagerPersisterInterface |
||
17 | { |
||
18 | const NAME = 'in_place'; |
||
19 | |||
20 | /** |
||
21 | * @var PersisterRegistry |
||
22 | */ |
||
23 | private $registry; |
||
24 | |||
25 | /** |
||
26 | * @var EventDispatcherInterface |
||
27 | */ |
||
28 | private $dispatcher; |
||
29 | |||
30 | /** |
||
31 | * @param PersisterRegistry $registry |
||
32 | * @param EventDispatcherInterface $dispatcher |
||
33 | */ |
||
34 | 11 | View Code Duplication | public function __construct(PersisterRegistry $registry, EventDispatcherInterface $dispatcher) |
35 | { |
||
36 | 11 | $this->registry = $registry; |
|
37 | 11 | $this->dispatcher = $dispatcher; |
|
38 | |||
39 | 11 | if (class_exists(LegacyEventDispatcherProxy::class)) { |
|
40 | 11 | $this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher); |
|
41 | } |
||
42 | 11 | } |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 10 | public function insert(PagerInterface $pager, array $options = array()) |
|
48 | { |
||
49 | 10 | $pager->setMaxPerPage(empty($options['max_per_page']) ? 100 : $options['max_per_page']); |
|
50 | |||
51 | 10 | $options = array_replace([ |
|
52 | 10 | 'max_per_page' => $pager->getMaxPerPage(), |
|
53 | 10 | 'first_page' => $pager->getCurrentPage(), |
|
54 | 10 | 'last_page' => $pager->getNbPages(), |
|
55 | ], $options); |
||
56 | |||
57 | 10 | $pager->setCurrentPage($options['first_page']); |
|
58 | |||
59 | 10 | $objectPersister = $this->registry->getPersister($options['indexName'], $options['typeName']); |
|
60 | |||
61 | try { |
||
62 | 10 | $event = new PrePersistEvent($pager, $objectPersister, $options); |
|
0 ignored issues
–
show
|
|||
63 | 10 | $this->dispatcher->dispatch(Events::PRE_PERSIST, $event); |
|
64 | 10 | $pager = $event->getPager(); |
|
65 | 10 | $options = $event->getOptions(); |
|
66 | |||
67 | 10 | $lastPage = min($options['last_page'], $pager->getNbPages()); |
|
68 | 10 | $page = $pager->getCurrentPage(); |
|
69 | do { |
||
70 | 10 | $pager->setCurrentPage($page); |
|
71 | |||
72 | 10 | $this->insertPage($page, $pager, $objectPersister, $options); |
|
0 ignored issues
–
show
It seems like
$objectPersister defined by $this->registry->getPers..., $options['typeName']) on line 59 can be null ; however, FOS\ElasticaBundle\Persi...Persister::insertPage() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
Loading history...
|
|||
73 | |||
74 | 9 | $page++; |
|
75 | 9 | } while ($page <= $lastPage); |
|
76 | 9 | } finally { |
|
77 | 10 | $event = new PostPersistEvent($pager, $objectPersister, $options); |
|
0 ignored issues
–
show
It seems like
$objectPersister defined by $this->registry->getPers..., $options['typeName']) on line 59 can be null ; however, FOS\ElasticaBundle\Persi...istEvent::__construct() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
Loading history...
|
|||
78 | 10 | $this->dispatcher->dispatch(Events::POST_PERSIST, $event); |
|
79 | } |
||
80 | 9 | } |
|
81 | |||
82 | /** |
||
83 | * @param int $page |
||
84 | * @param PagerInterface $pager |
||
85 | * @param ObjectPersisterInterface $objectPersister |
||
86 | * @param array $options |
||
87 | * |
||
88 | * @throws \Exception |
||
89 | */ |
||
90 | 10 | private function insertPage($page, PagerInterface $pager, ObjectPersisterInterface $objectPersister, array $options = array()) |
|
91 | { |
||
92 | 10 | $pager->setCurrentPage($page); |
|
93 | |||
94 | 10 | $event = new PreFetchObjectsEvent($pager, $objectPersister, $options); |
|
95 | 10 | $this->dispatcher->dispatch(Events::PRE_FETCH_OBJECTS, $event); |
|
96 | 10 | $pager = $event->getPager(); |
|
97 | 10 | $options = $event->getOptions(); |
|
98 | |||
99 | 10 | $objects = $pager->getCurrentPageResults(); |
|
100 | |||
101 | 10 | if ($objects instanceof \Traversable) { |
|
102 | $objects = iterator_to_array($objects); |
||
103 | } |
||
104 | |||
105 | 10 | $event = new PreInsertObjectsEvent($pager, $objectPersister, $objects, $options); |
|
106 | 10 | $this->dispatcher->dispatch(Events::PRE_INSERT_OBJECTS, $event); |
|
107 | 10 | $pager = $event->getPager(); |
|
108 | 10 | $options = $event->getOptions(); |
|
109 | 10 | $objects = $event->getObjects(); |
|
110 | |||
111 | try { |
||
112 | 10 | if (!empty($objects)) { |
|
113 | 10 | $objectPersister->insertMany($objects); |
|
114 | } |
||
115 | |||
116 | 8 | $event = new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options); |
|
117 | 8 | $this->dispatcher->dispatch(Events::POST_INSERT_OBJECTS, $event); |
|
118 | 2 | } catch (\Exception $e) { |
|
119 | 2 | $event = new OnExceptionEvent($pager, $objectPersister, $e, $objects, $options); |
|
120 | 2 | $this->dispatcher->dispatch(Events::ON_EXCEPTION, $event); |
|
121 | |||
122 | 2 | if ($event->isIgnored()) { |
|
123 | 1 | $event = new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options); |
|
124 | 1 | $this->dispatcher->dispatch(Events::POST_INSERT_OBJECTS, $event); |
|
125 | } else { |
||
126 | 1 | $e = $event->getException(); |
|
127 | |||
128 | 1 | throw $e; |
|
129 | } |
||
130 | } |
||
131 | 9 | } |
|
132 | |||
133 | } |
||
134 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: