Completed
Pull Request — master (#94)
by Joshua
82:48 queued 60:07
created

MultiFileMetadataSourceImpl::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2.0185
1
<?php
2
/**
3
 *
4
 *
5
 * @author joshuag
6
 * @created: 04/08/2015 09:03
7
 * @project libphonenumber-for-php
8
 */
9
10
namespace libphonenumber;
11
12
13
class MultiFileMetadataSourceImpl implements MetadataSourceInterface
14
{
15
    private static $metaDataFilePrefix = PhoneNumberUtil::META_DATA_FILE_PREFIX;
16
17
    /**
18
     * A mapping from a region code to the PhoneMetadata for that region.
19
     * @var PhoneMetadata[]
20
     */
21
    private $regionToMetadataMap = array();
22
23
    /**
24
     * A mapping from a country calling code for a non-geographical entity to the PhoneMetadata for
25
     * that country calling code. Examples of the country calling codes include 800 (International
26
     * Toll Free Service) and 808 (International Shared Cost Service).
27
     * @var PhoneMetadata[]
28
     */
29
    private $countryCodeToNonGeographicalMetadataMap = array();
30
31
    /**
32
     * The prefix of the metadata files from which region data is loaded.
33
     * @var String
34
     */
35
    private $currentFilePrefix;
36
37
38
    /**
39
     * The metadata loader used to inject alternative metadata sources.
40
     * @var MetadataLoaderInterface
41
     */
42
    private $metadataLoader;
43
44
    /**
45
     * @param MetadataLoaderInterface $metadataLoader
46
     * @param string|null $currentFilePrefix
47
     */
48 403
    public function __construct(MetadataLoaderInterface $metadataLoader, $currentFilePrefix = null)
49
    {
50 403
        if ($currentFilePrefix === null) {
51
            $currentFilePrefix = self::$metaDataFilePrefix;
52
        }
53
54 403
        $this->currentFilePrefix = $currentFilePrefix;
55 403
        $this->metadataLoader = $metadataLoader;
56 403
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 4037
    public function getMetadataForRegion($regionCode)
62
    {
63 4037
        if (!array_key_exists($regionCode, $this->regionToMetadataMap)) {
64
            // The regionCode here will be valid and won't be '001', so we don't need to worry about
65
            // what to pass in for the country calling code.
66 598
            $this->loadMetadataFromFile($this->currentFilePrefix, $regionCode, 0, $this->metadataLoader);
67
        }
68
69 4037
        return $this->regionToMetadataMap[$regionCode];
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 31
    public function getMetadataForNonGeographicalRegion($countryCallingCode)
76
    {
77 31
        if (!array_key_exists($countryCallingCode, $this->countryCodeToNonGeographicalMetadataMap)) {
78 31
            $this->loadMetadataFromFile($this->currentFilePrefix, PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY, $countryCallingCode, $this->metadataLoader);
79
        }
80
81 31
        return $this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode];
82
    }
83
84
    /**
85
     * @param string $filePrefix
86
     * @param string $regionCode
87
     * @param int $countryCallingCode
88
     * @param MetadataLoaderInterface $metadataLoader
89
     * @throws \RuntimeException
90
     */
91 611
    public function loadMetadataFromFile($filePrefix, $regionCode, $countryCallingCode, MetadataLoaderInterface $metadataLoader)
92
    {
93 611
        $isNonGeoRegion = PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCode;
94 611
        $fileName = $filePrefix . '_' . ($isNonGeoRegion ? $countryCallingCode : $regionCode) . '.php';
95 611
        if (!is_readable($fileName)) {
96 1
            throw new \RuntimeException('missing metadata: ' . $fileName);
97
        } else {
98 610
            $data = $metadataLoader->loadMetadata($fileName);
99 610
            $metadata = new PhoneMetadata();
100 610
            $metadata->fromArray($data);
101 610
            if ($isNonGeoRegion) {
102 31
                $this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode] = $metadata;
103
            } else {
104 598
                $this->regionToMetadataMap[$regionCode] = $metadata;
105
            }
106
        }
107 610
    }
108
109
}
110