PostElement::execute()   B
last analyzed

Complexity

Conditions 4
Paths 9

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0016

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 20
cts 21
cp 0.9524
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 9
nop 0
crap 4.0016
1
<?php
2
/**
3
 * This file is part of the Drest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Lee Davis
9
 * @copyright Copyright (c) Lee Davis <@leedavis81>
10
 * @link https://github.com/leedavis81/drest/blob/master/LICENSE
11
 * @license http://opensource.org/licenses/MIT The MIT X License (MIT)
12
 */
13
namespace Drest\Service\Action;
14
15
use DrestCommon\Response\Response;
16
use DrestCommon\ResultSet;
17
18
class PostElement extends AbstractAction
19
{
20
21 1
    public function execute()
22
    {
23 1
        $classMetaData = $this->getMatchedRoute()->getClassMetaData();
24 1
        $em = $this->getEntityManager();
25
26 1
        $entityClass = $classMetaData->getClassName();
27 1
        $object = new $entityClass;
28
29 1
        $this->runHandle($object);
30
        try {
31 1
            $em->persist($object);
32 1
            $em->flush($object);
33
34 1
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_201);
35 1
            if (($location = $this->getMatchedRoute()->getOriginLocation(
36 1
                    $object,
37 1
                    $this->getRequest()->getUrl(),
38 1
                    $this->getEntityManager()
39 1
                )) !== false
40 1
            ) {
41 1
                $this->getResponse()->setHttpHeader('Location', $location);
42 1
            }
43
44 1
            $resultSet = ResultSet::create(['location' => ($location) ? $location : 'unknown'], 'response');
45 1
        } catch (\Exception $e) {
46
            return $this->handleError($e, Response::STATUS_CODE_500);
47
        }
48
49 1
        return $resultSet;
50
    }
51
}
52