Total Complexity | 61 |
Total Lines | 499 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ApplePassFactory 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 ApplePassFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ApplePassFactory |
||
33 | { |
||
34 | /** |
||
35 | * Output path for generated pass files |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $outputPath = ''; |
||
40 | |||
41 | /** |
||
42 | * Overwrite if pass exists |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $overwrite = false; |
||
47 | |||
48 | /** |
||
49 | * Pass type identifier |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $passTypeIdentifier; |
||
54 | |||
55 | /** |
||
56 | * Team identifier |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $teamIdentifier; |
||
61 | |||
62 | /** |
||
63 | * Organization name |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $organizationName; |
||
68 | |||
69 | /** |
||
70 | * P12 file |
||
71 | * |
||
72 | * @var \Passbook\Certificate\P12Interface |
||
73 | */ |
||
74 | protected $p12; |
||
75 | |||
76 | /** |
||
77 | * WWDR file |
||
78 | * |
||
79 | * @var \Passbook\Certificate\WWDRInterface |
||
80 | */ |
||
81 | protected $wwdr; |
||
82 | |||
83 | /** |
||
84 | * @var bool - skip signing the pass; should only be used for testing |
||
85 | */ |
||
86 | protected $skipSignature; |
||
87 | |||
88 | /** |
||
89 | * @var PassValidatorInterface |
||
90 | */ |
||
91 | private $passValidator; |
||
92 | |||
93 | /** |
||
94 | * Pass file extension |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | public const PASS_EXTENSION = '.pkpass'; |
||
99 | |||
100 | public function __construct($passTypeIdentifier, $teamIdentifier, $organizationName, $p12File, $p12Pass, $wwdrFile) |
||
101 | { |
||
102 | // Required pass information |
||
103 | $this->passTypeIdentifier = $passTypeIdentifier; |
||
104 | $this->teamIdentifier = $teamIdentifier; |
||
105 | $this->organizationName = $organizationName; |
||
106 | |||
107 | // Create certificate objects |
||
108 | $this->p12 = new P12($p12File, $p12Pass); |
||
109 | $this->wwdr = new WWDR($wwdrFile); |
||
110 | |||
111 | // By default use the PassValidator |
||
112 | $this->passValidator = new PassValidator(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Set outputPath |
||
117 | * |
||
118 | * @param string $outputPath |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function setOutputPath($outputPath) |
||
123 | { |
||
124 | $this->outputPath = $outputPath; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Get outputPath |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getOutputPath() |
||
135 | { |
||
136 | return $this->outputPath; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * The output path with a directory separator on the end. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getNormalizedOutputPath() |
||
145 | { |
||
146 | return rtrim($this->outputPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Set overwrite |
||
151 | * |
||
152 | * @param boolean $overwrite |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setOverwrite($overwrite) |
||
157 | { |
||
158 | $this->overwrite = $overwrite; |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get overwrite |
||
165 | * |
||
166 | * @return boolean |
||
167 | */ |
||
168 | public function isOverwrite() |
||
169 | { |
||
170 | return $this->overwrite; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Set skip signature |
||
175 | * |
||
176 | * When set, the pass will not be signed when packaged. This should only |
||
177 | * be used for testing. |
||
178 | * |
||
179 | * @param boolean $skipSignature |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function setSkipSignature($skipSignature) |
||
184 | { |
||
185 | $this->skipSignature = $skipSignature; |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get skip signature |
||
192 | * |
||
193 | * @return boolean |
||
194 | */ |
||
195 | public function getSkipSignature() |
||
196 | { |
||
197 | return $this->skipSignature; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set an implementation of PassValidatorInterface to validate the pass |
||
202 | * before packaging. When set to null, no validation is performed when |
||
203 | * packaging the pass. |
||
204 | * |
||
205 | * @param PassValidatorInterface|null $passValidator |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setPassValidator(PassValidatorInterface $passValidator = null) |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return PassValidatorInterface |
||
218 | */ |
||
219 | public function getPassValidator() |
||
220 | { |
||
221 | return $this->passValidator; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Serialize pass |
||
226 | * |
||
227 | * @param PassInterface $pass |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | public static function serialize(PassInterface $pass) |
||
232 | { |
||
233 | return self::jsonEncode($pass->toArray()); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Creates a pkpass file |
||
238 | * |
||
239 | * @param PassInterface $pass - the pass to be packaged into a .pkpass file |
||
240 | * @param string $passName - filename to be used for the pass; if blank the serial number will be used |
||
241 | * |
||
242 | * @return SplFileObject If an IO error occurred |
||
243 | * @throws InvalidArgumentException|PassInvalidException|Exception |
||
244 | */ |
||
245 | public function package(PassInterface $pass, $passName = '') |
||
246 | { |
||
247 | if ($pass->getSerialNumber() == '') { |
||
248 | throw new InvalidArgumentException('Pass must have a serial number to be packaged'); |
||
249 | } |
||
250 | |||
251 | $this->populateRequiredInformation($pass); |
||
252 | |||
253 | if ($this->passValidator) { |
||
254 | if (!$this->passValidator->validate($pass)) { |
||
255 | throw new PassInvalidException('Failed to validate passbook', $this->passValidator->getErrors()); |
||
256 | }; |
||
257 | } |
||
258 | |||
259 | $passDir = $this->preparePassDirectory($pass); |
||
260 | |||
261 | // Serialize pass |
||
262 | file_put_contents($passDir . 'pass.json', self::serialize($pass)); |
||
263 | |||
264 | // Images |
||
265 | $this->prepareImages($pass, $passDir); |
||
266 | |||
267 | // Localizations |
||
268 | $this->prepareLocalizations($pass, $passDir); |
||
269 | |||
270 | // Manifest.json - recursive, also add files in sub directories |
||
271 | $manifestJSONFile = $this->prepareManifest($passDir); |
||
272 | |||
273 | // Signature |
||
274 | $this->sign($passDir, $manifestJSONFile); |
||
275 | |||
276 | // Zip pass |
||
277 | $zipFile = $this->getNormalizedOutputPath() . $this->getPassName($passName, $pass) . self::PASS_EXTENSION; |
||
278 | $this->zip($passDir, $zipFile); |
||
279 | |||
280 | // Remove temporary pass directory |
||
281 | $this->rrmdir($passDir); |
||
282 | |||
283 | return new SplFileObject($zipFile); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $passDir |
||
288 | * @param $manifestJSONFile |
||
289 | */ |
||
290 | private function sign($passDir, $manifestJSONFile): void |
||
291 | { |
||
292 | if ($this->getSkipSignature()) { |
||
293 | return; |
||
294 | } |
||
295 | |||
296 | $signatureFile = $passDir . 'signature'; |
||
297 | $p12 = file_get_contents($this->p12->getRealPath()); |
||
298 | $certs = []; |
||
299 | if (openssl_pkcs12_read($p12, $certs, $this->p12->getPassword()) === true) { |
||
300 | $certdata = openssl_x509_read($certs['cert']); |
||
301 | $privkey = openssl_pkey_get_private($certs['pkey'], $this->p12->getPassword()); |
||
302 | openssl_pkcs7_sign( |
||
303 | $manifestJSONFile, |
||
304 | $signatureFile, |
||
305 | $certdata, |
||
306 | $privkey, |
||
307 | [], |
||
308 | PKCS7_BINARY | PKCS7_DETACHED, |
||
309 | $this->wwdr->getRealPath() |
||
310 | ); |
||
311 | // Get signature content |
||
312 | $signature = file_get_contents($signatureFile); |
||
313 | // Check signature content |
||
314 | if (!$signature) { |
||
315 | throw new FileException("Couldn't read signature file."); |
||
316 | } |
||
317 | // Delimiters |
||
318 | $begin = 'filename="smime.p7s"'; |
||
319 | $end = '------'; |
||
320 | // Convert signature |
||
321 | $signature = substr($signature, strpos($signature, $begin) + strlen($begin)); |
||
322 | $signature = substr($signature, 0, strpos($signature, $end)); |
||
323 | $signature = base64_decode($signature); |
||
324 | // Put new signature |
||
325 | if (!file_put_contents($signatureFile, $signature)) { |
||
326 | throw new FileException("Couldn't write signature file."); |
||
327 | } |
||
328 | } else { |
||
329 | throw new FileException('Error reading certificate file'); |
||
330 | } |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Creates a zip of a directory including all sub directories (recursive) |
||
335 | * |
||
336 | * @param $source - path to the source directory |
||
|
|||
337 | * @param $destination - output directory |
||
338 | * |
||
339 | * @return bool |
||
340 | * @throws Exception |
||
341 | */ |
||
342 | private function zip($source, $destination) |
||
343 | { |
||
344 | if (!extension_loaded('zip')) { |
||
345 | throw new Exception('ZIP extension not available'); |
||
346 | } |
||
347 | |||
348 | $source = realpath($source); |
||
349 | if (!is_dir($source)) { |
||
350 | throw new FileException('Source must be a directory.'); |
||
351 | } |
||
352 | |||
353 | $zip = new ZipArchive(); |
||
354 | $shouldOverwrite = $this->isOverwrite() ? ZipArchive::OVERWRITE : 0; |
||
355 | if (!$zip->open($destination, ZipArchive::CREATE | $shouldOverwrite)) { |
||
356 | throw new FileException("Couldn't open zip file."); |
||
357 | } |
||
358 | |||
359 | /* @var $iterator RecursiveIteratorIterator|RecursiveDirectoryIterator */ |
||
360 | $dirIterator = new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS); |
||
361 | $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST); |
||
362 | while ($iterator->valid()) { |
||
363 | if ($iterator->isDir()) { |
||
364 | $zip->addEmptyDir($iterator->getSubPathName()); |
||
365 | } elseif ($iterator->isFile()) { |
||
366 | $zip->addFromString($iterator->getSubPathName(), file_get_contents($iterator->key())); |
||
367 | } |
||
368 | $iterator->next(); |
||
369 | } |
||
370 | |||
371 | return $zip->close(); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Recursive folder remove |
||
376 | * |
||
377 | * @param string $dir |
||
378 | * |
||
379 | * @return bool |
||
380 | */ |
||
381 | private function rrmdir($dir) |
||
382 | { |
||
383 | $files = array_diff(scandir($dir), ['.', '..']); |
||
384 | foreach ($files as $file) { |
||
385 | is_dir("$dir/$file") ? $this->rrmdir("$dir/$file") : unlink("$dir/$file"); |
||
386 | } |
||
387 | |||
388 | return rmdir($dir); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @param PassInterface $pass |
||
393 | */ |
||
394 | private function populateRequiredInformation(PassInterface $pass): void |
||
406 | } |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @param $array |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | private static function jsonEncode($array) |
||
415 | { |
||
416 | // Check if JSON_UNESCAPED_SLASHES is defined to support PHP 5.3. |
||
417 | $options = defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_SLASHES : 0; |
||
418 | return json_encode($array, $options); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param $passName |
||
423 | * @param PassInterface $pass |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getPassName($passName, PassInterface $pass) |
||
428 | { |
||
429 | $passNameSanitised = preg_replace('/[^a-zA-Z0-9]+/', '', $passName); |
||
430 | return strlen($passNameSanitised) != 0 ? $passNameSanitised : $pass->getSerialNumber(); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @param $passDir |
||
435 | * |
||
436 | * @return string |
||
437 | */ |
||
438 | private function prepareManifest($passDir) |
||
439 | { |
||
440 | $manifestJSONFile = $passDir . 'manifest.json'; |
||
441 | $manifest = []; |
||
442 | $files = new RecursiveIteratorIterator( |
||
443 | new RecursiveDirectoryIterator($passDir), |
||
444 | RecursiveIteratorIterator::SELF_FIRST |
||
445 | ); |
||
446 | foreach ($files as $file) { |
||
447 | // Ignore "." and ".." folders |
||
448 | if (in_array(substr($file, strrpos($file, '/') + 1), ['.', '..'])) { |
||
449 | continue; |
||
450 | } |
||
451 | // |
||
452 | $filePath = realpath($file); |
||
453 | if (is_file($filePath) === true) { |
||
454 | $relativePathName = str_replace($passDir, '', $file->getPathname()); |
||
455 | $manifest[$relativePathName] = sha1_file($filePath); |
||
456 | } |
||
457 | } |
||
458 | file_put_contents($manifestJSONFile, $this->jsonEncode($manifest)); |
||
459 | |||
460 | return $manifestJSONFile; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param PassInterface $pass |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | private function preparePassDirectory(PassInterface $pass) |
||
469 | { |
||
470 | $passDir = $this->getNormalizedOutputPath() . $pass->getSerialNumber() . DIRECTORY_SEPARATOR; |
||
471 | $passDirExists = file_exists($passDir); |
||
472 | if ($passDirExists && !$this->isOverwrite()) { |
||
473 | throw new FileException('Temporary pass directory already exists'); |
||
474 | } elseif (!$passDirExists && !mkdir($passDir, 0777, true)) { |
||
475 | throw new FileException("Couldn't create temporary pass directory"); |
||
476 | } |
||
477 | |||
478 | return $passDir; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @param PassInterface $pass |
||
483 | * @param string $passDir |
||
484 | */ |
||
485 | private function prepareImages(PassInterface $pass, $passDir): void |
||
486 | { |
||
487 | /** @var Image $image */ |
||
488 | foreach ($pass->getImages() as $image) { |
||
489 | $fileName = $passDir . $image->getContext(); |
||
490 | if ($image->getDensity() === 2) { |
||
491 | $fileName .= '@2x'; |
||
492 | } elseif ($image->getDensity() === 3) { |
||
493 | $fileName .= '@3x'; |
||
494 | } |
||
495 | |||
496 | $fileName .= '.' . $image->getExtension(); |
||
497 | copy($image->getPathname(), $fileName); |
||
498 | } |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * @param PassInterface $pass |
||
503 | * @param string $passDir |
||
504 | */ |
||
505 | private function prepareLocalizations(PassInterface $pass, $passDir): void |
||
531 | } |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 |