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

AssetManifestFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
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\DomainInterface;
7
use EventEspresso\core\services\factory\FactoryInterface;
8
use EventEspresso\core\services\loaders\LoaderInterface;
9
use InvalidArgumentException;
10
11
/**
12
 * Class AssetManifestFactory
13
 *
14
 * @package EventEspresso\core\services\assets
15
 * @since   $VID:$
16
 */
17
class AssetManifestFactory implements FactoryInterface
18
{
19
    /**
20
     * @var AssetManifestInterface[]
21
     */
22
    private static $manifests = [];
23
24
    /**
25
     * @var LoaderInterface $loader
26
     */
27
    protected $loader;
28
29
30
    /**
31
     * AssetManifestFactory constructor.
32
     *
33
     * @param LoaderInterface $loader
34
     */
35
    public function __construct(LoaderInterface $loader)
36
    {
37
        $this->loader = $loader;
38
    }
39
40
41
    /**
42
     * returns the applicable AssetManifest for the provided Domain
43
     *
44
     * @param DomainInterface $domain
45
     * @return AssetManifestInterface
46
     */
47
    public function createFromDomainObject(DomainInterface $domain)
48
    {
49
        return $this->getAssetManifestForDomain(AssetManifest::class, $domain);
50
    }
51
52
53
    /**
54
     * for creating an atypical AssetManifest for the Domain provided in the $arguments array
55
     *
56
     * @param string $fqcn      Fully Qualified Class Name
57
     * @param array  $arguments [optional] array of data required for construction
58
     * @return AssetManifestInterface
59
     */
60
    public function create($fqcn, array $arguments = [])
61
    {
62 View Code Duplication
        if (! isset($arguments[0]) || ! $arguments[0] instanceof DomainInterface) {
63
            throw new InvalidArgumentException(
64
                esc_html__(
65
                    'In order to generate an AssetManifest class you need to supply an array where the first argument is an instance of DomainInterface.',
66
                    'event_espresso'
67
                )
68
            );
69
        }
70
        return $this->getAssetManifestForDomain($fqcn, $arguments[0]);
71
    }
72
73
74
    /**
75
     * @param string          $manifest_fqcn
76
     * @param DomainInterface $domain
77
     * @return AssetManifestInterface
78
     */
79
    private function getAssetManifestForDomain($manifest_fqcn, DomainInterface $domain)
80
    {
81
        $domain_fqcn = get_class($domain);
82
        if (! isset(AssetManifestFactory::$manifests[ $domain_fqcn ])) {
83
            $asset_manifest = new $manifest_fqcn($domain);
84
            if (! $asset_manifest instanceof AssetManifestInterface || ! $asset_manifest instanceof $manifest_fqcn) {
85
                throw new DomainException(
86
                    sprintf(
87
                        esc_html__(
88
                            'The requested AssetManifest class "%1$s" could not be loaded.',
89
                            'event_espresso'
90
                        ),
91
                        $manifest_fqcn
92
                    )
93
                );
94
            }
95
            // we still need to share this with the core loader to facilitate automatic dependency injection
96
            $this->loader->share(AssetManifest::class, $asset_manifest, [$domain]);
97
            AssetManifestFactory::$manifests[ $domain_fqcn ] = $asset_manifest;
98
        }
99
        return AssetManifestFactory::$manifests[ $domain_fqcn ];
100
    }
101
}
102