CropFileManager::saveTransformedFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
/**
4
 * Copyright 2014 Jonathan Bouzekri. All rights reserved.
5
 *
6
 * @copyright Copyright 2014 Jonathan Bouzekri <[email protected]>
7
 * @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE
8
 * @link https://github.com/jbouzekri/FileUploaderBundle
9
 */
10
11
namespace Jb\Bundle\FileUploaderBundle\Service;
12
13
use Liip\ImagineBundle\Imagine\Data\DataManager;
14
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
15
use Knp\Bundle\GaufretteBundle\FilesystemMap;
16
use Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration;
17
use Liip\ImagineBundle\Binary\BinaryInterface;
18
19
/**
20
 * Croper
21
 *
22
 * @author jobou
23
 */
24
class CropFileManager
25
{
26
    /**
27
     * @var \Liip\ImagineBundle\Imagine\Data\DataManager
28
     */
29
    protected $dataManager;
30
31
    /**
32
     * @var \Liip\ImagineBundle\Imagine\Filter\FilterManager
33
     */
34
    protected $filterManager;
35
36
    /**
37
     * @var \Knp\Bundle\GaufretteBundle\FilesystemMap
38
     */
39
    protected $filesystemMap;
40
41
    /**
42
     * @var \Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration
43
     */
44
    protected $configuration;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param DataManager $dataManager
50
     * @param FilterManager $filterManager
51
     * @param FilesystemMap $filesystemMap
52
     * @param EndpointConfiguration $configuration
53
     */
54
    public function __construct(
55
        DataManager $dataManager,
56
        FilterManager $filterManager,
57
        FilesystemMap $filesystemMap,
58
        EndpointConfiguration $configuration
59
    ) {
60
        $this->dataManager = $dataManager;
61
        $this->filterManager = $filterManager;
62
        $this->filesystemMap = $filesystemMap;
63
        $this->configuration = $configuration;
64
    }
65
66
    /**
67
     * Transform the file
68
     *
69
     * @param array $data
70
     *
71
     * @return \Liip\ImagineBundle\Binary\BinaryInterface
72
     */
73
    public function transformFile(array $data)
74
    {
75
        return $this->filterManager->apply(
76
            $this->dataManager->find('original', $data['filename']),
77
            array(
78
                'filters' => array(
79
                    'crop'=> array(
80
                        'start' => array($data['x'], $data['y']),
81
                        'size' => array($data['width'], $data['height'])
82
                    )
83
                )
84
            )
85
        );
86
    }
87
88
    /**
89
     * Save the transformed file
90
     *
91
     * @param string $endpoint
92
     * @param BinaryInterface $cropedFile
93
     * @param array $data
94
     */
95
    public function saveTransformedFile($endpoint, BinaryInterface $cropedFile, array $data)
96
    {
97
        $this
98
            ->filesystemMap
99
            ->get(
100
                $this->configuration->getValue($endpoint, 'croped_fs')
101
            )
102
            ->write(
103
                $data['filename'],
104
                $cropedFile->getContent()
105
            );
106
    }
107
}
108