PhotoService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 55
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 10 2
A upload() 0 20 3
1
<?php
2
3
namespace AppBundle\Service;
4
5
use AppBundle\Entity\Meal;
6
use Imagine\Image\Box;
7
use Imagine\Image\ImageInterface;
8
use Imagine\Imagick\Imagine;
9
use Symfony\Component\HttpFoundation\File\File;
10
11
class PhotoService
12
{
13
    /**
14
     * @var string
15
     */
16
    private $uploadsDir;
17
18
    /**
19
     * @var string
20
     */
21
    private $uploadsUriPrefix;
22
23
    private $sizes = array(
24
        '300'  => array(300, 100),
25
        '800' => array(800,  600),
26
    );
27
28
    public function __construct($uploadsDir, $uploadsUriPrefix)
29
    {
30
        $this->uploadsDir = $uploadsDir;
31
        $this->uploadsUriPrefix = $uploadsUriPrefix;
32
    }
33
34
    public function get(Meal $meal, $size)
35
    {
36
        $filename = $meal->getId().'-'.$size.'.jpg';
37
38
        if (file_exists($this->uploadsDir.'/'.$filename)) {
39
            return $this->uploadsUriPrefix.'/'.$filename;
40
        }
41
42
        return null;
43
    }
44
45
    public function upload(Meal $meal, File $file)
46
    {
47
        $imagine = new Imagine();
48
        //$mode    = ImageInterface::THUMBNAIL_INSET;
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
49
        $mode    = ImageInterface::THUMBNAIL_OUTBOUND;
50
51
        foreach ($this->sizes as $name => $size) {
52
            $path = $this->uploadsDir.'/'.$meal->getId().'-'.$name.'.jpg';
53
54
            $dir = dirname($path);
55
            if (!is_dir($dir)) {
56
                mkdir($dir, 0777, true);
57
            }
58
59
            $imagine->open($file)
60
                ->thumbnail(new Box($size[0], $size[1]), $mode)
61
                ->save($path)
62
            ;
63
        }
64
    }
65
}
66