@@ -89,59 +89,101 @@ public function validateMarkup()
8989 */
9090 private function initializeProvider ()
9191 {
92- $ name = 'provider ' ;
93- $ interface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface ' ;
94- $ componentClass = $ this ->config [$ name ]['class ' ];
95- $ componentConfig = $ this ->config [$ name ]['config ' ];
96- $ component = new $ componentClass ($ this ->moduleContainer , $ componentConfig );
97- if (($ component instanceof $ interface ) === false ) {
98- $ errorMessage = sprintf ('Invalid class «%s» provided for component «%s». ' , $ componentClass , $ name );
99- throw new Exception ($ errorMessage );
100- }
101-
102- $ this ->provider = $ component ;
92+ $ providerName = 'provider ' ;
93+ $ providerClass = $ this ->getComponentClass ($ providerName );
94+ $ providerConfig = $ this ->getComponentConfig ($ providerName );
95+ $ this ->provider = new $ providerClass ($ this ->moduleContainer , $ providerConfig );
96+ $ providerInterface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface ' ;
97+ $ this ->validateComponentInstance ($ this ->provider , $ providerInterface , $ providerName );
10398 }
10499
105100 /**
106101 * Initializes markup validator.
107102 */
108103 private function initializeValidator ()
109104 {
110- $ this ->validator = $ this ->makeComponent (
111- 'validator ' ,
112- 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface '
113- );
105+ $ validatorName = 'validator ' ;
106+ $ validatorClass = $ this ->getComponentClass ($ validatorName );
107+ $ validatorConfig = $ this ->getComponentConfig ($ validatorName );
108+ $ this ->validator = new $ validatorClass ($ validatorConfig );
109+ $ validatorInterface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface ' ;
110+ $ this ->validateComponentInstance ($ this ->validator , $ validatorInterface , $ validatorName );
114111 }
115112
116113 /**
117114 * Initializes markup validator.
118115 */
119116 private function initializeReporter ()
120117 {
121- $ this ->reporter = $ this ->makeComponent (
122- 'reporter ' ,
123- 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupReporterInterface '
124- );
118+ $ reporterName = 'reporter ' ;
119+ $ reporterClass = $ this ->getComponentClass ($ reporterName );
120+ $ reporterConfig = $ this ->getComponentConfig ($ reporterName );
121+ $ this ->reporter = new $ reporterClass ($ reporterConfig );
122+ $ reporterInterface = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupReporterInterface ' ;
123+ $ this ->validateComponentInstance ($ this ->reporter , $ reporterInterface , $ reporterName );
125124 }
126125
127126 /**
128- * Constructs a validator component .
127+ * Returns component class name .
129128 *
130- * @param string $name Component name.
131- * @param string $interface Component interface.
129+ * @param string $componentName Component name.
132130 *
133- * @return object Component instance .
131+ * @return string Component class name .
134132 */
135- private function makeComponent ( $ name , $ interface )
133+ private function getComponentClass ( $ componentName )
136134 {
137- $ componentClass = $ this -> config [ $ name ][ 'class ' ] ;
138- $ componentConfig = $ this ->config [$ name ][ ' config ' ];
139- $ component = new $ componentClass ( $ componentConfig );
140- if (( $ component instanceof $ interface ) === false ) {
141- $ errorMessage = sprintf ('Invalid class «%s» provided for component «%s». ' , $ componentClass , $ name );
135+ $ componentClassKey = 'class ' ;
136+ if ( isset ( $ this ->config [$ componentName ][ $ componentClassKey ]) === false ||
137+ is_string ( $ this -> config [ $ componentName ][ $ componentClassKey ]) === false
138+ ) {
139+ $ errorMessage = sprintf ('Invalid class configuration of component «%s». ' , $ componentName );
142140 throw new Exception ($ errorMessage );
143141 }
144142
145- return $ component ;
143+ $ componentClass = $ this ->config [$ componentName ][$ componentClassKey ];
144+
145+ return $ componentClass ;
146+ }
147+
148+ /**
149+ * Returns component configuration parameters.
150+ *
151+ * @param string $componentName Component name.
152+ *
153+ * @return string Component configuration parameters.
154+ */
155+ private function getComponentConfig ($ componentName )
156+ {
157+ $ componentConfig = array ();
158+
159+ $ componentConfigKey = 'config ' ;
160+ if (isset ($ this ->config [$ componentName ][$ componentConfigKey ]) === true &&
161+ is_array ($ this ->config [$ componentName ][$ componentConfigKey ]) === true
162+ ) {
163+ $ componentConfig = $ this ->config [$ componentName ][$ componentConfigKey ];
164+ }
165+
166+ return $ componentConfig ;
167+ }
168+
169+ /**
170+ * Ensures that a component is an instance of a specifi interface.
171+ *
172+ * @param object $component Component instance to validate.
173+ * @param string $interface Interface to validate component instance against.
174+ * @param string $componentName Component name. User for error logging.
175+ *
176+ * @throws Exception When `component` is not an instance of the `interface`.
177+ */
178+ private function validateComponentInstance ($ component , $ interface , $ componentName )
179+ {
180+ if (($ component instanceof $ interface ) === false ) {
181+ $ componentClass = get_class ($ component );
182+ $ errorMessage = vsprintf ('Invalid class «%s» provided for component «%s». ' , array (
183+ $ componentClass ,
184+ $ componentName ,
185+ ));
186+ throw new Exception ($ errorMessage );
187+ }
146188 }
147189}
0 commit comments