Issues (6)

lib/DoctrineOrm/Loader/DeferredEntityLoader.php (1 issue)

1
<?php
2
3
namespace Malef\Associate\DoctrineOrm\Loader;
4
5
use GraphQL\Deferred;
6
use Malef\Associate\DoctrineOrm\Association\AssociationTree;
7
use Malef\Associate\DoctrineOrm\Loader\ArgumentConverter\EntitiesArgumentConverter;
8
9
class DeferredEntityLoader
10
{
11
    /**
12
     * @var EntityLoader
13
     */
14
    protected $entityLoader;
15
16
    /**
17
     * @var EntitiesArgumentConverter
18
     */
19
    protected $entitiesArgumentConverter;
20
21
    /**
22
     * @var AssociationTree
23
     */
24
    protected $associationTree;
25
26
    /**
27
     * @var string|null
28
     */
29
    protected $entityClassName;
30
31
    /**
32
     * @var array
33
     */
34
    protected $entities = [];
35
36
    public function __construct(
37
        EntitiesArgumentConverter $entitiesArgumentConverter,
38
        EntityLoader $entityLoader,
39
        AssociationTree $associationTree,
40
        ?string $entityClassName
41
    ) {
42
        $this->entitiesArgumentConverter = $entitiesArgumentConverter;
43
        $this->entityLoader = $entityLoader;
44
        $this->associationTree = $associationTree;
45
        $this->entityClassName = $entityClassName;
46
    }
47
48
    public function createDeferred(iterable $entities, \Closure $resolveCallback): Deferred
49
    {
50
        foreach ($entities as $entity) {
51
            $this->entities[] = $entity;
52
        }
53
54
        return new Deferred(
55
            function () use ($resolveCallback) {
56
                $this->load();
57
58
                return $resolveCallback();
59
            }
60
        );
61
    }
62
63
    protected function load(): void
64
    {
65
        if (!$this->entities) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entities of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
            return;
67
        }
68
69
        $entities = $this->entitiesArgumentConverter->convertEntitiesToArrayCollection($this->entities);
70
71
        $this->entityLoader->load(
72
            $entities,
73
            $this->associationTree,
74
            $this->entityClassName
75
        );
76
77
        $this->entities = [];
78
    }
79
}
80