Test Failed
Push — master ( 5f574e...cd391d )
by Pavel
01:40
created

CreateAction::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 1.037
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Action;
2
3
use Pz\Doctrine\Rest\Request\CreateRequestInterface;
4
use Pz\Doctrine\Rest\RestRepository;
5
use Pz\Doctrine\Rest\RestResponseInterface;
6
7
trait CreateAction
8
{
9
    /**
10
     * Doctrine repository from where get data.
11
     *
12
     * @return RestRepository
13
     */
14
    abstract protected function repository();
15
16
    /**
17
     * @return RestResponseInterface
18
     */
19
    abstract protected function response();
20
21
    /**
22
     * @param CreateRequestInterface $request
23
     *
24
     * @return object
25
     */
26
    abstract protected function createEntity($request);
27
28
    /**
29
     * @param CreateRequestInterface $request
30
     *
31
     * @return array
32
     */
33 1
    public function create(CreateRequestInterface $request)
34
    {
35 1
        $request->authorize($this->repository()->getClassName());
36
37 1
        $entity = $this->createEntity($request);
38
39 1
        $this->repository()->em()->persist($entity);
40
        $this->repository()->em()->flush();
41
42
        return $this->response()->create($request, $entity);
43
    }
44
}
45