Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

TypeGuesser::guessType()   D

Complexity

Conditions 18
Paths 27

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 48
rs 4.9067
cc 18
eloc 33
nc 27
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\DoctrinePHPCRAdminBundle\Guesser;
13
14
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
15
use Sonata\AdminBundle\Model\ModelManagerInterface;
16
17
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
18
use Doctrine\ODM\PHPCR\Mapping\MappingException;
19
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
20
21
use Symfony\Component\Form\Guess\Guess;
22
use Symfony\Component\Form\Guess\TypeGuess;
23
24
/**
25
 * Guesser for displaying fields.
26
 *
27
 * Form guesses happen in the FormContractor.
28
 */
29
class TypeGuesser implements TypeGuesserInterface
30
{
31
    /**
32
     * @var ManagerRegistry
33
     */
34
    protected $registry;
35
36
    /**
37
     * @var array
38
     */
39
    private $cache;
40
41
    /**
42
     * @param ManagerRegistry $registry
43
     */
44
    public function __construct(ManagerRegistry $registry)
45
    {
46
        $this->registry = $registry;
47
        $this->cache = array();
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function guessType($class, $property, ModelManagerInterface $modelManager)
54
    {
55
        if (!$metadata = $this->getMetadata($class)) {
56
            return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
57
        }
58
59
        if ($metadata->hasAssociation($property)) {
60
            $mapping = $metadata->mappings[$property];
61
62
            switch ($mapping['type']) {
63
                case ClassMetadata::MANY_TO_MANY:
64
                case 'referrers':
65
                    return new TypeGuess('doctrine_phpcr_many_to_many', array(), Guess::HIGH_CONFIDENCE);
66
67
                case ClassMetadata::MANY_TO_ONE:
68
                case 'parent':
69
                    return new TypeGuess('doctrine_phpcr_many_to_one', array(), Guess::HIGH_CONFIDENCE);
70
71
                case 'children':
72
                    return new TypeGuess('doctrine_phpcr_one_to_many', array(), Guess::HIGH_CONFIDENCE);
73
74
                case 'child':
75
                    return new TypeGuess('doctrine_phpcr_one_to_one', array(), Guess::HIGH_CONFIDENCE);
76
            }
77
        }
78
79
        // TODO: missing multivalue support
80
        switch ($metadata->getTypeOfField($property)) {
81
            case 'boolean':
82
                return new TypeGuess('boolean', array(), Guess::HIGH_CONFIDENCE);
83
            case 'date':
84
                return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
85
86
            case 'decimal':
87
            case 'double':
88
                return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
89
            case 'integer':
90
            case 'long':
91
                return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
92
            case 'string':
93
                return new TypeGuess('string', array(), Guess::HIGH_CONFIDENCE);
94
            case 'binary':
95
            case 'uri':
96
                return new TypeGuess('string', array(), Guess::MEDIUM_CONFIDENCE);
97
        }
98
99
        return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
100
    }
101
102
    /**
103
     * @param string $class
104
     *
105
     * @return mixed
106
     */
107
    protected function getMetadata($class)
108
    {
109
        if (array_key_exists($class, $this->cache)) {
110
            return $this->cache[$class];
111
        }
112
113
        $this->cache[$class] = null;
114
        foreach ($this->registry->getManagers() as $dm) {
115
            try {
116
                return $this->cache[$class] = $dm->getClassMetadata($class);
117
            } catch (MappingException $e) {
118
                // not an entity or mapped super class
119
            }
120
        }
121
122
        return null;
123
    }
124
}
125