ImageManipulator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initializeFormatCallables() 0 7 1
A formatImageData() 0 22 3
1
<?php
2
3
namespace BWC\Share\Image;
4
5
class ImageManipulator
6
{
7
    const FORMAT_JPG = 'jpg';
8
    const FORMAT_PNG = 'png';
9
10
    /**
11
     * @var array
12
     */
13
    protected $formats;
14
15
    public function __construct()
16
    {
17
        $this->initializeFormatCallables();
18
    }
19
20
    /**
21
     * Initialize supported format and their callables
22
     */
23
    protected function initializeFormatCallables()
24
    {
25
        $this->formats = array(
26
          self::FORMAT_PNG => 'imagepng',
27
          self::FORMAT_JPG => 'imagejpeg',
28
        );
29
    }
30
31
    /**
32
     * Formats image resource data to chosen format.
33
     *
34
     * @param resource $data
35
     * @param string $format
36
     * @param int $quality
37
     *
38
     * @return string
39
     *
40
     * @throws \InvalidArgumentException
41
     */
42
    protected function formatImageData($data, $format, $quality = null)
43
    {
44
        if (!isset($this->formats[$format])) {
45
            throw new \InvalidArgumentException(sprintf(
46
              'Unsupported format %s. Supported formats are: %s',
47
              $format,
48
              implode(', ', array_keys($this->formats))
49
            ));
50
        }
51
52
        $params = array($data);
53
        if (null !== $quality) {
54
            $params[] = null;
55
            $params[] = $quality;
56
        }
57
58
        ob_start();
59
        call_user_func_array($this->formats[$format], $params);
60
        $formatted = ob_get_clean();
61
62
        return $formatted;
63
    }
64
} 
65