Completed
Push — master ( 9488c4...585d40 )
by Derek Stephen
08:30
created

User::hasEntityPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Del\Config\Container;
4
5
use Del\Common\Container\RegistrationInterface;
6
use Del\Config\Container\Person as PersonPackage;
7
use Del\Repository\User as UserRepository;
8
use Del\Service\Person;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Del\Config\Container\Person.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Del\Service\User as UserService;
10
use Doctrine\ORM\EntityManager;
11
use Pimple\Container;
12
13
class User implements RegistrationInterface
14
{
15
    /**
16
     * @param Container $c
17
     */
18 1
    public function addToContainer(Container $c)
19
    {
20 1
        $personPackage = new PersonPackage();
21 1
        $personPackage->addToContainer($c);
22 1
        $this->addUserRepository($c);
23 1
        $this->addUserService($c);
24 1
    }
25
26 1
    private function addUserRepository(Container $c)
27
    {
28
        $c['repository.user'] = $c->factory(function ($c) {
29
            /** @var EntityManager $em */
30 1
            $em = $c['doctrine.entity_manager'];
31
            /** @var UserRepository $repo */
32 1
            $repo = $em->getRepository('Del\Entity\User');
33 1
            return $repo;
34 1
        });
35 1
    }
36
37
    private function addUserService(Container $c)
38
    {
39 1
        $c['service.user'] = $c->factory(function ($c) {
40
            $svc = new UserService($c['doctrine.entity_manager'], $c['service.person']);
41
            return $svc;
42 1
        });
43 1
    }
44
45 1
    public function getEntityPath()
46
    {
47 1
        return 'vendor/delboy1978uk/user/src/Entity';
48
    }
49
50 1
    public function hasEntityPath()
51
    {
52 1
        return true;
53
    }
54
55
}