Total Complexity | 41 |
Total Lines | 260 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like Factory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Factory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class Factory |
||
51 | { |
||
52 | /** @var PhiremockFactory */ |
||
53 | private $phiremockFactory; |
||
54 | |||
55 | /** @var StringObjectArrayMap */ |
||
56 | private $factoryCache; |
||
57 | |||
58 | public function __construct(PhiremockFactory $factory) |
||
59 | { |
||
60 | $this->phiremockFactory = $factory; |
||
61 | $this->factoryCache = new StringObjectArrayMap(); |
||
62 | } |
||
63 | |||
64 | public static function createDefault(): self |
||
65 | { |
||
66 | return new self(new PhiremockFactory()); |
||
67 | } |
||
68 | |||
69 | public function createFileSystemService(): FileSystem |
||
70 | { |
||
71 | if (!$this->factoryCache->has('fileSystem')) { |
||
72 | $this->factoryCache->set('fileSystem', new FileSystem()); |
||
73 | } |
||
74 | |||
75 | return $this->factoryCache->get('fileSystem'); |
||
76 | } |
||
77 | |||
78 | public function createLogger(): LoggerInterface |
||
79 | { |
||
80 | if (!$this->factoryCache->has('logger')) { |
||
81 | $logger = new Logger('stdoutLogger'); |
||
82 | $logLevel = IS_DEBUG_MODE ? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO; |
||
83 | $logger->pushHandler(new StreamHandler(STDOUT, $logLevel)); |
||
84 | $this->factoryCache->set('logger', $logger); |
||
85 | } |
||
86 | |||
87 | return $this->factoryCache->get('logger'); |
||
88 | } |
||
89 | |||
90 | public function createHttpResponseStrategy(): HttpResponseStrategy |
||
91 | { |
||
92 | if (!$this->factoryCache->has('httpResponseStrategy')) { |
||
93 | $this->factoryCache->set( |
||
94 | 'httpResponseStrategy', |
||
95 | new HttpResponseStrategy( |
||
96 | $this->createScenarioStorage(), |
||
97 | $this->createLogger() |
||
98 | ) |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | return $this->factoryCache->get('httpResponseStrategy'); |
||
103 | } |
||
104 | |||
105 | public function createRegexResponseStrategy(): RegexResponseStrategy |
||
106 | { |
||
107 | if (!$this->factoryCache->has('regexResponseStrategy')) { |
||
108 | $this->factoryCache->set( |
||
109 | 'regexResponseStrategy', |
||
110 | new RegexResponseStrategy( |
||
111 | $this->createScenarioStorage(), |
||
112 | $this->createLogger() |
||
113 | ) |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | return $this->factoryCache->get('regexResponseStrategy'); |
||
118 | } |
||
119 | |||
120 | public function createProxyResponseStrategy(): ProxyResponseStrategy |
||
121 | { |
||
122 | if (!$this->factoryCache->has('proxyResponseStrategy')) { |
||
123 | $this->factoryCache->set( |
||
124 | 'proxyResponseStrategy', |
||
125 | new ProxyResponseStrategy( |
||
126 | $this->createHttpClient(), |
||
127 | $this->createScenarioStorage(), |
||
128 | $this->createLogger() |
||
129 | ) |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | return $this->factoryCache->get('proxyResponseStrategy'); |
||
134 | } |
||
135 | |||
136 | public function createResponseStrategyLocator(): ResponseStrategyLocator |
||
137 | { |
||
138 | if (!$this->factoryCache->has('responseStrategyLocator')) { |
||
139 | $this->factoryCache->set( |
||
140 | 'responseStrategyLocator', |
||
141 | new ResponseStrategyLocator($this) |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | return $this->factoryCache->get('responseStrategyLocator'); |
||
146 | } |
||
147 | |||
148 | public function createRequestsRouter(): FastRouterHandler |
||
149 | { |
||
150 | if (!$this->factoryCache->has('router')) { |
||
151 | $this->factoryCache->set( |
||
152 | 'router', |
||
153 | new FastRouterHandler($this->createActionLocator(), $this->createLogger()) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | return $this->factoryCache->get('router'); |
||
158 | } |
||
159 | |||
160 | public function createHomePathService(): HomePathService |
||
161 | { |
||
162 | if (!$this->factoryCache->has('homePathService')) { |
||
163 | $this->factoryCache->set( |
||
164 | 'homePathService', |
||
165 | new HomePathService() |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | return $this->factoryCache->get('homePathService'); |
||
170 | } |
||
171 | |||
172 | public function createHttpServer(): ServerInterface |
||
173 | { |
||
174 | if (!$this->factoryCache->has('httpServer')) { |
||
175 | $this->factoryCache->set( |
||
176 | 'httpServer', |
||
177 | new ReactPhpServer($this->createRequestsRouter(), $this->createLogger()) |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | return $this->factoryCache->get('httpServer'); |
||
182 | } |
||
183 | |||
184 | public function createExpectationStorage(): ExpectationStorage |
||
185 | { |
||
186 | if (!$this->factoryCache->has('expectationsStorage')) { |
||
187 | $this->factoryCache->set( |
||
188 | 'expectationsStorage', |
||
189 | new ExpectationAutoStorage() |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | return $this->factoryCache->get('expectationsStorage'); |
||
194 | } |
||
195 | |||
196 | public function createExpectationBackup(): ExpectationStorage |
||
197 | { |
||
198 | if (!$this->factoryCache->has('expectationsBackup')) { |
||
199 | $this->factoryCache->set( |
||
200 | 'expectationsBackup', |
||
201 | new ExpectationAutoStorage() |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | return $this->factoryCache->get('expectationsBackup'); |
||
206 | } |
||
207 | |||
208 | public function createRequestStorage(): RequestStorage |
||
209 | { |
||
210 | if (!$this->factoryCache->has('requestsStorage')) { |
||
211 | $this->factoryCache->set( |
||
212 | 'requestsStorage', |
||
213 | new RequestAutoStorage() |
||
214 | ); |
||
215 | } |
||
216 | |||
217 | return $this->factoryCache->get('requestsStorage'); |
||
218 | } |
||
219 | |||
220 | public function createScenarioStorage(): ScenarioStorage |
||
221 | { |
||
222 | if (!$this->factoryCache->has('scenariosStorage')) { |
||
223 | $this->factoryCache->set( |
||
224 | 'scenariosStorage', |
||
225 | new ScenarioAutoStorage() |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | return $this->factoryCache->get('scenariosStorage'); |
||
230 | } |
||
231 | |||
232 | public function createRequestExpectationComparator(): RequestExpectationComparator |
||
233 | { |
||
234 | if (!$this->factoryCache->has('requestExpectationComparator')) { |
||
235 | $this->factoryCache->set( |
||
236 | 'requestExpectationComparator', |
||
237 | new RequestExpectationComparator( |
||
238 | $this->createScenarioStorage(), |
||
239 | $this->createLogger() |
||
240 | ) |
||
241 | ); |
||
242 | } |
||
243 | |||
244 | return $this->factoryCache->get('requestExpectationComparator'); |
||
245 | } |
||
246 | |||
247 | public function createFileExpectationsLoader(): FileExpectationsLoader |
||
262 | } |
||
263 | |||
264 | public function createActionLocator(): ActionLocator |
||
265 | { |
||
266 | if (!$this->factoryCache->has('actionLocator')) { |
||
267 | $this->factoryCache->set( |
||
268 | 'actionLocator', |
||
269 | new ActionLocator($this->createActionFactory()) |
||
270 | ); |
||
271 | } |
||
272 | |||
273 | return $this->factoryCache->get('actionLocator'); |
||
274 | } |
||
275 | |||
276 | public function createActionFactory(): ActionsFactory |
||
286 | } |
||
287 | |||
288 | public function createRequestToExpectationMapper(): RequestToExpectationMapper |
||
301 | } |
||
302 | |||
303 | public function createHttpClient(): ClientInterface |
||
304 | { |
||
305 | if (!class_exists(GuzzleClient::class, true)) { |
||
306 | throw new \Exception('A default http client implementation is needed. Please extend the factory or install Guzzle Http Client v6'); |
||
307 | } |
||
308 | |||
309 | return new GuzzlePsr18Client(); |
||
310 | } |
||
311 | } |
||
312 |