Completed
Push — master ( 658cec...20b116 )
by Philip
06:38
created

AbstractCrudServiceRestResourceController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 113
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A listEntities() 0 4 1
A fetchEntity() 0 9 2
A createEntity() 0 4 1
A updateEntity() 0 4 1
A removeEntity() 0 4 1
A listSubresource() 0 4 1
A buildAssociation() 0 4 1
A createAssociation() 0 4 1
A addAssociation() 0 4 1
A removeAssociation() 0 4 1
A getServiceId() 0 4 1
getService() 0 1 ?
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Controller;
4
5
use Dontdrinkandroot\RestBundle\Metadata\RestMetadataFactory;
6
use Dontdrinkandroot\RestBundle\Service\Normalizer;
7
use Dontdrinkandroot\Service\CrudServiceInterface;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
11
use Symfony\Component\Serializer\SerializerInterface;
12
use Symfony\Component\Validator\Validator\ValidatorInterface;
13
14
/**
15
 * @author Philip Washington Sorst <[email protected]>
16
 */
17
abstract class AbstractCrudServiceRestResourceController extends AbstractRestResourceController
18
{
19 80
    public function __construct(
20
        ValidatorInterface $validator,
21
        RequestStack $requestStack,
22
        RestMetadataFactory $metadataFactory,
23
        PropertyAccessorInterface $propertyAccessor,
24
        SerializerInterface $serializer
25
    ) {
26 80
        parent::__construct(
27 80
            $validator,
28 80
            $requestStack,
29 80
            $metadataFactory,
30 80
            $propertyAccessor,
31 80
            $serializer
32
        );
33 80
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 6
    protected function listEntities(int $page = 1, int $perPage = 50)
39
    {
40 6
        return $this->getService()->findAllPaginated($page, $perPage);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 56
    protected function fetchEntity($id)
47
    {
48 56
        $entity = $this->getService()->find($id);
49 56
        if (null === $entity) {
50 2
            throw new NotFoundHttpException();
51
        }
52
53 56
        return $entity;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 10
    protected function createEntity($entity)
60
    {
61 10
        return $this->getService()->create($entity);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 10
    protected function updateEntity($entity)
68
    {
69 10
        return $this->getService()->update($entity);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    protected function removeEntity($entity)
76
    {
77 2
        $this->getService()->remove($entity);
78 2
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 6
    protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50)
84
    {
85 6
        return $this->getService()->findAssociationPaginated($entity, $subresource, $page, $perPage);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    protected function buildAssociation($parent, string $subresource, $entity)
92
    {
93 2
        return $this->getService()->createAssociation($parent, $subresource, $entity);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 2
    protected function createAssociation($associatedEntity)
100
    {
101 2
        return $this->getService()->create($associatedEntity);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 12
    protected function addAssociation($parent, string $subresource, $subId)
108
    {
109 12
        $this->getService()->addAssociation($parent, $subresource, $subId);
110 12
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 12
    protected function removeAssociation($parent, string $subresource, $subId = null)
116
    {
117 12
        $this->getService()->removeAssociation($parent, $subresource, $subId);
118 12
    }
119
120
    protected function getServiceId()
121
    {
122
        return $this->getCurrentRequest()->attributes->get('_service');
123
    }
124
125
    /**
126
     * @return CrudServiceInterface
127
     */
128
    abstract protected function getService();
129
}
130