Completed
Branch EDTR/graphql-data-loaders (8642bb)
by
unknown
09:27 queued 44s
created

DataLoaderCollection::loadCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\graphql\loaders;
4
5
use EventEspresso\core\exceptions\InvalidInterfaceException;
6
use EventEspresso\core\services\collections\Collection;
7
use EventEspresso\core\services\collections\CollectionDetails;
8
use EventEspresso\core\services\collections\CollectionDetailsException;
9
use EventEspresso\core\services\collections\CollectionInterface;
10
use EventEspresso\core\services\collections\CollectionLoader;
11
use EventEspresso\core\services\collections\CollectionLoaderException;
12
13
/**
14
 * Class DataLoaderCollection
15
 * SplObjectStorage Collection of EventEspresso\core\services\graphql\GQLDataDomainInterface objects
16
 *
17
 * @package EventEspresso\core\services\graphql
18
 * @author  Brent Christensen
19
 * @since   $VID:$
20
 */
21
class DataLoaderCollection extends Collection
22
{
23
24
    const COLLECTION_NAME = 'espresso_graphql_data_loaders';
25
26
    const COLLECTION_INTERFACE = 'EventEspresso\core\services\graphql\loaders\GQLDataDomainInterface';
27
28
    /**
29
     * @var CollectionLoader $loader
30
     */
31
    protected $loader;
32
33
34
    /**
35
     * DataLoaderCollection constructor
36
     *
37
     * @throws InvalidInterfaceException
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct(
42
            DataLoaderCollection::COLLECTION_INTERFACE,
43
            DataLoaderCollection::COLLECTION_NAME
44
        );
45
    }
46
47
48
    /**
49
     * @throws CollectionDetailsException
50
     * @throws CollectionLoaderException
51
     * @since $VID:$
52
     */
53
    private function loadCollection()
54
    {
55
        if (! $this->loader instanceof CollectionLoader) {
56
            $this->loader = new CollectionLoader(
57
                new CollectionDetails(
58
                    // collection name
59
                    DataLoaderCollection::COLLECTION_NAME,
60
                    // collection interface
61
                    DataLoaderCollection::COLLECTION_INTERFACE,
62
                    // FQCNs for classes to add (all classes within each namespace will be loaded)
63
                    apply_filters(
64
                        'FHEE__EventEspresso_core_services_graphql_DataLoaderCollection__loadCollection__collection_FQCNs',
65
                        ['EventEspresso\core\domain\services\graphql\data\domains']
66
                    ),
67
                    // filepaths to classes to add
68
                    array(),
69
                    // file mask to use if parsing folder for files to add
70
                    '',
71
                    // what to use as identifier for collection entities
72
                    // using CLASS NAME prevents duplicates (works like a singleton)
73
                    CollectionDetails::ID_CLASS_NAME
74
                ),
75
                $this
76
            );
77
        }
78
    }
79
80
81
    /**
82
     * @return CollectionInterface
83
     * @throws CollectionDetailsException
84
     * @throws CollectionLoaderException
85
     * @since $VID:$
86
     */
87
    public function getDataLoaders()
88
    {
89
        $this->loadCollection();
90
        return $this->loader->getCollection();
91
    }
92
}
93