Passed
Pull Request — master (#9)
by Roman
03:21
created

PersistStep   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 12 2
A requiresBefore() 0 3 1
1
<?php
2
3
4
namespace Kami\ApiCoreBundle\RequestProcessor\Step\Common;
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 Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
11
12
class PersistStep extends AbstractStep
13
{
14
    /**
15
     * @var EntityManager
16
     */
17
    protected $manager;
18
19
    public function __construct(Registry $doctrine)
20
    {
21
        $this->manager = $doctrine->getManager();
22
    }
23
24
    public function execute()
25
    {
26
        $entity = $this->getFromResponse('entity');
27
28
        try {
29
            $this->manager->persist($entity);
30
            $this->manager->flush();
31
        } catch (\Exception $exception) {
32
            throw new BadRequestHttpException('Your request can not be stored', $exception);
33
        }
34
35
        return $this->createResponse(['response_data' => $entity]);
36
    }
37
38
    public function requiresBefore()
39
    {
40
        return [ValidateFormStep::class];
41
    }
42
43
44
}