Admin   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 8
dl 0
loc 113
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setRootPath() 0 4 1
A getRootPath() 0 4 1
A createQuery() 0 11 2
A id() 0 4 1
A toString() 0 19 6
A getSubject() 0 16 6
A configureRoutes() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Admin;
15
16
use PHPCR\Util\PathHelper;
17
use PHPCR\Util\UUIDHelper;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
20
use Sonata\AdminBundle\Route\RouteCollection;
21
22
/**
23
 * Extend the Admin class to incorporate phpcr changes.
24
 *
25
 * Especially make sure that there are no duplicated slashes in the generated urls
26
 *
27
 * @author Uwe Jäger <[email protected]>
28
 */
29
class Admin extends AbstractAdmin
30
{
31
    /**
32
     * Path to the root node in the repository under which documents of this
33
     * admin should be created.
34
     *
35
     * @var string
36
     */
37
    private $rootPath;
38
39
    /**
40
     * Set the root path in the repository. To be able to create new items,
41
     * this path must already exist.
42
     *
43
     * @param string $rootPath
44
     */
45
    public function setRootPath($rootPath): void
46
    {
47
        $this->rootPath = $rootPath;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getRootPath()
54
    {
55
        return $this->rootPath;
56
    }
57
58
    /**
59
     * @param string $context
60
     *
61
     * @return ProxyQueryInterface
62
     */
63
    public function createQuery($context = 'list')
64
    {
65
        $query = $this->getModelManager()->createQuery($this->getClass());
66
        $query->setRootPath($this->getRootPath());
67
68
        foreach ($this->extensions as $extension) {
69
            $extension->configureQuery($this, $query, $context);
70
        }
71
72
        return $query;
73
    }
74
75
    /**
76
     * @param object $object
77
     *
78
     * @return string
79
     */
80
    public function id($object)
81
    {
82
        return $this->getUrlsafeIdentifier($object);
83
    }
84
85
    /**
86
     * Get subject.
87
     *
88
     * Overridden to allow a broader set of valid characters in the ID, and
89
     * if the ID is not a UUID, to call absolutizePath on the ID.
90
     *
91
     * @return mixed
92
     */
93
    public function getSubject()
94
    {
95
        if (null === $this->subject && $this->request) {
96
            $id = $this->request->get($this->getIdParameter());
97
            if (null === $id || !preg_match('#^[0-9A-Za-z/\-_]+$#', $id)) {
98
                $this->subject = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object|null of property $subject.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
            } else {
100
                if (!UUIDHelper::isUUID($id)) {
101
                    $id = PathHelper::absolutizePath($id, '/');
102
                }
103
                $this->subject = $this->getObject($id);
104
            }
105
        }
106
107
        return $this->subject;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->subject; of type object|null|false adds false to the return on line 107 which is incompatible with the return type declared by the interface Sonata\AdminBundle\Admin...inInterface::getSubject of type object|null. It seems like you forgot to handle an error condition.
Loading history...
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function toString($object)
114
    {
115
        if (!\is_object($object)) {
116
            return parent::toString($object);
117
        }
118
119
        if (method_exists($object, '__toString') && null !== $object->__toString()) {
120
            $string = (string) $object;
121
122
            return '' !== $string ? $string : $this->trans('link_add', [], 'SonataAdminBundle');
123
        }
124
125
        $dm = $this->getModelManager()->getDocumentManager();
126
        if ($dm->contains($object)) {
127
            return PathHelper::getNodeName($dm->getUnitOfWork()->getDocumentId($object));
128
        }
129
130
        return parent::toString($object);
131
    }
132
133
    protected function configureRoutes(RouteCollection $collection): void
134
    {
135
        foreach (['edit', 'create', 'delete'] as $name) {
136
            if ($collection->has($name)) {
137
                $collection->get($name)->addOptions(['expose' => true]);
138
            }
139
        }
140
    }
141
}
142