PostElement   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 34
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 30 4
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