Completed
Branch FET-3467-waitlists (4406f2)
by
unknown
46:31 queued 34:26
created

LoaderFactory::getLoader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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