Completed
Branch Gutenberg/master (b3a823)
by
unknown
73:52 queued 60:28
created

DomainFactory::getEventEspressoCoreDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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\InvalidClassException;
10
use EventEspresso\core\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidFilePathException;
12
use EventEspresso\core\exceptions\InvalidInterfaceException;
13
use InvalidArgumentException;
14
use EventEspresso\core\services\loaders\LoaderFactory;
15
16
/**
17
 * Class DomainFactory
18
 * Factory class for generating addon Domain objects
19
 *
20
 * @package EventEspresso\core\domain
21
 * @author  Brent Christensen
22
 * @since   4.9.50
23
 */
24
class DomainFactory
25
{
26
27
    /**
28
     * @param FullyQualifiedName $domain_fqcn   [required] Fully Qualified Class Name for the Domain class
29
     * @param array $arguments                  [required] array of arguments to be passed to the Domain class
30
     *                                          constructor. Must at least include the following two value objects:
31
     *                                          array(
32
     *                                              EventEspresso\core\domain\values\FilePath $plugin_file
33
     *                                              EventEspresso\core\domain\values\Version $version
34
     *                                          )
35
     * @return mixed
36
     * @throws DomainException
37
     * @throws InvalidArgumentException
38
     * @throws InvalidDataTypeException
39
     * @throws InvalidInterfaceException
40
     */
41
    public static function getShared(FullyQualifiedName $domain_fqcn, array $arguments)
42
    {
43
        if (! isset($arguments[0], $arguments[1])) {
44
            throw new InvalidArgumentException(
45
                esc_html__(
46
                    'You need to pass at least two arguments, representing the addon plugin file and version, in order to generate a Domain class',
47
                    'event_espresso'
48
                )
49
            );
50
        }
51
        $domain = LoaderFactory::getLoader()->getShared($domain_fqcn, $arguments);
52
        if (! $domain instanceof $domain_fqcn && ! $domain instanceof DomainBase) {
53
            throw new DomainException(
54
                sprintf(
55
                    esc_html__(
56
                        'The requested Domain class "%1$s" could not be loaded.',
57
                        'event_espresso'
58
                    ),
59
                    $domain_fqcn
60
                )
61
            );
62
        }
63
        return $domain;
64
    }
65
66
67
    /**
68
     * @return Domain
69
     * @throws DomainException
70
     * @throws InvalidArgumentException
71
     * @throws InvalidDataTypeException
72
     * @throws InvalidFilePathException
73
     * @throws InvalidInterfaceException
74
     */
75
    public static function getEventEspressoCoreDomain()
76
    {
77
        $domain = new Domain(
78
            new FilePath(EVENT_ESPRESSO_MAIN_FILE),
79
            Version::fromString(espresso_version())
80
        );
81
        LoaderFactory::getLoader()->share('EventEspresso\core\domain\Domain', $domain);
82
        return $domain;
83
    }
84
}
85