irishdan /
ResponsiveImageBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the IrishDan\ResponsiveImageBundle package. |
||
| 4 | * |
||
| 5 | * (c) Daniel Byrne <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE file that was distributed with this source |
||
| 8 | * code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace IrishDan\ResponsiveImageBundle\ImageProcessing; |
||
| 12 | |||
| 13 | use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvent; |
||
| 14 | use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvents; |
||
| 15 | use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper; |
||
| 16 | use IrishDan\ResponsiveImageBundle\ResponsiveImageInterface; |
||
| 17 | use IrishDan\ResponsiveImageBundle\StyleManager; |
||
| 18 | use League\Flysystem\FilesystemInterface; |
||
| 19 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class ImageManager |
||
| 23 | * |
||
| 24 | * @package IrishDan\ResponsiveImageBundle\Image |
||
| 25 | */ |
||
| 26 | class ImageManager |
||
| 27 | { |
||
| 28 | protected $styleManager; |
||
| 29 | protected $ImageStyler; |
||
| 30 | protected $fileSystem; |
||
| 31 | protected $temporaryFileSystem; |
||
| 32 | protected $eventDispatcher; |
||
| 33 | protected $generatedImages = []; |
||
| 34 | |||
| 35 | public function __construct( |
||
| 36 | StyleManager $styleManager, |
||
| 37 | ImageStyler $imageStyler, |
||
| 38 | PrimaryFileSystemWrapper $fileSystem, |
||
| 39 | FilesystemInterface $temporaryFileSystem = null, |
||
| 40 | EventDispatcherInterface $eventDispatcher = null |
||
| 41 | ) { |
||
| 42 | $this->styleManager = $styleManager; |
||
| 43 | $this->ImageStyler = $imageStyler; |
||
| 44 | $this->fileSystem = $fileSystem->getFileSystem(); |
||
| 45 | $this->temporaryFileSystem = $temporaryFileSystem; |
||
| 46 | $this->eventDispatcher = $eventDispatcher; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function createAllStyledImages(ResponsiveImageInterface $image) |
||
| 50 | { |
||
| 51 | $styles = $this->styleManager->getAllStylesNames(); |
||
| 52 | |||
| 53 | return $this->createStyledImages($image, $styles); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function createStyledImages(ResponsiveImageInterface $image, array $styles = []) |
||
| 57 | { |
||
| 58 | // Copy the image from its current filesystem onto the filesystem used by intervention. |
||
| 59 | $contents = $this->fileSystem->read($image->getPath()); |
||
| 60 | $this->temporaryFileSystem->put($image->getPath(), $contents); |
||
| 61 | |||
| 62 | // Generate all of the required files |
||
| 63 | foreach ($styles as $style) { |
||
| 64 | // @TODO: Use the relative part instead of the full path!!! |
||
| 65 | $this->createStyledImage($image, $style); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Dispatch an event. |
||
| 69 | if (!empty($this->eventDispatcher)) { |
||
| 70 | $imagesGeneratedEvent = new StyledImagesEvent($image, $this->generatedImages); |
||
| 71 | $this->eventDispatcher->dispatch(StyledImagesEvents::STYLED_IMAGES_GENERATED, $imagesGeneratedEvent); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $this->generatedImages; |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function createStyledImage(ResponsiveImageInterface $image, $style) |
||
| 78 | { |
||
| 79 | $styleData = $this->styleManager->getStyleData($style); |
||
| 80 | |||
| 81 | $directory = $this->temporaryFileSystem->getAdapter()->getPathPrefix(); |
||
| 82 | $source = $directory . $image->getPath(); |
||
| 83 | |||
| 84 | if (!empty($styleData)) { |
||
| 85 | $cropFocusData = $image->getCropCoordinates(); |
||
| 86 | $relativeStylePath = $this->styleManager->getStylePath($image, $style); |
||
| 87 | |||
| 88 | $destination = $directory . $relativeStylePath; |
||
| 89 | |||
| 90 | // Intervention needs directories to exist prior to creating images. |
||
| 91 | $this->createStyleDirectory($relativeStylePath); |
||
| 92 | |||
| 93 | try { |
||
| 94 | $this->ImageStyler->createImage($source, $destination, $styleData, $cropFocusData); |
||
| 95 | $this->generatedImages[$style] = $relativeStylePath; |
||
| 96 | } catch (\Exception $e) { |
||
| 97 | // @TODO: Throw exception |
||
| 98 | } |
||
| 99 | } |
||
| 100 | else { |
||
|
0 ignored issues
–
show
|
|||
| 101 | // throw InvalidArgumentException:: |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | public function deleteAllImages(ResponsiveImageInterface $image) |
||
| 106 | { |
||
| 107 | $this->deleteImage($image); |
||
| 108 | $this->deleteStyledImages($image); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function deleteStyledImages(ResponsiveImageInterface $image, array $styles = []) |
||
| 112 | { |
||
| 113 | if (empty($styles)) { |
||
| 114 | $styles = $this->styleManager->getAllStylesNames(); |
||
| 115 | } |
||
| 116 | |||
| 117 | foreach ($styles as $style) { |
||
| 118 | $this->deleteImage($image, $style); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function deleteImage(ResponsiveImageInterface $image, $style = '') |
||
| 123 | { |
||
| 124 | View Code Duplication | if (!empty($style)) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
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. Loading history...
|
|||
| 125 | $path = $this->styleManager->getStylePath($image, $style); |
||
| 126 | } |
||
| 127 | else { |
||
| 128 | $path = $image->getPath(); |
||
| 129 | } |
||
| 130 | $this->fileSystem->delete($path); |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function createStyleDirectory($destination) |
||
| 134 | { |
||
| 135 | $filename = basename($destination); |
||
| 136 | $directory = explode($filename, $destination)[0]; |
||
| 137 | |||
| 138 | if (!$this->temporaryFileSystem->has($directory)) { |
||
| 139 | $this->temporaryFileSystem->createDir($directory); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | public function imageExists($path) |
||
| 144 | { |
||
| 145 | return $this->fileSystem->has($path); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function createCustomStyledImage(ResponsiveImageInterface $image, $customStyleString, $forceGenerate = false) |
||
| 149 | { |
||
| 150 | // @TODO: To avoid creating images, that already exist, check if it exists, need a way to disable this checking |
||
| 151 | |||
| 152 | // check is it exists, using the string |
||
| 153 | $stylePath = $this->styleManager->getStylePath($image, $customStyleString); |
||
| 154 | |||
| 155 | $exists = $this->imageExists($stylePath); |
||
| 156 | if (!$exists || $forceGenerate) { |
||
| 157 | // Create the style array and add to the existing styles |
||
| 158 | $style = $this->styleManager->styleDataFromCustomStyleString($customStyleString); |
||
| 159 | $this->styleManager->addStyle($customStyleString, $style); |
||
| 160 | |||
| 161 | // generate the image |
||
| 162 | $this->createStyledImages($image, [$customStyleString]); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | public function deleteCustomStyledImages(ResponsiveImageInterface $image) |
||
|
0 ignored issues
–
show
|
|||
| 167 | { |
||
| 168 | // @TODO: Implement |
||
| 169 | } |
||
| 170 | } |
This check looks for the
elsebranches ofifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elsebranches can be removed.could be turned into
This is much more concise to read.