Completed
Pull Request — master (#10)
by dan
09:38
created

ImageController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 59
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 50 5
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\Controller;
12
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\BinaryFileResponse;
15
use Symfony\Component\HttpFoundation\StreamedResponse;
16
17
/**
18
 * Class ImageController
19
 *
20
 * @package ResponsiveImageBundle\Controller
21
 */
22
class ImageController extends Controller
23
{
24
    /**
25
     * Generates a derivative image as a response
26
     *
27
     * @param $stylename
28
     * @param $filename
29
     */
30
    public function indexAction($stylename, $filename)
31
    {
32
        // Get image style information.
33
        if (empty($this->get('responsive_image.style_manager')->styleExists($stylename))) {
34
            throw $this->createNotFoundException('The style does not exist');
35
        }
36
37
        // Create image if the file exists.
38
        // $imageEntityClass = $this->getParameter('responsive_image.entity_class');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        $imageObject = $this->get('responsive_image.file_to_object')->getObjectFromFilename($filename);
40
41
        if (!empty($imageObject)) {
42
            $generatedImageArray = $this->get('responsive_image.image_manager')->createStyledImages(
43
                $imageObject,
44
                [$stylename]
45
            );
46
47
            if (!empty($generatedImageArray[$stylename])) {
48
                $path = $generatedImageArray[$stylename];
49
50
                $cache  = $this->get('responsive_image.file_system_factory')->getAdapter();
51
                $stream = $cache->readStream($path);
52
53
                $response = new StreamedResponse();
54
                $response->headers->set('Content-Type', $cache->getMimetype($path));
55
                $response->headers->set('Content-Length', $cache->getSize($path));
56
                $response->setPublic();
57
                $response->setMaxAge(31536000);
58
                $response->setExpires(date_create()->modify('+1 years'));
59
60
                $response->setCallback(
61
                    function () use ($stream) {
62
                        if (ftell($stream['stream']) !== 0) {
63
                            rewind($stream['stream']);
64
                        }
65
                        fpassthru($stream['stream']);
66
                        fclose($stream['stream']);
67
                    }
68
                );
69
70
                return $response;
71
            }
72
            else {
73
                throw $this->createNotFoundException('Derived image could not be created');
74
            }
75
        }
76
        else {
77
            throw $this->createNotFoundException('The file does not exist');
78
        }
79
    }
80
}
81