Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
78 / 90
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Snap_Marketing_Environment
88.64% covered (warning)
88.64%
78 / 88
75.00% covered (warning)
75.00%
6 / 8
39.01
0.00% covered (danger)
0.00%
0 / 1
 allowed_environments
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 normalize
47.06% covered (danger)
47.06%
8 / 17
0.00% covered (danger)
0.00%
0 / 1
38.08
 is_live
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 credential_option_keys
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
6
 endpoint_constant_names
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
6
 resolve_endpoint
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 admin_environment_options
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 credentials_configured
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Pure helpers for Snap Marketing environment resolution (unit-testable).
4 *
5 * @package snap_marketing
6 */
7
8if ( ! defined( 'ABSPATH' ) && ! defined( 'SNAP_MARKETING_TESTING' ) ) {
9    exit;
10}
11
12/**
13 * Maps UI environment labels to Auth / Audience / SDK endpoint selectors.
14 */
15class Snap_Marketing_Environment {
16
17    const ENV_DEV        = 'Dev';
18    const ENV_QA         = 'Qa';
19    const ENV_SANDBOX    = 'Sandbox';
20    const ENV_PRODUCTION = 'Production';
21
22    /**
23     * Allowed UI environment labels.
24     *
25     * @return string[]
26     */
27    public static function allowed_environments() {
28        return array(
29            self::ENV_DEV,
30            self::ENV_QA,
31            self::ENV_SANDBOX,
32            self::ENV_PRODUCTION,
33        );
34    }
35
36    /**
37     * Normalize a raw environment value to a known UI label.
38     *
39     * @param string $environment Raw environment.
40     * @return string
41     */
42    public static function normalize( $environment ) {
43        $environment = is_string( $environment ) ? trim( $environment ) : '';
44
45        switch ( strtolower( $environment ) ) {
46            case 'dev':
47            case 'development':
48                return self::ENV_DEV;
49            case 'qa':
50                return self::ENV_QA;
51            case 'production':
52            case 'prod':
53            case 'live':
54                return self::ENV_PRODUCTION;
55            case 'sandbox':
56            case 'training':
57            case '':
58            default:
59                if ( in_array( $environment, self::allowed_environments(), true ) ) {
60                    return $environment;
61                }
62                return self::ENV_SANDBOX;
63        }
64    }
65
66    /**
67     * Whether the environment is a production-like live mode.
68     *
69     * @param string $environment Environment label.
70     * @return bool
71     */
72    public static function is_live( $environment ) {
73        return self::ENV_PRODUCTION === self::normalize( $environment );
74    }
75
76    /**
77     * Credential option field names for an environment.
78     *
79     * @param string $environment Environment label.
80     * @return array{id:string,secret:string}
81     */
82    public static function credential_option_keys( $environment ) {
83        switch ( self::normalize( $environment ) ) {
84            case self::ENV_DEV:
85                return array(
86                    'id'     => 'snap_marketing_dev_id',
87                    'secret' => 'snap_marketing_dev_secret_key',
88                );
89            case self::ENV_QA:
90                return array(
91                    'id'     => 'snap_marketing_qa_id',
92                    'secret' => 'snap_marketing_qa_secret_key',
93                );
94            case self::ENV_PRODUCTION:
95                return array(
96                    'id'     => 'snap_marketing_live_id',
97                    'secret' => 'snap_marketing_live_secret_key',
98                );
99            case self::ENV_SANDBOX:
100            default:
101                return array(
102                    'id'     => 'snap_marketing_sandbox_id',
103                    'secret' => 'snap_marketing_sandbox_secret_key',
104                );
105        }
106    }
107
108    /**
109     * Constant names used for Auth / Audience / SDK URLs for an environment.
110     *
111     * @param string $environment Environment label.
112     * @return array{auth:string,audience:string,sdk:string,frequency:string}
113     */
114    public static function endpoint_constant_names( $environment ) {
115        switch ( self::normalize( $environment ) ) {
116            case self::ENV_DEV:
117                return array(
118                    'auth'      => 'Dev_API_URL',
119                    'audience'  => 'Dev_Audience_URL',
120                    'sdk'       => 'Dev_Snap_Marketing_SDK',
121                    'frequency' => 'Sandbox_Snap_Marketing_frequency_URL',
122                );
123            case self::ENV_QA:
124                return array(
125                    'auth'      => 'Qa_API_URL',
126                    'audience'  => 'Qa_Audience_URL',
127                    'sdk'       => 'Qa_Snap_Marketing_SDK',
128                    'frequency' => 'Sandbox_Snap_Marketing_frequency_URL',
129                );
130            case self::ENV_PRODUCTION:
131                return array(
132                    'auth'      => 'Live_API_URL',
133                    'audience'  => 'Live_Audience_URL',
134                    'sdk'       => 'Live_Snap_Marketing_SDK',
135                    'frequency' => 'Live_Snap_Marketing_frequency_URL',
136                );
137            case self::ENV_SANDBOX:
138            default:
139                return array(
140                    'auth'      => 'Sandbox_API_URL',
141                    'audience'  => 'Sandbox_Audience_URL',
142                    'sdk'       => 'Sandbox_Snap_Marketing_SDK',
143                    'frequency' => 'Sandbox_Snap_Marketing_frequency_URL',
144                );
145        }
146    }
147
148    /**
149     * Resolve a defined constant value for the environment endpoint map.
150     *
151     * @param string $environment Environment label.
152     * @param string $key         One of auth|audience|sdk|frequency.
153     * @return string|null
154     */
155    public static function resolve_endpoint( $environment, $key ) {
156        $map = self::endpoint_constant_names( $environment );
157        if ( ! isset( $map[ $key ] ) ) {
158            return null;
159        }
160        $name = $map[ $key ];
161        if ( ! defined( $name ) ) {
162            return null;
163        }
164        $value = constant( $name );
165        return is_string( $value ) ? $value : null;
166    }
167
168    /**
169     * Environments exposed in the admin UI for the current SNAP_ENV.
170     *
171     * @param bool $include_internal When true (snap_qa), include Dev/Qa.
172     * @return array<string,string> value => label
173     */
174    public static function admin_environment_options( $include_internal = false ) {
175        if ( $include_internal ) {
176            return array(
177                self::ENV_DEV        => 'Dev',
178                self::ENV_QA         => 'QA',
179                self::ENV_SANDBOX    => 'Sandbox',
180                self::ENV_PRODUCTION => 'Production',
181            );
182        }
183
184        return array(
185            self::ENV_SANDBOX    => 'Sandbox',
186            self::ENV_PRODUCTION => 'Production',
187        );
188    }
189
190    /**
191     * Whether configured client id and secret are both non-empty.
192     *
193     * @param string $client_id     Client id.
194     * @param string $client_secret Client secret.
195     * @return bool
196     */
197    public static function credentials_configured( $client_id, $client_secret ) {
198        $id     = is_string( $client_id ) ? trim( $client_id ) : '';
199        $secret = is_string( $client_secret ) ? trim( $client_secret ) : '';
200        return '' !== $id && '' !== $secret;
201    }
202}