22
33/**
44 * This file is part of the Ingram Micro Cloud Blue Connect SDK.
5- *
6- * @copyright (c) 2018. Ingram Micro. All Rights Reserved.
7- */
5+ *
6+ * @copyright (c) 2018. Ingram Micro. All Rights Reserved.
7+ */
88
99namespace Connect ;
1010
11- class Config
11+ class Config
1212{
13- /**
14- * @var - string Connect QuickStart API Key
15- */
16- public $ apiKey ;
17-
18- /**
19- * @var string - Connect QuickStart API Endpoint URI
20- */
21- public $ apiEndpoint ;
22-
23- /**
24- * @var array of strings - list of products to work with
25- */
26- public $ products ;
27-
28- /**
29- * @var int - logLevel - what messages to write to log
30- */
31- public $ logLevel = LoggerInterface::LEVEL_INFO ;
32-
33- /**
34- * @var int - network interaction timeout, seconds
35- */
36- public $ timeout = 50 ;
37-
38- /**
39- * @var bool - do we need to verify SSL certificate of server
40- */
41- public $ sslVerifyHost = true ;
13+ /**
14+ * @var - string Connect QuickStart API Key
15+ */
16+ public $ apiKey ;
17+
18+ /**
19+ * @var string - Connect QuickStart API Endpoint URI
20+ */
21+ public $ apiEndpoint ;
22+
23+ /**
24+ * @var array of strings - list of products to work with
25+ */
26+ public $ products ;
27+
28+ /**
29+ * @var int - logLevel - what messages to write to log
30+ */
31+ public $ logLevel = LoggerInterface::LEVEL_INFO ;
32+
33+ /**
34+ * @var int - network interaction timeout, seconds
35+ */
36+ public $ timeout = 50 ;
37+
38+ /**
39+ * @var bool - do we need to verify SSL certificate of server
40+ */
41+ public $ sslVerifyHost = true ;
4242
4343 /**
4444 * @param mixed $config -
@@ -48,67 +48,73 @@ class Config
4848 * @throws ConfigPropertyInvalid
4949 * @throws \ReflectionException
5050 */
51- public function __construct ($ config )
52- {
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- }
66-
67- if (!is_array ($ config ))
68- throw new ConfigException ("Invalid argument for \\Connect \\Config class constructor: " . gettype ($ config ));
69-
70- $ ref = new \ReflectionClass ($ this );
71- foreach ($ ref ->getProperties () as $ prop ) {
72- $ name = $ prop ->getName ();
73-
74- if (!isset ($ config [$ name ]))
75- continue ;
76-
77- $ value = $ config [$ name ];
78-
79- if ($ name == 'products ' ) {
80- $ prop ->setValue ($ this , is_array ($ value ) ? $ value : array ($ value ));
81- } elseif ($ name == 'logLevel ' ) {
82- $ found = false ;
83- foreach (LoggerInterface::LEVELS as $ k => $ v ) {
84- if (strtoupper ($ value ) == $ v ) {
85- $ prop ->setValue ($ this , $ k );
86- $ found = true ;
87- }
88- }
89- if (!$ found )
90- throw new ConfigPropertyInvalid ('Unknown log level ' , $ name , $ value );
91- } elseif ($ name == "sslVerifyHost " ) {
92- if (!is_bool ($ value ))
93- throw new ConfigPropertyInvalid ('Should be boolean ' , $ name , $ value );
94- $ prop ->setValue ($ this , $ value );
95- } else {
96- $ prop ->setValue ($ this ,$ value );
97- }
98- }
99- }
51+ public function __construct ($ config )
52+ {
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+ }
66+
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 );
93+ }
94+ } elseif ($ name == "sslVerifyHost " ) {
95+ if (!is_bool ($ value )) {
96+ throw new ConfigPropertyInvalid ('Should be boolean ' , $ name , $ value );
97+ }
98+ $ prop ->setValue ($ this , $ value );
99+ } else {
100+ $ prop ->setValue ($ this , $ value );
101+ }
102+ }
103+ }
100104
101105 /**
102106 * Validate configuration
103107 * @throws ConfigPropertyMissed
104108 */
105109 public function validate ()
106- {
107- if (!isset ($ this ->apiKey ))
108- throw new ConfigPropertyMissed ('apiKey ' );
109-
110- if (!isset ($ this ->apiEndpoint ))
111- throw new ConfigPropertyMissed ('apiEndpoint ' );
112-
113- }
110+ {
111+ if (!isset ($ this ->apiKey )) {
112+ throw new ConfigPropertyMissed ('apiKey ' );
113+ }
114+
115+ if (!isset ($ this ->apiEndpoint )) {
116+ throw new ConfigPropertyMissed ('apiEndpoint ' );
117+ }
118+
119+ }
114120}
0 commit comments