Completed
Branch EDTR/refactor-fast-api-fetch (cef37c)
by
unknown
09:44 queued 46s
created

ConnectionCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 86
loc 86
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A loadCollection() 26 26 2
A loadConnections() 5 5 1
A getIdentifier() 6 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *     Event Espresso
4
 *     Manage events, sell tickets, and receive payments from your WordPress website.
5
 *     Copyright (c) 2008-2019 Event Espresso  All Rights Reserved.
6
 *
7
 *     This program is free software: you can redistribute it and/or modify
8
 *     it under the terms of the GNU General Public License as published by
9
 *     the Free Software Foundation, either version 3 of the License, or
10
 *     (at your option) any later version.
11
 *
12
 *     This program is distributed in the hope that it will be useful,
13
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *     GNU General Public License for more details.
16
 *
17
 *     You should have received a copy of the GNU General Public License
18
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace EventEspresso\core\services\graphql;
22
23
use EventEspresso\core\exceptions\InvalidInterfaceException;
24
use EventEspresso\core\services\collections\Collection;
25
use EventEspresso\core\services\collections\CollectionDetails;
26
use EventEspresso\core\services\collections\CollectionDetailsException;
27
use EventEspresso\core\services\collections\CollectionInterface;
28
use EventEspresso\core\services\collections\CollectionLoader;
29
use EventEspresso\core\services\collections\CollectionLoaderException;
30
31
/**
32
 * Class ConnectionCollection
33
 * SplObjectStorage Collection of EventEspresso\core\services\graphql\ConnectionInterface objects
34
 *
35
 * @package EventEspresso\core\services\graphql
36
 * @author  Brent Christensen
37
 * @since   $VID:$
38
 */
39 View Code Duplication
class ConnectionCollection extends Collection
40
{
41
42
    const COLLECTION_NAME = 'espresso_graphql_connections';
43
44
    /**
45
     * @var CollectionLoader $loader
46
     */
47
    protected $loader;
48
49
    /**
50
     * ConnectionCollection constructor
51
     *
52
     * @throws InvalidInterfaceException
53
     */
54
    public function __construct()
55
    {
56
        parent::__construct(
57
            'EventEspresso\core\services\graphql\ConnectionInterface',
58
            ConnectionCollection::COLLECTION_NAME
59
        );
60
    }
61
62
63
    /**
64
     * @throws CollectionDetailsException
65
     * @throws CollectionLoaderException
66
     * @since $VID:$
67
     */
68
    private function loadCollection()
69
    {
70
        if (! $this->loader instanceof CollectionLoader) {
71
            $this->loader = new CollectionLoader(
72
                new CollectionDetails(
73
                // collection name
74
                    ConnectionCollection::COLLECTION_NAME,
75
                    // collection interface
76
                    'EventEspresso\core\services\graphql\ConnectionInterface',
77
                    // FQCNs for classes to add (all classes within each namespace will be loaded)
78
                    apply_filters(
79
                        'FHEE__EventEspresso_core_services_graphql_ConnectionCollection__loadCollection__collection_FQCNs',
80
                        ['EventEspresso\core\domain\services\graphql\connections']
81
                    ),
82
                    // filepaths to classes to add
83
                    array(),
84
                    // file mask to use if parsing folder for files to add
85
                    '',
86
                    // what to use as identifier for collection entities
87
                    // using CLASS NAME prevents duplicates (works like a singleton)
88
                    CollectionDetails::ID_CLASS_NAME
89
                ),
90
                $this
91
            );
92
        }
93
    }
94
95
96
    /**
97
     * @return CollectionInterface
98
     * @throws CollectionDetailsException
99
     * @throws CollectionLoaderException
100
     * @since $VID:$
101
     */
102
    public function loadConnections()
103
    {
104
        $this->loadCollection();
105
        return $this->loader->getCollection();
106
    }
107
108
109
    /**
110
     * getIdentifier
111
     * Overrides EventEspresso\core\services\collections\Collection::getIdentifier()
112
     * If no $identifier is supplied, then the  fully qualified class name is used
113
     *
114
     * @param        $object
115
     * @param mixed  $identifier
116
     * @return bool
117
     */
118
    public function getIdentifier($object, $identifier = null)
119
    {
120
        return ! empty($identifier)
121
            ? $identifier
122
            : get_class($object);
123
    }
124
}