Completed
Push — move_function ( 756717...0cdbe3 )
by Maximilian
04:27 queued 02:17
created

PositionEnhancer::enhance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Sonata\DoctrinePHPCRAdminBundle\Description;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use PHPCR\PathNotFoundException;
7
use Symfony\Cmf\Component\Resource\Description\Description;
8
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
9
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource;
10
11
/**
12
 * A description enhancer to add the position/sorting value of children on a parent.
13
 *
14
 * @author Maximilian Berghoff <[email protected]>
15
 */
16
class PositionEnhancer implements DescriptionEnhancerInterface
17
{
18
    /**
19
     * @var \PHPCR\SessionInterface
20
     */
21
    private $session;
22
23
    public function __construct(ManagerRegistry $manager, $sessionName)
24
    {
25
        $this->session = $manager->getConnection($sessionName);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function enhance(Description $description)
32
    {
33
        try {
34
            $node = $this->session->getNode($description->getResource()->getPath());
35
        } catch (PathNotFoundException $exception) {
36
            return;
37
        }
38
39
        $description->set('position', $node->getIndex());
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function supports(PuliResource $resource)
46
    {
47
        return true;
48
    }
49
}
50