Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | class ImageManager |
||
27 | { |
||
28 | /** |
||
29 | * @var StyleManager |
||
30 | */ |
||
31 | protected $styleManager; |
||
32 | protected $ImageStyler; |
||
33 | protected $fileSystem; |
||
34 | protected $temporaryFileSystem; |
||
35 | protected $eventDispatcher; |
||
36 | protected $generatedImages = []; |
||
37 | |||
38 | public function __construct( |
||
39 | StyleManager $styleManager, |
||
40 | ImageStyler $imageStyler, |
||
41 | PrimaryFileSystemWrapper $fileSystem, |
||
42 | FilesystemInterface $temporaryFileSystem = null, |
||
43 | EventDispatcherInterface $eventDispatcher = null |
||
44 | ) { |
||
45 | $this->styleManager = $styleManager; |
||
46 | $this->ImageStyler = $imageStyler; |
||
47 | $this->fileSystem = $fileSystem->getFileSystem(); |
||
48 | $this->temporaryFileSystem = $temporaryFileSystem; |
||
49 | $this->eventDispatcher = $eventDispatcher; |
||
50 | } |
||
51 | |||
52 | public function createAllStyledImages(ResponsiveImageInterface $image) |
||
53 | { |
||
54 | $styles = $this->styleManager->getAllStylesNames(); |
||
55 | |||
56 | return $this->createStyledImages($image, $styles); |
||
57 | } |
||
58 | |||
59 | public function createStyledImages(ResponsiveImageInterface $image, array $styles = []) |
||
60 | { |
||
61 | // Copy the image from its current filesystem onto the filesystem used by intervention. |
||
62 | $contents = $this->fileSystem->read($image->getPath()); |
||
63 | $this->temporaryFileSystem->put($image->getPath(), $contents); |
||
64 | |||
65 | // Generate all of the required files |
||
66 | foreach ($styles as $style) { |
||
67 | // @TODO: Use the relative part instead of the full path!!! |
||
68 | $this->createStyledImage($image, $style); |
||
69 | } |
||
70 | |||
71 | // Dispatch an event. |
||
72 | if (!empty($this->eventDispatcher)) { |
||
73 | $imagesGeneratedEvent = new StyledImagesEvent($image, $this->generatedImages); |
||
74 | $this->eventDispatcher->dispatch(StyledImagesEvents::STYLED_IMAGES_GENERATED, $imagesGeneratedEvent); |
||
75 | } |
||
76 | |||
77 | return $this->generatedImages; |
||
78 | } |
||
79 | |||
80 | protected function createStyledImage(ResponsiveImageInterface $image, $style) |
||
81 | { |
||
82 | $styleData = $this->styleManager->getStyleData($style); |
||
83 | |||
84 | $directory = $this->temporaryFileSystem->getAdapter()->getPathPrefix(); |
||
85 | $source = $directory . $image->getPath(); |
||
86 | |||
87 | if (!empty($styleData)) { |
||
88 | $cropFocusData = $image->getCropCoordinates(); |
||
89 | $relativeStylePath = $this->styleManager->getStylePath($image, $style); |
||
90 | |||
91 | $destination = $directory . $relativeStylePath; |
||
92 | |||
93 | // Intervention needs directories to exist prior to creating images. |
||
94 | $this->createStyleDirectory($relativeStylePath); |
||
95 | |||
96 | try { |
||
97 | $this->ImageStyler->createImage($source, $destination, $styleData, $cropFocusData); |
||
98 | $this->generatedImages[$style] = $relativeStylePath; |
||
99 | } catch (\Exception $e) { |
||
100 | // @TODO: Throw exception |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public function deleteAllImages(ResponsiveImageInterface $image) |
||
110 | |||
111 | public function deleteStyledImages(ResponsiveImageInterface $image, array $styles = []) |
||
121 | |||
122 | public function deleteImage(ResponsiveImageInterface $image, $style = '') |
||
132 | |||
133 | protected function createStyleDirectory($destination) |
||
142 | |||
143 | public function imageExists($path) |
||
147 | |||
148 | public function createCustomStyledImage(ResponsiveImageInterface $image, $customStyleString, $forceGenerate = false) |
||
165 | |||
166 | public function deleteCustomStyledImages(ResponsiveImageInterface $image) |
||
170 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.