Completed
Pull Request — master (#9)
by Roman
03:36
created

DeleteStep   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresBefore() 0 3 1
A execute() 0 12 2
A __construct() 0 3 1
1
<?php
2
3
4
namespace Kami\ApiCoreBundle\RequestProcessor\Step\Delete;
5
6
use Doctrine\Bundle\DoctrineBundle\Registry;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\ORMException;
9
use Kami\ApiCoreBundle\RequestProcessor\Step\AbstractStep;
10
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\FetchEntityByIdStep;
11
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\ValidateResourceAccessStep;
12
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13
14
class DeleteStep extends AbstractStep
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    protected  $manager;
20
21
    public function __construct(Registry $doctrine)
22
    {
23
        $this->manager = $doctrine->getManager();
24
    }
25
26
27
    public function execute()
28
    {
29
        $entity = $this->getFromResponse('entity');
30
31
        try {
32
            $this->manager->remove($entity);
33
            $this->manager->flush();
34
        } catch (ORMException $exception) {
35
            throw new BadRequestHttpException('Your request can not be processed', $exception);
36
        }
37
38
        return $this->createResponse(['response_data' => null], true, 204);
39
    }
40
41
    public function requiresBefore()
42
    {
43
        return [ValidateResourceAccessStep::class, FetchEntityByIdStep::class];
44
    }
45
46
}