Completed
Push — master ( f11392...85f8c7 )
by Pavel
01:55 queued 18s
created

CanHydrate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 6
cts 7
cp 0.8571
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 13 4
1
<?php namespace Pz\Doctrine\Rest\Traits;
2
3
use Doctrine\ORM\EntityManager;
4
use pmill\Doctrine\Hydrator\ArrayHydrator;
5
use pmill\Doctrine\Hydrator\JsonApiHydrator;
6
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
7
use Pz\Doctrine\Rest\Exceptions\RestException;
8
use Symfony\Component\HttpFoundation\Response;
9
10
trait CanHydrate
11
{
12
    /**
13
     * @param string|object       $entity
14
     * @param EntityManager       $em
15
     * @param RestRequestContract $request
16
     *
17
     * @return object
18
     * @throws \Exception
19
     */
20 3
    protected function hydrate($entity, EntityManager $em, RestRequestContract $request)
21
    {
22 3
        $all = $request->all();
23
24 3
        if ($request->isContentJsonApi()) {
25 3
            if (!isset($all['data']) || !is_array($all['data'])) {
26
                throw RestException::missingRootData();
27
            }
28
29 3
            return (new JsonApiHydrator($em))->hydrate($entity, $all['data']);
30
        }
31
32 2
        return (new ArrayHydrator($em))->hydrate($entity, $all);
33
    }
34
}
35