Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

src/Description/PositionEnhancer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Sonata\DoctrinePHPCRAdminBundle\Description;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use PHPCR\PathNotFoundException;
7
use PHPCR\Util\PathHelper;
8
use Symfony\Cmf\Component\Resource\Description\Description;
9
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
10
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource;
11
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
12
13
/**
14
 * A description enhancer to add the position/sorting value of children on a parent.
15
 *
16
 * @author Maximilian Berghoff <[email protected]>
17
 */
18
class PositionEnhancer implements DescriptionEnhancerInterface
19
{
20
    /**
21
     * @var \PHPCR\SessionInterface
22
     */
23
    private $session;
24
25
    /**
26
     * @param ManagerRegistry $manager
27
     * @param $sessionName
28
     */
29
    public function __construct(ManagerRegistry $manager, $sessionName)
30
    {
31
        $this->session = $manager->getConnection($sessionName);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function enhance(Description $description)
38
    {
39
        $nodePath = $description->getResource()->getPath();
40
        $nodeName = PathHelper::getNodeName($nodePath);
41
        $parentPath = PathHelper::getParentPath($nodePath);
42
43
        try {
44
            $parentNode = $this->session->getNode($parentPath);
45
        } catch (PathNotFoundException $exception) {
46
            return false;
47
        }
48
49
        $nodeIterator = $parentNode->getNodes();
50
        $nodeIterator->rewind();
51
        $counter = 0;
52
        while ($nodeIterator->valid()) {
53
            ++$counter;
54
            if ($nodeIterator->key() === $nodeName) {
55
                break;
56
            }
57
            $nodeIterator->next();
58
        }
59
60
        $description->set('position', $counter);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supports(PuliResource $resource)
67
    {
68
        if (!$resource instanceof CmfResource) {
69
            return false;
70
        }
71
72
        try {
73
            $parentNode = $this->session->getNode(PathHelper::getParentPath($resource->getPath()));
0 ignored issues
show
$parentNode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
        } catch (PathNotFoundException $exception) {
75
            return false;
76
        }
77
78
        // Todo: check for non orderable type
79
80
        return true;
81
    }
82
}
83