CropController::filterAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
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\Controller;
12
13
use Jb\Bundle\FileUploaderBundle\Form\Type\CropType;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
18
/**
19
 * CropController
20
 *
21
 * @author jobou
22
 */
23
class CropController extends Controller
24
{
25
    /**
26
     * Filter for croping
27
     *
28
     * @param Request $request
29
     * @param string $endpoint
30
     *
31
     * @return JsonResponse
32
     */
33
    public function filterAction(Request $request, $endpoint)
34
    {
35
        $form = $this->createForm(CropType::class);
36
37
        $form->handleRequest($request);
38
39
        // Form invalid. Exit.
40
        if (!$form->isValid()) {
41
            return $this->createErrorResponse(
42
                $this->get('translator')->trans('Invalid crop parameters')
43
            );
44
        }
45
46
        // Else process crop
47
        try {
48
            return new JsonResponse(
49
                $this->get('jb_fileuploader.croper')->crop($endpoint, $form->getData())
50
            );
51
        } catch (\Exception $e) {
52
            return $this->createErrorResponse($e->getMessage());
53
        }
54
    }
55
56
    /**
57
     * Create error message
58
     *
59
     * @param string $message
60
     *
61
     * @return JsonResponse
62
     */
63
    protected function createErrorResponse($message)
64
    {
65
        return new JsonResponse(
66
            array(
67
                'error' => $message
68
            ),
69
            400
70
        );
71
    }
72
}
73