CropController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterAction() 0 22 3
A createErrorResponse() 0 9 1
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