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

PersistStep::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
}