Passed
Pull Request — 1.1 (#1122)
by Tim
07:39
created

EntityManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getConfiguration() 0 3 1
A getRepository() 0 3 1
A persist() 0 3 1
A getApplication() 0 3 1
A getLdapClient() 0 3 1
A getBaseDn() 0 6 1
A find() 0 3 1
A remove() 0 3 1
1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Ldap\EntityManager
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\Ldap;
22
23
use Symfony\Component\Ldap\LdapInterface;
24
use AppserverIo\Ldap\LdapManagerInterface;
25
use AppserverIo\Ldap\EntityManagerInterface;
26
use AppserverIo\Psr\Application\ApplicationInterface;
27
use AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface;
28
29
/**
30
 * Simple LDAP Entity Manager implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/appserver-io/appserver
36
 * @link      http://www.appserver.io
37
 */
38
class EntityManager implements EntityManagerInterface
39
{
40
41
    /**
42
     * The Symfony LDAP client instance.
43
     *
44
     * @var \Symfony\Component\Ldap\LdapClientInterface
45
     */
46
    protected $ldapClient;
47
48
    /**
49
     * The application instance.
50
     *
51
     * @var \AppserverIo\Psr\Application\ApplicationInterface
52
     */
53
    protected $application;
54
55
    /**
56
     * The persistence unit configuration instance.
57
     *
58
     * @var \AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface
59
     */
60
    protected $configuration;
61
62
    /**
63
     * Initializes the Entity Manager with the Symfony LDAP adapter instance,
64
     * the actual application instance and the persistence unit configuration.
65
     *
66
     * @param \Symfony\Component\Ldap\LdapInterface                                        $ldapClient    The LDAP client instance
67
     * @param \AppserverIo\Psr\Application\ApplicationInterface                            $application   The application instance
68
     * @param \AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface $configuration The persistence unit configuration instance
69
     */
70
    public function __construct(
71
        LdapInterface $ldapClient,
72
        ApplicationInterface $application,
73
        PersistenceUnitConfigurationInterface $configuration
74
    ) {
75
76
        // set the instances
77
        $this->ldapClient = $ldapClient;
0 ignored issues
show
Documentation Bug introduced by
$ldapClient is of type Symfony\Component\Ldap\LdapInterface, but the property $ldapClient was declared to be of type Symfony\Component\Ldap\LdapClientInterface. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
78
        $this->application = $application;
79
        $this->configuration = $configuration;
80
    }
81
82
    /**
83
     * Returns the application instance.
84
     *
85
     * @return \AppserverIo\Psr\Application\ApplicationInterface The application instance
86
     */
87
    public function getApplication()
88
    {
89
        return $this->application;
90
    }
91
92
    /**
93
     * Returns the Symfony LDAP instance.
94
     *
95
     * @return \Symfony\Component\Ldap\LdapInterface The Symfony LDAP instance
96
     */
97
    public function getLdapClient()
98
    {
99
        return $this->ldapClient;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ldapClient returns the type Symfony\Component\Ldap\LdapClientInterface which is incompatible with the return type mandated by AppserverIo\Ldap\EntityM...erface::getLdapClient() of AppserverIo\Ldap\LdapClientInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
100
    }
101
102
    /**
103
     * Returns the persistence unit configuration instance.
104
     *
105
     * @return \AppserverIo\Description\Configuration\PersistenceUnitConfigurationInterface The configuration instance
106
     */
107
    public function getConfiguration()
108
    {
109
        return $this->configuration;
110
    }
111
112
    /**
113
     * Returns the base DN => database in datasource configuration.
114
     *
115
     * @return string The base DN
116
     */
117
    public function getBaseDn()
118
    {
119
        return $this->getApplication()
120
                     ->search(sprintf('ds/%s', $this->getConfiguration()->getDatasource()->getName()))
121
                     ->getDatabase()
122
                     ->getDatabaseName();
123
    }
124
125
    /**
126
     * Returns the repository instance for the entity with the passed lookup name.
127
     *
128
     * @param string $lookupName The lookup name of the entity to return the repository for
129
     *
130
     * @return object The LDAP repository instance
131
     */
132
    public function getRepository($lookupName)
133
    {
134
        return $this->getApplication()->search(LdapManagerInterface::IDENTIFIER)->lookupRepositoryByEntityName($lookupName);
135
    }
136
137
    /**
138
     * Finds an entity by its lookup name.
139
     *
140
     * @param string $lookupName The look name of the entity to find
141
     * @param mixed  $id         The identity of the entity to find
142
     *
143
     * @return object|null The entity instance or NULL if the entity can not be found.
144
     *
145
     * @throws \Exception
146
     */
147
    public function find($lookupName, $id)
148
    {
149
        return $this->getRepository($lookupName)->load($id);
150
    }
151
152
    /**
153
     * Tells the EntityManager to make an instance managed and persistent.
154
     *
155
     * The entity will be entered into the database at or before transaction
156
     * commit or as a result of the flush operation.
157
     *
158
     * NOTE: The persist operation always considers entities that are not yet known to
159
     * this EntityManager as NEW. Do not pass detached entities to the persist operation.
160
     *
161
     * @param object $entity The instance to make managed and persistent.
162
     *
163
     * @return void
164
     * @throws \Exception
165
     */
166
    public function persist($entity)
167
    {
168
        throw new \Exception(sprintf('Method "%s" has not been implemented yet!', __METHOD__));
169
    }
170
171
    /**
172
     * Removes an entity instance.
173
     *
174
     * A removed entity will be removed from the database at or before transaction commit
175
     * or as a result of the flush operation.
176
     *
177
     * @param object $entity The entity instance to remove
178
     *
179
     * @return void
180
     * @throws \Exception
181
     */
182
    public function remove($entity)
183
    {
184
        throw new \Exception(sprintf('Method "%s" has not been implemented yet!', __METHOD__));
185
    }
186
}
187