Passed
Push — master ( 6b8b10...614334 )
by Stone
06:50 queued 10s
created

SetPrimaryImageController::setPrimaryController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
namespace App\Controller\Media;
4
5
use App\Entity\Image;
6
use App\Entity\Trick;
7
use App\Event\Image\ImageSetPrimaryEvent;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * Class SetPrimaryImageController
18
 * @package App\Controller\Image
19
 *
20
 * Require the user to be connected for everything here
21
 * @IsGranted("ROLE_USER")
22
 */
23
class SetPrimaryImageController extends AbstractController
24
{
25
26
    /**
27
     * @var EventDispatcherInterface
28
     */
29
    private $dispatcher;
30
31
    public function __construct(EventDispatcherInterface $dispatcher)
32
    {
33
        $this->dispatcher = $dispatcher;
34
    }
35
36
    /**
37
     * @Route("/image/set/{trick}-{image}", name="image.setprimary")
38
     * @param Trick $trick
39
     * @param Image $image
40
     * @return JsonResponse|RedirectResponse
41
     */
42
    public function setPrimaryController(Trick $trick, Image $image, Request $request)
43
    {
44
        $event = new ImageSetPrimaryEvent($image, $trick);
45
        $this->dispatcher->dispatch(ImageSetPrimaryEvent::NAME, $event);
46
47
        if ($request->isXmlHttpRequest()) {
48
            $jsonResponse = array(
49
                'id' => $image->getId(),
50
                'image' => getenv('DEFAULT_UPLOAD_TRICK_IMAGE_PATH').'/'.$image->getImage(),
51
                'isPrimary' => $image->getPrimaryImage(),
52
                'defaultPrimaryImage' => getenv('DEFAULT_IMAGE_PATH').'/'.getenv('DEFAULT_TRICK_IMAGE'),
53
                'isCarousel' => getenv('PRIMARY_IMAGE_CAROUSEL'),
54
            );
55
56
            return new JsonResponse($jsonResponse);
57
        }
58
59
        return $this->redirectToRoute('trick.edit', ['id' => $trick->getId()]);
60
61
62
    }
63
64
}