Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
API
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_locations_for_postcode
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Given a country and postcode, find the state and list of valid cities for that postcode
4 *
5 * @package brianhenryie/bh-wc-postcode-address-autofill
6 */
7
8namespace BrianHenryIE\WC_Postcode_Address_Autofill\API;
9
10use BrianHenryIE\WC_Postcode_Address_Autofill\API_Interface;
11use BrianHenryIE\WC_Postcode_Address_Autofill\Settings_Interface;
12
13/**
14 * Reads saved files containing lookup tables.
15 */
16class API implements API_Interface {
17
18    /**
19     * Had been using the plugin basename to determine paths on the filesystem.
20     *
21     * @uses Settings::get_plugin_basename()
22     */
23    protected Settings_Interface $settings;
24
25    /**
26     * Object to fetch data from cache/db/disk.
27     */
28    protected Data_Loader $data_loader;
29
30    /**
31     * Constructor
32     *
33     * @param Data_Loader        $data_loader Object to fetch data from cache/db/disk.
34     * @param Settings_Interface $settings The plugin settings.
35     */
36    public function __construct( Data_Loader $data_loader, Settings_Interface $settings ) {
37        $this->settings    = $settings;
38        $this->data_loader = $data_loader;
39    }
40
41    /**
42     * Look-up the list of valid cities for a given postcode in a given country.
43     *
44     * @param string $country The country, used to determine which file to load.
45     * @param string $postcode The postcode to search for.
46     *
47     * @return ?Postcode_Locations_Result Null if no data available, empty if nothing found.
48     */
49    public function get_locations_for_postcode( string $country, string $postcode ): ?Postcode_Locations_Result {
50        $country = strtoupper( $country );
51
52        $country_data = $this->data_loader->get_data_for_country( $country );
53
54        if ( is_null( $country_data ) ) {
55            return null;
56        }
57
58        return $country_data->get_locations_for_postcode( $postcode );
59    }
60}