Test Failed
Pull Request — master (#11)
by
unknown
16:45 queued 43s
created

UpdateAction::hydrateRootEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Action;
2
3
use Pz\Doctrine\Rest\Resource\Item;
4
use Pz\Doctrine\Rest\RestResponseFactory;
5
use Pz\Doctrine\Rest\Traits\CanHydrate;
6
use Pz\Doctrine\Rest\RestAction;
7
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
8
use Pz\Doctrine\Rest\RestResponse;
9
use Pz\Doctrine\Rest\Traits\CanValidate;
10
11
class UpdateAction extends RestAction
12
{
13
    use CanHydrate;
0 ignored issues
show
introduced by
The trait Pz\Doctrine\Rest\Traits\CanHydrate requires some properties which are not provided by Pz\Doctrine\Rest\Action\UpdateAction: $reflFields, $associationMappings
Loading history...
14
    use CanValidate;
15
16
    /** @var array */
17
    protected $beforeUpdate = [];
18
19
    /** @var array */
20
    protected $afterUpdate = [];
21
22
    /**
23
     * @param RestRequestContract $request
24
     * @return RestResponse
25
     *
26
     * @throws \Doctrine\ORM\ORMException
27
     * @throws \Doctrine\ORM\OptimisticLockException
28
     * @throws \Pz\Doctrine\Rest\Exceptions\RestException
29
     */
30 1
    public function handle($request)
31
    {
32 1
        $entity = $this->repository()->findById($request->getId());
33
34 1
        $this->authorize($request, $entity);
35 1
        $this->hydrateRootEntity($entity, $request);
36 1
        $this->validateEntity($entity);
37
38 1
        if ($this->hasEvents()) {
39 1
            $this->repository()
40 1
                ->getEntityManager()
41 1
                ->getUnitOfWork()
42 1
                ->computeChangeSets();
43
44 1
            $changeSet = $this->repository()
45 1
                ->getEntityManager()
46 1
                ->getUnitOfWork()
47 1
                ->getEntityChangeSet($entity);
48
        }
49
50 1
        if (isset($changeSet)) {
51 1
            $this->callBeforeUpdate($entity, $changeSet, $request);
52
        }
53
54 1
        $this->repository()
55 1
            ->getEntityManager()
56 1
            ->flush();
57
58 1
        if (isset($changeSet)) {
59 1
            $this->callAfterUpdate($entity, $changeSet, $request);
60
        }
61
62 1
        return RestResponseFactory::resource($request,
63 1
            new Item($entity, $this->transformer())
64
        );
65
    }
66
67
    public function hydrateRootEntity($entity, RestRequestContract $request)
68
    {
69
        return $this->hydrateEntity($entity, $request->getData());
70
    }
71 1
72
    /**
73 1
     * @param callable $cb
74 1
     * @return $this
75
     */
76
    public function beforeUpdate(callable $cb)
77
    {
78
        $this->beforeUpdate[] = $cb;
79
        return $this;
80
    }
81 1
82
    /**
83 1
     * @param callable $cb
84 1
     * @return $this
85
     */
86
    public function afterUpdate(callable $cb)
87
    {
88
        $this->afterUpdate[] = $cb;
89
        return $this;
90 1
    }
91
92 1
    /**
93
     * @return bool
94
     */
95
    protected function hasEvents()
96
    {
97
        return !empty($this->beforeUpdate) || !empty($this->afterUpdate);
98
    }
99 1
100
    /**
101 1
     * @param object $entity
102 1
     * @param array $changeSet
103
     * @param RestRequestContract $request
104 1
     */
105
    protected function callBeforeUpdate($entity, array $changeSet, RestRequestContract $request)
106
    {
107
        foreach ($this->beforeUpdate as $beforeUpdate) {
108
            $beforeUpdate($entity, $changeSet, $request);
109
        }
110 1
    }
111
112 1
    /**
113 1
     * @param object $entity
114
     * @param array $changeSet
115 1
     * @param RestRequestContract $request
116
     */
117
    protected function callAfterUpdate($entity, array $changeSet, RestRequestContract $request)
118
    {
119
        foreach ($this->afterUpdate as $afterUpdate) {
120
            $afterUpdate($entity, $changeSet, $request);
121
        }
122
    }
123
}
124