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

BaristaFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use DomainException;
6
use EventEspresso\core\domain\DomainFactory;
7
use EventEspresso\core\domain\DomainInterface;
8
use EventEspresso\core\services\factory\FactoryInterface;
9
use EventEspresso\core\domain\values\FullyQualifiedName;
10
use EventEspresso\core\services\loaders\LoaderInterface;
11
12
class BaristaFactory implements FactoryInterface
13
{
14
    /**
15
     * @var AssetManifestFactory
16
     */
17
    private $manifest_factory;
18
19
    /**
20
     * @var BaristaInterface[]
21
     */
22
    private static $baristas = [];
23
24
    /**
25
     * @var LoaderInterface $loader
26
     */
27
    protected $loader;
28
29
30
    /**
31
     * BaristaFactory constructor.
32
     *
33
     * @param AssetManifestFactory $manifest_factory
34
     * @param LoaderInterface      $loader
35
     */
36
    public function __construct(AssetManifestFactory $manifest_factory, LoaderInterface $loader)
37
    {
38
        $this->manifest_factory = $manifest_factory;
39
        $this->loader           = $loader;
40
    }
41
42
43
    /**
44
     * @param DomainInterface $domain
45
     * @return BaristaInterface
46
     */
47
    public function createFromDomainObject(DomainInterface $domain)
48
    {
49
        $asset_manifest = $this->manifest_factory->createFromDomainObject($domain);
50
        return $this->getBaristaForDomain($asset_manifest, $domain);
51
    }
52
53
54
    /**
55
     * @param string $domain_fqcn      Fully Qualified Class Name for the applicable DomainInterface class
56
     * @param array  $domain_arguments arguments required by the applicable DomainInterface class
57
     * @return BaristaInterface
58
     */
59
    public function create($domain_fqcn = '', array $domain_arguments = [])
60
    {
61
        $domain         = $this->getDomain($domain_fqcn, $domain_arguments);
62
        $asset_manifest = $this->manifest_factory->createFromDomainObject($domain);
63
        return $this->getBaristaForDomain($asset_manifest, $domain);
64
    }
65
66
67
    /**
68
     * @param AssetManifestInterface $asset_manifest
69
     * @param DomainInterface        $domain
70
     * @return BaristaInterface
71
     */
72
    private function getBaristaForDomain(AssetManifestInterface $asset_manifest, DomainInterface $domain)
73
    {
74
        $domain_fqcn = get_class($domain);
75
        if (! isset(BaristaFactory::$baristas[ $domain_fqcn ])) {
76
            $barista = new Barista($asset_manifest);
77
            // we still need to share this with the core loader to facilitate automatic dependency injection
78
            $this->loader->share(Barista::class, $barista, [$asset_manifest]);
79
            BaristaFactory::$baristas[ $domain_fqcn ] = $barista;
80
        }
81
        return BaristaFactory::$baristas[ $domain_fqcn ];
82
    }
83
84
85
    /**
86
     * @param string $domain_fqcn Fully Qualified Class Name for the applicable DomainInterface class
87
     * @param array  $arguments
88
     * @return DomainInterface
89
     */
90
    private function getDomain($domain_fqcn, array $arguments = [])
91
    {
92
        // if no FQCN is supplied for the domain, then we are loading the defaults for core
93
        // add-ons will always have to supply their domain FQCN and arguments to retrieve their manifest
94
        $domain = empty($domain_fqcn)
95
            ? DomainFactory::getEventEspressoCoreDomain()
96
            : DomainFactory::getShared(new FullyQualifiedName($domain_fqcn), $arguments);
97
        if ($domain instanceof DomainInterface) {
98
            return $domain;
99
        }
100
        throw new DomainException(
101
            sprintf(
102
                esc_html__(
103
                    'BaristaFactory::create() requires a fully qualified class name (FQCN) for the currently applicable Domain object.
104
                    %1$sThe supplied FQCN ("%2$s") is either invalid or the class is missing.',
105
                    'event_espresso'
106
                ),
107
                '<br />',
108
                $domain_fqcn
109
            )
110
        );
111
    }
112
}
113