DriverFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 69
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A driversFromManager() 0 4 1
A driversFromMetadataDriver() 0 4 1
B fromMetadataDriver() 0 35 6
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Factory;
13
14
use Cubiche\Core\Metadata\Driver\ChainDriver;
15
use Cubiche\Core\Metadata\Driver\DriverInterface;
16
use Cubiche\Core\Metadata\Driver\MergeableDriver;
17
use Cubiche\Core\Metadata\Factory\DriverFactory as BaseDriverFactory;
18
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Locator\DoctrineAdapterLocator;
19
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver;
20
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
21
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
22
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
23
use Doctrine\Common\Persistence\ObjectManager;
24
25
/**
26
 * DriverFactory class.
27
 *
28
 * @author Ivannis Suárez Jerez <[email protected]>
29
 */
30
class DriverFactory extends BaseDriverFactory
31
{
32
    /**
33
     * Create a driver from a Doctrine object manager.
34
     *
35
     * @param ObjectManager $om
36
     *
37
     * @return DriverInterface
38
     */
39
    public static function driversFromManager(ObjectManager $om)
40
    {
41
        return self::driversFromMetadataDriver($om->getConfiguration()->getMetadataDriverImpl());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConfiguration() does only exist in the following implementations of said interface: Doctrine\ODM\MongoDB\DocumentManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
42
    }
43
44
    /**
45
     * Create a driver from a Doctrine metadata driver.
46
     *
47
     * @param MappingDriver $omDriver
48
     *
49
     * @return DriverInterface
50
     */
51
    public static function driversFromMetadataDriver(MappingDriver $omDriver)
52
    {
53
        return static::instance()->fromMetadataDriver($omDriver);
54
    }
55
56
    /**
57
     * Create the cubiche drivers from the Doctrine metadata drivers.
58
     *
59
     * @param MappingDriver $omDriver
60
     *
61
     * @return DriverInterface
62
     */
63
    protected function fromMetadataDriver(MappingDriver $omDriver)
64
    {
65
        if ($omDriver instanceof MappingDriverChain) {
66
            $drivers = array();
67
            foreach ($omDriver->getDrivers() as $nestedOmDriver) {
68
                $drivers[] = $this->fromMetadataDriver($nestedOmDriver);
69
            }
70
71
            return new ChainDriver($drivers);
72
        }
73
74
        if ($omDriver instanceof AnnotationDriver) {
75
            return new MergeableDriver(
76
                $this->createAnnotationDriver($omDriver->getReader())
77
            );
78
        }
79
80
        if ($omDriver instanceof FileDriver) {
81
            $reflClass = new \ReflectionClass($omDriver);
82
83
            $driverName = $reflClass->getShortName();
84
            if (strpos($driverName, 'Simplified') !== false) {
85
                $driverName = str_replace('Simplified', '', $driverName);
86
            }
87
88
            return new MergeableDriver(
89
                call_user_func(
90
                    array(static::class, 'create'.$driverName),
91
                    new DoctrineAdapterLocator($omDriver->getLocator())
92
                )
93
            );
94
        }
95
96
        throw new \InvalidArgumentException('Cannot adapt Doctrine driver of class '.get_class($omDriver));
97
    }
98
}
99