Completed
Pull Request — master (#244)
by Łukasz
09:35
created

ResourceElement::getRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Doctrine\Admin;
11
12
use FSi\Bundle\AdminBundle\Admin\ResourceRepository\GenericResourceElement;
13
use FSi\Bundle\AdminBundle\Exception\RuntimeException;
14
use FSi\Bundle\ResourceRepositoryBundle\Model\ResourceValue;
15
use FSi\Bundle\ResourceRepositoryBundle\Model\ResourceValueRepository;
16
17
/**
18
 * @author Norbert Orzechowicz <[email protected]>
19
 */
20
abstract class ResourceElement extends GenericResourceElement implements Element
21
{
22
    use ElementImpl {
23
        getRepository as protected getElementRepository;
24
    }
25
26
    /**
27
     * @return \FSi\Bundle\ResourceRepositoryBundle\Doctrine\ResourceRepository
28
     */
29
    public function getRepository()
30
    {
31
        $repository = $this->getElementRepository();
32
33
        if (!$repository instanceof ResourceValueRepository) {
34
            throw new RuntimeException(sprintf(
35
                'Repository for class %s must implement \FSi\Bundle\ResourceRepositoryBundle\Model\ResourceValueRepository',
36
                $this->getClassName()
37
            ));
38
        }
39
40
        return $repository;
41
    }
42
43
    /**
44
     * @param \FSi\Bundle\ResourceRepositoryBundle\Model\ResourceValue $resource
45
     */
46
    public function save(ResourceValue $resource)
47
    {
48
        $this->getObjectManager()->persist($resource);
49
        $this->getObjectManager()->flush();
50
    }
51
}
52