PackageLocator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 86
ccs 17
cts 17
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 13 3
A __construct() 0 5 3
A set() 0 4 1
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura Project for PHP.
5
 *
6
 * @package aura/intl
7
 *
8
 * @license http://opensource.org/licenses/MIT MIT
9
 *
10
 */
11
namespace Aura\Intl;
12
13
/**
14
 *
15
 * A ServiceLocator implementation for loading and retaining translator objects.
16
 *
17
 * @package aura/intl
18
 *
19
 */
20
class PackageLocator implements PackageLocatorInterface
21
{
22
    /**
23
     *
24
     * A registry of packages.
25
     *
26
     * Unlike many other registries, this one is two layers deep. The first
27
     * key is a package name, the second key is a locale code, and the value
28
     * is a callable that returns a Package object for that name and locale.
29
     *
30
     * @var array
31
     *
32
     */
33
    protected $registry = [];
34
35
    /**
36
     *
37
     * Tracks whether or not a registry entry has been converted from a
38
     * callable to a Package object.
39
     *
40
     * @var array
41
     *
42
     */
43
    protected $converted = [];
44
45
    /**
46
     *
47
     * Constructor.
48
     *
49
     * @param array $registry A registry of packages.
50
     *
51
     * @see $registry
52
     *
53
     */
54 9
    public function __construct(array $registry = [])
55
    {
56 9
        foreach ($registry as $name => $locales) {
57 8
            foreach ($locales as $locale => $spec) {
58 8
                $this->set($name, $locale, $spec);
59
            }
60
        }
61 9
    }
62
63
    /**
64
     *
65
     * Sets a Package object.
66
     *
67
     * @param string $name The package name.
68
     *
69
     * @param string $locale The locale for the package.
70
     *
71
     * @param callable $spec A callable that returns a package.
72
     *
73
     * @return void
74
     *
75
     */
76 8
    public function set($name, $locale, callable $spec)
77
    {
78 8
        $this->registry[$name][$locale] = $spec;
79 8
        $this->converted[$name][$locale] = false;
80 8
    }
81
82
    /**
83
     *
84
     * Gets a Package object.
85
     *
86
     * @param string $name The package name.
87
     *
88
     * @param string $locale The locale for the package.
89
     *
90
     * @return Package
91
     *
92
     */
93 3
    public function get($name, $locale)
94
    {
95 3
        if (! isset($this->registry[$name][$locale])) {
96 1
            throw new Exception("Package '$name' with locale '$locale' is not registered.");
97
        }
98
99 3
        if (! $this->converted[$name][$locale]) {
100 3
            $func = $this->registry[$name][$locale];
101 3
            $this->registry[$name][$locale] = $func();
102 3
            $this->converted[$name][$locale] = true;
103
        }
104
105 3
        return $this->registry[$name][$locale];
106
    }
107
}
108