Completed
Branch FET-10816-display-form-wide-er... (b27d66)
by
unknown
153:47 queued 140:47
created

LoaderFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getLoader() 0 15 3
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use EE_Registry;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\exceptions\InvalidInterfaceException;
8
use EventEspresso\core\services\collections\LooseCollection;
9
use InvalidArgumentException;
10
11
defined('EVENT_ESPRESSO_VERSION') || exit;
12
13
14
/**
15
 * Class LoaderFactory
16
 * All hard dependencies should be constructor injected wherever possible.
17
 * But other times, objects need to be created dynamically within another class
18
 * after some decision has been made, or relevant data has been collected.
19
 * If the constructors for the classes being generated have injected dependencies themselves,
20
 * then we want to use the Loader class for creating the classes as opposed to using the "new" keyword,
21
 * because using the Loader will ensure that all of the dependencies will get automatically injected.
22
 * So then the issue becomes HOW to get access to the Loader !?!?
23
 * First off, we do NOT  want  to be injecting the Loader all over the place,
24
 * because this is considered to be an anti pattern called "Service Location",
25
 * so instead we want to use Factory classes anywhere we would need to use the "new" keyword,
26
 * and then inject the factories we need into our other classes.
27
 * It is considered acceptable to inject the Loader into these factories.
28
 * But since the Loader would now be a hard dependency for these factories,
29
 * it too should be injected into the constructor just like any other hard dependency.
30
 * However, since our dependency tree is not yet complete at this moment,
31
 * at some point a class would need to use the "new" keyword to generate the Loader.
32
 * With most other classes this wouldn't be an issue, but since the Loader needs to be able to provide
33
 * shared instances of classes, we do NOT ever want more than one copy of the Loader floating around.
34
 * And that's the purpose of this class.
35
 * This class is a temporary measure for providing access to the ONE single instance of the Loader.
36
 * The Loader should still be injected into your factory class constructor
37
 * so that it can ultimately be provided via the dependency tree,
38
 * but it should not YET be required, and a class property should be created for holding the Loader.
39
 * ie:
40
 *
41
 *      private $loader;
42
 *
43
 *      public function __construct(LoaderInterface $loader = null)
44
 *      {
45
 *          $this->loader = $loader;
46
 *      }
47
 *
48
 * The getter for the Loader should check the instance to see if injection was successful,
49
 * and if not, use THIS class to obtain the ONE single instance of the Loader.
50
 * ie:
51
 *
52
 *      private function getLoader()
53
 *      {
54
 *          if (! $this->loader instanceof LoaderInterface) {
55
 *              $this->loader = LoaderFactory::getLoader();
56
 *          }
57
 *          return $this->loader;
58
 *      }
59
 *
60
 * Then the methods in your factory class that generate their target classes,
61
 * can use this getter to obtain the Loader to use for generating objects.
62
 * ie:
63
 *
64
 *      public function createSomeObject(array $arguments = array())
65
 *      {
66
 *          return $this->loader->getNew('fully/qualified/ClassName', $arguments);
67
 *      }
68
 *
69
 * Eventually, when our dependency tree is complete
70
 * and ALL class construction can be traced back to the DI container,
71
 * then any injected factories can obtain their instance of the Loader via their constructor,
72
 * and this LoaderFactory (and the need to have getLoader() methods that use it)
73
 * will be completely unnecessary and can be removed.
74
 *
75
 * @package       Event Espresso
76
 * @author        Brent Christensen
77
 * @since         4.9.44
78
 */
79
class LoaderFactory
80
{
81
82
    /**
83
     * @var LoaderInterface $loader ;
84
     */
85
    private static $loader;
0 ignored issues
show
Unused Code introduced by
The property $loader is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
86
87
88
89
    /**
90
     * @param mixed $generator      provided during  very first instantiation in
91
     *                              EE_Load_Espresso_Core::handle_request()
92
     *                              otherwise can be left null
93
     * @return LoaderInterface
94
     * @throws InvalidArgumentException
95
     * @throws InvalidInterfaceException
96
     * @throws InvalidDataTypeException
97
     */
98
    public static function getLoader($generator = null)
99
    {
100
        if (! LoaderFactory::$loader instanceof LoaderInterface) {
101
            $generator = $generator !== null ? $generator : EE_Registry::instance();
102
            $core_loader = new CoreLoader($generator);
103
            LoaderFactory::$loader = new Loader(
104
                $core_loader,
105
                new CachingLoader(
106
                    $core_loader,
107
                    new LooseCollection('')
108
                )
109
            );
110
        }
111
        return LoaderFactory::$loader;
112
    }
113
114
115
}
116