88
99namespace Connect ;
1010
11- class Config
11+ /**
12+ * Class Config
13+ * @property string $apiKey
14+ * @property string $apiEndpoint
15+ * @package Connect
16+ */
17+ class Config extends Model
1218{
1319 /**
14- * @var - string Connect QuickStart API Key
20+ * Connect QuickStart API Key
21+ * @var string
1522 */
16- public $ apiKey ;
23+ protected $ apiKey ;
1724
1825 /**
19- * @var string - Connect QuickStart API Endpoint URI
26+ * Connect QuickStart API Endpoint URI
27+ * @var string
2028 */
21- public $ apiEndpoint ;
29+ protected $ apiEndpoint ;
2230
2331 /**
24- * @var array of strings - list of products to work with
32+ * List of products to work with
33+ * @var string[]
2534 */
2635 public $ products ;
2736
2837 /**
29- * @var int - logLevel - what messages to write to log
38+ * What messages to write to log (legacy)
39+ * @var int
40+ */
41+ public $ logLevel = 2 ;
42+
43+ /**
44+ * Enable the debug mode
45+ * @var bool
3046 */
31- public $ logLevel = LoggerInterface:: LEVEL_INFO ;
47+ public $ debug = false ;
3248
3349 /**
34- * @var int - network interaction timeout, seconds
50+ * Network interaction timeout, seconds
51+ * @var int
3552 */
3653 public $ timeout = 50 ;
3754
3855 /**
39- * @var bool - do we need to verify SSL certificate of server
56+ * Do we need to verify SSL certificate of server
57+ * @var bool
4058 */
4159 public $ sslVerifyHost = true ;
4260
61+
4362 /**
44- * @param mixed $config -
63+ * List of runtime providers
64+ * @var array
65+ */
66+ protected $ providers = [
67+ 'curl ' => '\Connect\RuntimeProvider\CurlProvider ' ,
68+ 'logger ' => '\Connect\RuntimeProvider\LoggerProvider '
69+ ];
70+
71+ /**
72+ * @param array|object|string $source
4573 * array -> has pairs of key/value to fill in config
4674 * string -> path to file to read config from
75+ *
4776 * @throws ConfigException
4877 * @throws ConfigPropertyInvalid
4978 * @throws \ReflectionException
5079 */
51- public function __construct ($ config )
80+ public function __construct ($ source )
5281 {
53- if (is_string ($ config )) {
54- try {
55- $ txt = file_get_contents ($ config );
56- } catch (\Exception $ e ) {
57- throw new ConfigException ("Can't read file $ config: " . $ e ->getMessage ());
58- }
59-
60- try {
61- $ config = json_decode ($ txt , true );
62- } catch (\Exception $ e ) {
63- throw new ConfigException ("Can't parse JSON in file $ config: " . $ e ->getMessage ());
64- }
65- }
82+ switch (gettype ($ source )) {
83+ case 'string ' :
6684
67- if (!is_array ($ config )) {
68- throw new ConfigException ("Invalid argument for \\Connect \\Config class constructor: " . gettype ($ config ));
69- }
70-
71- $ ref = new \ReflectionClass ($ this );
72- foreach ($ ref ->getProperties () as $ prop ) {
73- $ name = $ prop ->getName ();
74-
75- if (!isset ($ config [$ name ])) {
76- continue ;
77- }
78-
79- $ value = $ config [$ name ];
80-
81- if ($ name == 'products ' ) {
82- $ prop ->setValue ($ this , is_array ($ value ) ? $ value : array ($ value ));
83- } elseif ($ name == 'logLevel ' ) {
84- $ found = false ;
85- foreach (LoggerInterface::LEVELS as $ k => $ v ) {
86- if (strtoupper ($ value ) == $ v ) {
87- $ prop ->setValue ($ this , $ k );
88- $ found = true ;
89- }
90- }
91- if (!$ found ) {
92- throw new ConfigPropertyInvalid ('Unknown log level ' , $ name , $ value );
85+ if (!is_readable ($ source )) {
86+ throw new ConfigException ("Can't read file $ source " );
9387 }
94- } elseif ( $ name == " sslVerifyHost " ) {
95- if (!is_bool ( $ value )) {
96- throw new ConfigPropertyInvalid ( ' Should be boolean ' , $ name , $ value );
88+ $ source = json_decode ( file_get_contents ( $ source ), true );
89+ if (!isset ( $ source )) {
90+ throw new ConfigException ( " Can't parse JSON config file. " );
9791 }
98- $ prop ->setValue ($ this , $ value );
99- } else {
100- $ prop ->setValue ($ this , $ value );
101- }
92+ break ;
93+ case 'array ' :
94+ case 'object ' :
95+ break ;
96+ default :
97+ throw new ConfigException ("Invalid argument for \\Connect \\Config class constructor: " . gettype ($ source ));
10298 }
99+
100+ parent ::__construct ($ source );
103101 }
104102
105103 /**
106- * Validate configuration
104+ * Validate and set the API Key property
105+ * @param $value
107106 * @throws ConfigPropertyMissed
108107 */
109- public function validate ( )
108+ public function setApiKey ( $ value )
110109 {
111- if (! isset ( $ this -> apiKey )) {
112- throw new ConfigPropertyMissed (' apiKey ' );
110+ if (empty ( $ value )) {
111+ throw new ConfigPropertyMissed (" Missing required property apiKey. " );
113112 }
113+ $ this ->apiKey = trim ($ value );
114+ }
114115
115- if (!isset ($ this ->apiEndpoint )) {
116- throw new ConfigPropertyMissed ('apiEndpoint ' );
116+ /**
117+ * Validate and set the API Endpoint property
118+ * @param $value
119+ * @throws ConfigPropertyMissed
120+ */
121+ public function setApiEndpoint ($ value )
122+ {
123+ if (empty ($ value )) {
124+ throw new ConfigPropertyMissed ("Missing required property apiEndpoint. " );
117125 }
118126
127+ $ this ->apiEndpoint = trim ($ value );
119128 }
120- }
129+ }
0 commit comments