Completed
Branch barista-prod (8f1757)
by
unknown
38:38 queued 29:41
created

DomainFactory::getDomain()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 31

Duplication

Lines 8
Ratio 25.81 %

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 2
dl 8
loc 31
rs 8.4906
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain;
4
5
use DomainException;
6
use EventEspresso\core\domain\values\FilePath;
7
use EventEspresso\core\domain\values\FullyQualifiedName;
8
use EventEspresso\core\domain\values\Version;
9
use EventEspresso\core\exceptions\InvalidDataTypeException;
10
use EventEspresso\core\exceptions\InvalidFilePathException;
11
use EventEspresso\core\exceptions\InvalidInterfaceException;
12
use InvalidArgumentException;
13
use EventEspresso\core\services\loaders\LoaderFactory;
14
15
/**
16
 * Class DomainFactory
17
 * Factory class for generating addon Domain objects
18
 *
19
 * @package EventEspresso\core\domain
20
 * @author  Brent Christensen
21
 * @since   4.9.50
22
 */
23
class DomainFactory
24
{
25
    /**
26
     * @var DomainInterface[]
27
     */
28
    protected static $domains = [];
29
30
31
    /**
32
     * @param FullyQualifiedName $domain_fqcn       [required] Fully Qualified Class Name for the Domain class
33
     * @param array              $arguments         [required] array of arguments to be passed to the Domain class
34
     *                                              constructor. Must at least include the following two value objects:
35
     *                                              array(
36
     *                                              EventEspresso\core\domain\values\FilePath $plugin_file
37
     *                                              EventEspresso\core\domain\values\Version $version
38
     *                                              )
39
     * @return DomainInterface
40
     * @throws DomainException
41
     * @throws InvalidArgumentException
42
     * @throws InvalidDataTypeException
43
     * @throws InvalidInterfaceException
44
     */
45
    public static function getShared(FullyQualifiedName $domain_fqcn, array $arguments)
46
    {
47
        $fqcn = $domain_fqcn->string();
48
        return DomainFactory::getDomain($fqcn, $arguments);
49
    }
50
51
52
    /**
53
     * @return DomainInterface
54
     * @throws DomainException
55
     * @throws InvalidArgumentException
56
     * @throws InvalidDataTypeException
57
     * @throws InvalidFilePathException
58
     * @throws InvalidInterfaceException
59
     */
60
    public static function getEventEspressoCoreDomain()
61
    {
62
        $fqcn = 'EventEspresso\core\domain\Domain';
63
        if (! isset(DomainFactory::$domains[ $fqcn ])) {
64
            DomainFactory::getDomain($fqcn, [EVENT_ESPRESSO_MAIN_FILE, espresso_version()]);
65
        }
66
        return DomainFactory::$domains[ $fqcn ];
67
    }
68
69
70
    /**
71
     * @param string $fqcn
72
     * @param array  $arguments
73
     * @return DomainInterface
74
     */
75
    private static function getDomain($fqcn, array $arguments)
76
    {
77
        if (! isset(DomainFactory::$domains[ $fqcn ])) {
78 View Code Duplication
            if (! isset($arguments[0], $arguments[1])) {
79
                throw new InvalidArgumentException(
80
                    esc_html__(
81
                        'You need to pass at least two arguments, representing the addon plugin file and version, in order to generate a Domain class',
82
                        'event_espresso'
83
                    )
84
                );
85
            }
86
            $filepath = $arguments[0] instanceof FilePath ? $arguments[0] : new FilePath($arguments[0]);
87
            $version  = $arguments[1] instanceof Version ? $arguments[1] : Version::fromString($arguments[1]);
88
            $domain   = new $fqcn($filepath, $version);
89
            if (! $domain instanceof DomainBase || ! $domain instanceof $fqcn) {
90
                throw new DomainException(
91
                    sprintf(
92
                        esc_html__(
93
                            'The requested Domain class "%1$s" could not be loaded.',
94
                            'event_espresso'
95
                        ),
96
                        $fqcn
97
                    )
98
                );
99
            }
100
            DomainFactory::$domains[ $fqcn ] = $domain;
101
            // we still need to share this with the core loader to facilitate automatic dependency injection
102
            LoaderFactory::getLoader()->share($fqcn, $domain, [$filepath, $version, $domain->assetNamespace()]);
103
        }
104
        return DomainFactory::$domains[ $fqcn ];
105
    }
106
}
107